Heap的Array实现法
来源:互联网 发布:qq风险软件 编辑:程序博客网 时间:2024/06/05 06:00
总论
Heap 逻辑上二叉树形状 物理上线性 array实现
这里做的是一个大根堆,涵盖方法有:
insert(): insert a new element to the heap
this invokes a help method called moveUp() cuz the inserted element may be larger than its parent, we need to move it up
初始建堆也就用insert()
remove(): remove the root of the heap
this invokes a help method called moveDown() cuz once the root is removed,
we use the rightmost element from the last level (logical view) to substitute the original root, and then this new root may not be the larger than its two children, so we need to move it down until it reaches its right position.
Def:
parent = (index - 1) / 2;
left = 2 * index + 1;
right = 2 * index + 2;
filling array from position 0
痛点
1.什么时候定义parent, left, right 这些变量?
何时用何时定,比如moveUp()用到parent所以在那里定义parent
2.moveUp 和 moveDown 是对 index进行操作
3.moveUp写法 出loop的条件 index <= 0
4.moveDown() 出Loop的条件 – index > size/2
5.粗心错,注意 remove() 补充root时要用array[size-1];然后size–; 当然也可以写 array[0]=array[–size];
代码
public class Heap { private static final int maxSize = 10; //max size of this array is 10 private int[] array; private int size; public Heap() { array = new int[maxSize]; size = 0; } /** * @param newVal being inserted into this heap */ public boolean insert(int newVal) { if (size == maxSize) { return false; } array[size] = newVal; moveUp(size); size++; return true; } /** * moveUp要跟parent比较所以parent在其中有定义 * @param index */ private void moveUp(int index) { int parent = (index - 1) / 2; int bottom = array[index]; //temporarily save the current element while (index > 0 && bottom > array[parent]) { array[index] = array[parent]; //use parent element to substitute ele in current index index = parent; parent = (index - 1) / 2; } array[index] = bottom; } /** * @return the heap root */ public int remove() { int root = array[0]; array[0] = array[size-1]; size--; moveDown(0); return root; } private void moveDown(int index) { int largerChild; int top = array[index]; //先存起来最后再放入 while (index < size / 2) { int left = 2 * index + 1; int right = 2 * index + 2; largerChild = (right < size && array[left] < array[right]) ? right : left; if (top >= array[largerChild]) { break; } array[index] = array[largerChild]; index = largerChild; } array[index] = top; } public void display() { System.out.print("The current heap is: "); for (int i = 0; i < size; i++) { System.out.print(array[i] + " "); } System.out.println(); } public static void main(String[] args) { /* construct a heap */ Heap heap = new Heap(); heap.insert(74); heap.insert(51); heap.insert(26); heap.insert(3); heap.insert(83); heap.insert(99); heap.display(); //remove root heap.remove(); heap.display(); //add a new element 90 heap.insert(90); heap.display(); }}
欢迎点评
- Heap的Array实现法
- 堆的实现 Heap
- 堆(heap)的实现
- Heap的java实现
- binary heap(array based)
- 关于dijkstra+heap的实现
- 堆(Heap)的实现
- STL的 heap 堆实现
- 关于dijkstra+heap的实现
- 一个简单的Heap实现
- php array的实现
- Build Min Heap Using Array
- 浅析 Win2K 中堆(Heap)的实现
- 浅析 Win2K 中堆(Heap)的实现
- 转 关于dijkstra+heap的实现
- 堆排序(Heap Sort)算法的实现
- 堆(heap)基于数组的实现
- heap实现的最大优先队列
- 关于AbstractQueuedSynchronizer(AQS)的简单理解
- Zookeeper学习笔记(二)通过Java API调用Zookeeper
- php扩展开发
- C初始化字符串指针和二维字符数组
- jQuery--记事
- Heap的Array实现法
- Linux select服务器
- Java通过jersey实现客户端图片上传示例
- 每日MySQL之001:MySQL在SUSE Linux下的安装
- DC- 20 :二叉树
- Silicon Labs的蓝牙MESH解决方案能够为开发者缩短6个月的产品上市时间
- 【剑指offer】面试题50(2):字符流中第一个只出现一次的字符
- 设计模式之适配器模式
- Python学习记录——Python函数及对象