dataStructure@ Implementation of minimal heap
2016-06-30 00:09
423 查看
A binary heap is a heap data structure created using a binary tree.
binary tree has two rules -
Binary Heap has to be complete binary tree at all levels except the last level. This is called shape property.
All nodes are either greater than equal to (Max-Heap) or less than equal to (Min-Heap) to each of its child nodes. This is called heap property.
Implementation:
Use array to store the data.
Start storing from index 1, not 0.
For any given node at position i:
Its Left Child is at [2*i] if available.
Its Right Child is at [2*i+1] if available.
Its Parent Node is at [i/2]if available.
min heap
binary tree has two rules -
Binary Heap has to be complete binary tree at all levels except the last level. This is called shape property.
All nodes are either greater than equal to (Max-Heap) or less than equal to (Min-Heap) to each of its child nodes. This is called heap property.
Implementation:
Use array to store the data.
Start storing from index 1, not 0.
For any given node at position i:
Its Left Child is at [2*i] if available.
Its Right Child is at [2*i+1] if available.
Its Parent Node is at [i/2]if available.
package data_structure_testing; import java.util.ArrayList; import java.util.HashSet; public class MinHeap { public ArrayList<Integer> minheap = null; public MinHeap() { this.minheap = new ArrayList<Integer> (); this.minheap.add(Integer.MIN_VALUE); /** * ------------------------------------------------------------------------- * Integer.MIN_VALUE | | | | | | | | | | | | | | | | | | | * ------------------------------------------------------------------------- */ } public void swap(int i, int j) { if(i == j) return; int tmp = minheap.get(i); minheap.set(i, minheap.get(j)); minheap.set(j, tmp); } public int getSize() { return minheap.size(); } public int parent(int i) { return i/2; } public int leftChild(int i) { return i*2; } public int rightChild(int i) { return i*2 + 1; } public void offer(int x) { minheap.add(x); int position = minheap.size() - 1; bubbleUp(position); } public void bubbleUp(int position) { int pa = parent(position); if(pa != 0 && minheap.get(position) < minheap.get(pa)) { swap(position, pa); bubbleUp(pa); } } public int peek() { if(getSize() == 1) { System.out.println("the min heap is empty!"); return minheap.get(0); } else{ return minheap.get(1); } } public void pushDown(int i) { int min_position = i, min_value = minheap.get(i); int lc = leftChild(i), rc = lc + 1; if(lc >= getSize()) { return; } if(lc < getSize()) { if(minheap.get(lc) < min_value) { min_position = lc; min_value = minheap.get(lc); } if(rc < getSize() && minheap.get(rc) < min_value) { min_position = rc; } } swap(min_position, i); if(min_position != i) { pushDown(min_position); } } public int poll() { if(getSize() == 1) { System.out.println("since the min heap is empty, so we cannot poll any element from it!"); return minheap.get(0); } int top = minheap.get(1); swap(1, getSize()-1); minheap.remove(getSize()-1); if(getSize() > 1) { pushDown(1); } return top; } public static void main(String[] args) { MinHeap minheap = new MinHeap(); minheap.offer(5); minheap.offer(4); minheap.offer(2); minheap.offer(4); minheap.offer(3); System.out.println(minheap.poll()); System.out.println(minheap.poll()); System.out.println(minheap.poll()); System.out.println(minheap.poll()); System.out.println(minheap.poll()); System.out.println(minheap.poll()); } }
min heap
相关文章推荐
- tableview的应用
- 网络编程-HttpClient、异步http、Gson、多线程下载
- 字节字符流转换类
- Docker UCP介绍
- iOS中常用的几种设计模式
- MyBatis框架的搭建和基本的写法事列
- 内容提供者
- Android数据持久化存储
- 《剑指offer》二叉树的镜像
- Android开发小问题记录
- android Activity 四大启动模式探究
- 软件项目风险管理(Project Risk Management)
- 广播
- 随便写给phper的docker入门手册
- Floyd最短路算法
- mysql使用出现的问题
- 安全、体验、技术至上:刷脸支付这样一路走向我们身边
- linux解压包
- PHP请求Socket接口测试实例
- Visual Studio 2010 前端开发工具/扩展/插件推荐