java最小根堆实现

1.堆结点 

Java代码   收藏代码
  1. package boke.heap1;  
  2.   
  3. /** 
  4.  * 堆结点 
  5.  *  
  6.  * @since jdk1.5及其以上 
  7.  * @author 毛正吉 
  8.  * @version 1.0 
  9.  * @date 2010.05.24 
  10.  *  
  11.  */  
  12. public class Node {  
  13.     private int iData; // 结点数据是整型  
  14.   
  15.     public Node(int key) {  
  16.         iData = key;  
  17.     }  
  18.   
  19.     /** 
  20.      * setKey 
  21.      *  
  22.      * @param id 
  23.      */  
  24.     public void setKey(int id) {  
  25.         iData = id;  
  26.     }  
  27.   
  28.     /** 
  29.      * getKey 
  30.      *  
  31.      * @return 
  32.      */  
  33.     public int getKey() {  
  34.         return iData;  
  35.     }  
  36. }  

2. 最小堆 

Java代码   收藏代码
  1. package boke.heap1;  
  2.   
  3. import boke.heap.Node;  
  4.   
  5. /** 
  6.  * 最小堆 
  7.  *  
  8.  * @since jdk1.5及其以上 
  9.  * @author 毛正吉 
  10.  * @version 1.0 
  11.  * @date 2010.05.24 
  12.  *  
  13.  */  
  14. public class MinHeap {  
  15.     private Node[] heapArray; // 堆容器  
  16.     private int maxSize; // 堆得最大大小  
  17.     private int currentSize; // 堆大小  
  18.   
  19.     public MinHeap(int _maxSize) {  
  20.         maxSize = _maxSize;  
  21.         heapArray = new Node[maxSize];  
  22.         currentSize = 0;  
  23.     }  
  24.   
  25.     /** 
  26.      * 自上而下调整 
  27.      *  
  28.      * @param start 
  29.      * @param endOfHeap 
  30.      */  
  31.     public void filterDown(int start, int endOfHeap) {  
  32.         int i = start;  
  33.         int j = 2 * i + 1// j是i的左子女位置  
  34.         Node temp = heapArray[i];  
  35.   
  36.         while (j <= endOfHeap) { // 检查是否到最后位置  
  37.             if (j < endOfHeap // 让j指向两子女中的小者  
  38.                     && heapArray[j].getKey() > heapArray[j + 1].getKey()) {  
  39.                 j++;  
  40.             }  
  41.             if (temp.getKey() <= heapArray[j].getKey()) { // 小则不做调整  
  42.                 break;  
  43.             } else { // 否则小者上移,i,j下降  
  44.                 heapArray[i] = heapArray[j];  
  45.                 i = j;  
  46.                 j = 2 * j + 1;  
  47.             }  
  48.         }  
  49.         heapArray[i] = temp;  
  50.     }  
  51.   
  52.     /** 
  53.      * 自下而上的调整:从结点start开始到0为止,自下向上比较,如果子女的值小于双亲结点的值则互相交换 
  54.      *  
  55.      * @param start 
  56.      */  
  57.     public void filterUp(int start) {  
  58.         int j = start;  
  59.         int i = (j - 1) / 2;  
  60.         Node temp = heapArray[j];  
  61.   
  62.         while (j > 0) { // 沿双亲结点路径向上直达根节点  
  63.             if (heapArray[i].getKey() <= temp.getKey()) {// 双亲结点值小,不调整  
  64.                 break;  
  65.             } else {// 双亲结点值大,调整  
  66.                 heapArray[j] = heapArray[i];  
  67.                 j = i;  
  68.                 i = (i - 1) / 2;  
  69.             }  
  70.             heapArray[j] = temp; // 回送  
  71.         }  
  72.     }  
  73.   
  74.     /** 
  75.      * 堆中插入结点 
  76.      *  
  77.      * @param key 
  78.      * @return 
  79.      * @throws MinHeapException 
  80.      */  
  81.     public boolean insert(int key) throws MinHeapException {  
  82.         boolean bool = true;  
  83.         if (isFull()) {  
  84.             bool = false;  
  85.             throw new MinHeapException("MinHeap is full!");  
  86.         } else {  
  87.             Node newNode = new Node(key);  
  88.             heapArray[currentSize] = newNode;  
  89.             filterUp(currentSize);  
  90.             currentSize++;  
  91.         }  
  92.         return bool;  
  93.     }  
  94.   
  95.     /** 
  96.      * 删除堆中的最小值 
  97.      *  
  98.      * @return 
  99.      * @throws MinHeapException 
  100.      */  
  101.     public Node removeMin() throws MinHeapException {  
  102.         if (isEmpty()) {  
  103.             throw new MinHeapException("MinHeap is empty!");  
  104.         }  
  105.         Node root = heapArray[0];  
  106.         heapArray[0] = heapArray[currentSize - 1];  
  107.         currentSize--;  
  108.         filterDown(0, currentSize - 1);  
  109.         return root;  
  110.     }  
  111.   
  112.     /** 
  113.      * 按某种格式输出堆 
  114.      */  
  115.     public void displayHeap() {  
  116.         System.out.print("heapArray: ");  
  117.         for (int i = 0; i < currentSize; i++) {  
  118.             if (heapArray[i] != null) {  
  119.                 System.out.print(heapArray[i].getKey() + " ");  
  120.             } else {  
  121.                 System.out.print("-- ");  
  122.             }  
  123.         }  
  124.         System.out.println();  
  125.   
  126.         int nBlanks = 32// heap format  
  127.         int itemsPerRow = 1;  
  128.         int column = 0;  
  129.         int j = 0// current item  
  130.         String dots = "...............................";  
  131.         System.out.println(dots + dots); // dotted top line  
  132.   
  133.         while (currentSize > 0) { // for each heap item  
  134.             if (column == 0) { // first item in row  
  135.                 for (int k = 0; k < nBlanks; k++) { // preceding blanks  
  136.                     System.out.print(" ");  
  137.                 }  
  138.             }  
  139.             System.out.print(heapArray[j].getKey()); // display item  
  140.   
  141.             if (++j == currentSize) { // done?  
  142.                 break;  
  143.             }  
  144.   
  145.             if (++column == itemsPerRow) { // end of row?  
  146.                 nBlanks /= 2// half the blanks  
  147.                 itemsPerRow *= 2// twice the items  
  148.                 column = 0// start over on  
  149.                 System.out.println(); // next row  
  150.             } else { // next item on row  
  151.                 for (int k = 0; k < nBlanks * 2 - 2; k++) {  
  152.                     System.out.print(' '); // interim blanks  
  153.                 }  
  154.             }  
  155.         }  
  156.         System.out.println("\n" + dots + dots);  
  157.     }  
  158.   
  159.     public boolean isEmpty() {  
  160.         return currentSize == 0;  
  161.     }  
  162.   
  163.     public boolean isFull() {  
  164.         return currentSize == maxSize;  
  165.     }  
  166.   
  167.     public void makeEmpty() {  
  168.         currentSize = 0;  
  169.     }  
  170. }  

