Java实现堆
2015-08-24 18:23
204 查看
package com.heap; class Node { private int iData; public int getKey() { return iData; } public void setKey(int iData) { this.iData = iData; } public Node(int iData) { super(); this.iData = iData; } } class Heap { private Node[] heapArray; private int maxSize; private int currentSize; public int getMaxSize(){ return maxSize; } public int getCurrentSize(){ return currentSize; } public Heap(int max) { this.maxSize = max; currentSize = 0; heapArray = new Node[maxSize]; } public boolean isEmpty() { return currentSize == 0; } //插入使用向上筛选, 节点初始时插入到数组的最后第一个空着的单元中,数组容量大小增一 public boolean insert(int key) { if (currentSize == maxSize) { return false; } Node newNode = new Node(key); heapArray[currentSize] = newNode; trickleUp(currentSize++); return true; } //向上筛选,插入时,从下往上查找,直到新插入节点咋一个大于它的节点之下,在一个小于它的节点之上 private void trickleUp(int index) { int parent = (index - 1) / 2; Node bottom = heapArray[index]; while (index > 0 && heapArray[parent].getKey() < bottom.getKey()) { heapArray[index] = heapArray[parent]; index = parent; parent = (parent - 1) / 2; } heapArray[index] = bottom; } //删除每次删除的是根节点 //1.移走根;2.把最后一个节点移动到根的位置;3.一直向下筛选这个节点,直到它在一个大于它的节点之下 //小于它的节点之上为止 public Node remove() { Node root = heapArray[0]; heapArray[0] = heapArray[--currentSize]; trickleDown(0); return root; } //向下筛选, private void trickleDown(int index) { int largeChild; Node top = heapArray[index];//当前节点 while (index < currentSize / 2) { int leftChild = 2 * index + 1; int rightChild = leftChild + 1; if (rightChild < currentSize && heapArray[leftChild].getKey() < heapArray[rightChild] .getKey()) { largeChild = rightChild; } else{ largeChild = leftChild; } if(top.getKey() >= heapArray[largeChild].getKey()) break; heapArray[index] = heapArray[largeChild]; index = largeChild; } heapArray[index] = top; } //修改节点 public boolean change(int index, int newValue){ if(index < 0 || index >= currentSize) return false; int oldValue = heapArray[index].getKey(); heapArray[index].setKey(newValue); if(oldValue < newValue) trickleUp(index); else trickleDown(index); return true; } //打印 public void displayHeap(){ System.out.println("heapArray:"); for(int m = 0; m < currentSize; m++) if(heapArray[m] != null) System.out.print(heapArray[m].getKey() + " "); else System.out.print("--"); System.out.println(); int nBlanks = 32; int itemsPerRow = 1; int column = 0; int j = 0; String dots = "................................"; System.out.println(dots + dots); while(currentSize > 0){ if(column == 0) for(int k = 0; k < nBlanks; k++) System.out.print(' '); System.out.print(heapArray[j].getKey()); if(++j == currentSize) break; if(++column == itemsPerRow){ nBlanks /= 2; itemsPerRow *= 2; column = 0; System.out.println(); } else for(int k = 0; k < nBlanks * 2 - 2; k++) System.out.print(' '); } System.out.println("\n" + dots + dots); } } package com.heap; import java.util.Arrays; public class TestHeap { public static void main(String[] args) { int value, value2; Heap theHeap = new Heap(31); boolean success; theHeap.insert(70); theHeap.insert(40); theHeap.insert(50); theHeap.insert(20); theHeap.insert(60); theHeap.insert(100); theHeap.insert(80); theHeap.insert(30); theHeap.insert(10); theHeap.insert(90); // //显示 theHeap.displayHeap(); // // success = theHeap.insert(55); // System.out.println("success:" + success ); // theHeap.displayHeap(); // // theHeap.remove(); // theHeap.displayHeap(); //--------------------------------堆排序 // int[] arr = {12, 54, 9, 3 ,123, 90,13}; // theHeap = new Heap(7); // for(int i = 0; i < arr.length; i++) // theHeap.insert(arr[i]); // for(int i = 0; i < arr.length; i++) // arr[i] = theHeap.remove().getKey(); // // System.out.println(Arrays.toString(arr)); //-------------------------------- } //堆排序 public static void heapSort(int arr[]){ Heap theHeap = new Heap(7); for(int i = 0; i < arr.length; i++) theHeap.insert(arr[i]); for(int i = 0; i < arr.length; i++) arr[i] = theHeap.remove().getKey(); } }
相关文章推荐
- [转载]浅析Java中的final关键字
- java必看书籍
- eclipse集成jboss及jboss环境配置
- 同一字符串,MD5加密后出现不同的结果
- java多线程详解(5)-Threadlocal用法
- Spring事务配置的五种方式
- webservice示例(java)
- Spring MVC事务配置
- Windows FILETIME与JAVA Date互转
- maven 修改使用jdk版本
- java系统学习(四) --------java字符串的处理
- java系统学习(四) --------java字符串的处理
- java获取本机mac
- Introduction to Java Programming编程题9.3<检验密码>
- spring aop配置实例
- Eclipse Java EE透视图下看不见Referenced Libraries解决办法
- Eclipse 常用快捷键 (动画讲解)
- Java集合中List的Sort()方法进行排序
- javah 错误: 找不到 'XXX' 的类文件。
- Echarts制作报表-Struts2与之交互