//简单用java实现一下堆的数据结构
1 package com.datastruct; 2 3 import java.util.ArrayList; 4 5 public class BigHeap { 6 7 8 ArrayList<Integer> heapList = new ArrayList<>(); 9 10 /* 11 *交换堆中的两个元素 12 */ 13 private void swap(int srcIndex,int dstIndex) 14 { 15 int tmp = heapList.get(srcIndex); 16 17 heapList.set(srcIndex,heapList.get(dstIndex)); 18 heapList.set(dstIndex, tmp); 19 20 } 21 22 /* 23 *将指定元素的位置进行上移操作 24 */ 25 private void HeapUp(int index) 26 { 27 28 if(index > 1) 29 { 30 int parent = index / 2; 31 int parentVal = heapList.get(parent).intValue(); 32 int indexVal = heapList.get(index).intValue(); 33
大顶堆(递归实现)
最新推荐文章于 2023-04-04 15:30:33 发布
本文介绍了如何使用递归实现大顶堆。在删除堆中元素时,若交换后导致子节点大于父节点,需要进行上移操作以保持堆的性质。

最低0.47元/天 解锁文章
1205

被折叠的 条评论
为什么被折叠?