3. 堆异常 

Java代码   收藏代码
  1. package boke.heap1;  
  2.   
  3. /** 
  4.  * 堆异常 
  5.  *  
  6.  * @since jdk1.5及其以上 
  7.  * @author 毛正吉 
  8.  * @version 1.0 
  9.  * @date 2010.05.24 
  10.  *  
  11.  */  
  12. public class MinHeapException extends Exception {  
  13.     public MinHeapException() {  
  14.         super("MinHeapException");  
  15.     }  
  16.   
  17.     public MinHeapException(String exMsg) {  
  18.         super(exMsg);  
  19.     }  
  20. }  

4.  最小堆应用测试 

Java代码   收藏代码
  1. package boke.heap1;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7. /** 
  8.  * 最小堆应用测试 
  9.  *  
  10.  * @since jdk1.5及其以上 
  11.  * @author 毛正吉 
  12.  * @version 1.0 
  13.  * @date 2010.05.24 
  14.  *  
  15.  */  
  16. public class MinHeapApp {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      * @throws IOException 
  21.      * @throws MinHeapException 
  22.      */  
  23.     public static void main(String[] args) throws IOException, MinHeapException {  
  24.         int value, value2;  
  25.         MinHeap hp = new MinHeap(31);  
  26.         boolean success;  
  27.   
  28.         hp.insert(53);  
  29.         hp.insert(17);  
  30.         hp.insert(78);  
  31.         hp.insert(9);  
  32.         hp.insert(45);  
  33.         hp.insert(65);  
  34.         hp.insert(87);  
  35.         hp.insert(23);  
  36.   
  37.         while (true) {  
  38.             System.out.print("Enter first letter of ");  
  39.             System.out.print("show, insert, remove: ");  
  40.             int choice = getChar();  
  41.   
  42.             switch (choice) {  
  43.             case 's':  
  44.                 hp.displayHeap();  
  45.                 break;  
  46.             case 'i':  
  47.                 System.out.print("Enter value to insert: ");  
  48.                 value = getInt();  
  49.                 success = hp.insert(value);  
  50.                 if (!success) {  
  51.                     System.out.println("Can't insert; heap is full");  
  52.                 }  
  53.                 break;  
  54.             case 'r':  
  55.                 if (!hp.isEmpty()) {  
  56.                     hp.removeMin();  
  57.                 } else {  
  58.                     System.out.println("Can't remove; heap is empty");  
  59.                 }  
  60.                 break;  
  61.             default:  
  62.                 System.out.println("Invalid entry\n");  
  63.             }  
  64.         }  
  65.   
  66.     }  
  67.   
  68.     /** 
  69.      * 获得控制台输入流 
  70.      *  
  71.      * @return 
  72.      * @throws IOException 
  73.      */  
  74.     public static String getString() throws IOException {  
  75.         return new BufferedReader(new InputStreamReader(System.in)).readLine();  
  76.     }  
  77.   
  78.     /** 
  79.      * 获得控制台输入字符 
  80.      *  
  81.      * @return 
  82.      * @throws IOException 
  83.      */  
  84.     public static char getChar() throws IOException {  
  85.         return getString().charAt(0);  
  86.     }  
  87.   
  88.     /** 
  89.      * 获得控制台输入整型 
  90.      *  
  91.      * @return 
  92.      * @throws NumberFormatException 
  93.      * @throws IOException 
  94.      */  
  95.     public static int getInt() throws NumberFormatException, IOException {  
  96.         return Integer.parseInt(getString());  
  97.     }  
  98.   
  99. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值