Java資料結構和演算法(十四)——堆

來源:互聯網
上載者:User

標籤:find   img   mda   get   otto   least   sem   插入資料   優先   

  在Java資料結構和演算法(五)——隊列中我們介紹了優先順序隊列,優先順序隊列是一種抽象資料類型(ADT),它提供了刪除最大(或最小)關鍵字值的資料項目的方法,插入資料項目的方法,優先順序隊列可以用有序數組來實現,這種實現方式儘管刪除最大資料項目的時間複雜度為O(1),但是插入還是需要較長的時間 O(N),因為每次插入平均需要移動一半的資料項目,來保證插入後,數組依舊有序。

  本篇部落格我們介紹另外一種資料結構——堆,注意這裡的堆和我們Java語言,C++語言等程式設計語言在記憶體中的“堆”是不一樣的,這裡的堆是一種樹,由它實現的優先順序隊列的插入和刪除的時間複雜度都為O(logN),這樣儘管刪除的時間變慢了,但是插入的時間快了很多,當速度非常重要,而且有很多插入操作時,可以選擇用堆來實現優先順序隊列。

1、堆的定義

  ①、它是完全二叉樹,除了樹的最後一層節點不需要是滿的,其它的每一層從左至右都是滿的。注意下面兩種情況,第二種最後一層從左至右中間有斷隔,那麼也是不完全二叉樹。

  

  ②、它通常用數組來實現。

  

  這種用數組實現的二叉樹,假設節點的索引值為index,那麼:

  節點的左子節點是 2*index+1,

  節點的右子節點是 2*index+2,

  節點的父節點是 (index-1)/2。

  ③、堆中的每一個節點的關鍵字都大於(或等於)這個節點的子節點的關鍵字。

  這裡要注意堆和前面說的二叉搜尋樹的區別,二叉搜尋樹中所有節點的左子節點關鍵字都小於右子節點關鍵字,在二叉搜尋樹中通過一個簡單的演算法就可以按序遍曆節點。但是在堆中,按序遍曆節點是很困難的,如所示,堆只有沿著從根節點到葉子節點的每一條路徑是降序排列的,指定節點的左邊節點或者右邊節點,以及上層節點或者下層節點由於不在同一條路徑上,他們的關鍵字可能比指定節點大或者小。所以相對於二叉搜尋樹,堆是弱序的。

2、遍曆和尋找

  前面我們說了,堆是弱序的,所以想要遍曆堆是很困難的,基本上,堆是不支援遍曆的。

  對於尋找,由於堆的特性,在尋找的過程中,沒有足夠的資訊來決定選擇通過節點的兩個子節點中的哪一個來選擇走向下一層,所以也很難在堆中尋找到某個關鍵字。

  因此,堆這種組織似乎非常接近無序,不過,對於快速的移除最大(或最小)節點,也就是根節點,以及能快速插入新的節點,這兩個操作就足夠了。

3、移除

  移除是指刪除關鍵字最大的節點(或最小),也就是根節點。

  根節點在數組中的索引總是0,即maxNode = heapArray[0];

  移除根節點之後,那樹就空了一個根節點,也就是數組有了一個空的資料單元,這個空單元我們必須填上。

  第一種方法:將數組所有資料項目都向前移動一個單元,這比較費時。

  第二種方法:

    ①、移走根

    ②、把最後一個節點移動到根的位置

    ③、一直向下篩選這個節點,直到它在一個大於它的節點之下,小於它的節點之上為止。

  具體步驟如下:

  

  圖a表示把最後一個節點移到根節點,圖b、c、d表示將節點向下篩選到合適的位置,它的合適位置在最底層(有時候可能在中間),圖e表示節點在正確位置的情景。

  注意:向下篩選的時候,將目標節點和其子節點比較,誰大就和誰交換位置。

4、插入

  插入節點也很容易,插入時,選擇向上篩選,節點初始時插入到數組最後第一個空著的單元,數組容量大小增一。然後進行向上篩選的演算法。

  注意:向上篩選和向下不同,向上篩選只用和一個父節點進行比較,比父節點小就停止篩選了。

  

5、完整的Java堆代碼

  首先我們要知道用數組表示堆的一些要點。若數組中節點的索引為x,則:

  節點的左子節點是 2*index+1,

  節點的右子節點是 2*index+2,

  節點的父節點是 (index-1)/2。

  注意:"/" 這個符號,應用於整數的算式時,它執行整除,且得到是是向下取整的值。

package com.ys.tree.heap;public class Heap {private Node[] heapArray;private int maxSize;private int currentSize;public Heap(int mx) {maxSize = mx;currentSize = 0;heapArray = new Node[maxSize];}public boolean isEmpty() {return (currentSize == 0)? true : false;}public boolean isFull() {return (currentSize == maxSize)? true : false;}public boolean insert(int key) {if(isFull()) {return false;}Node newNode = new Node(key);heapArray[currentSize] = newNode;trickleUp(currentSize++);return true;}//向上調整public void trickleUp(int index) {int parent = (index - 1) / 2; //父節點的索引Node bottom = heapArray[index]; //將新加的尾節點存在bottom中while(index > 0 && heapArray[parent].getKey() < bottom.getKey()) {heapArray[index] = heapArray[parent];index = parent;parent = (parent - 1) / 2;}heapArray[index] = bottom;}public Node remove() {Node root = heapArray[0];heapArray[0] = heapArray[--currentSize];trickleDown(0);return root;}//向下調整public void trickleDown(int index) {Node top = heapArray[index];int largeChildIndex;while(index < currentSize/2) { //while node has at least one childint leftChildIndex = 2 * index + 1;int rightChildIndex = leftChildIndex + 1;//find larger childif(rightChildIndex < currentSize &&  //rightChild exists?heapArray[leftChildIndex].getKey() < heapArray[rightChildIndex].getKey()) {largeChildIndex = rightChildIndex;}else {largeChildIndex = leftChildIndex;}if(top.getKey() >= heapArray[largeChildIndex].getKey()) {break;}heapArray[index] = heapArray[largeChildIndex];index = largeChildIndex;}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(array format): ");for(int i = 0; i < currentSize; i++) {if(heapArray[i] != null) {System.out.print(heapArray[i].getKey() + " ");}else {System.out.print("--");}}}}class Node {private int iData;public Node(int key) {iData = key;}public int getKey() {return iData;}public void setKey(int key) {iData = key;}}

  

 

Java資料結構和演算法(十四)——堆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.