Data Structure
  • 資料結構自學筆記
  • 1 - Stack & Queue
    • 1.1 - Stack
    • 1.2 - Queue
    • 1.3 - Stack and Queue
  • 2 - Tree & Binary Tree
    • 2.1 - Tree
    • 2.2 - Binary Tree
    • 2.3 - Binary Tree Traversal
    • 2.4 - Binary Search Tree
    • 2.5 - Heap
    • 2.6 - Thread Binary Tree
    • 2.7 - Tree and Binary Tree Conversion
    • 2.8 Advanced Trees
      • 2.8.1 - Min-Max Heap
      • 2.8.2 - Deap
      • 2.8.3 - Symmetric Min-Max Heap
      • 2.8.4 - Extended Binary Tree
      • 2.8.5 - AVL Tree
      • 2.8.6 - M-Way Search Tree
      • 2.8.7 - B Tree
      • 2.8.8 - Red-Black Tree
      • 2.8.9 - Optimal Binary Search Tree
      • 2.8.10 - Splay Tree
      • 2.8.11 - Leftest Heap
      • 2.8.12 - Binomial Heap
  • 3 - Search & Sort
    • 3.1 - Searching
    • 3.2 - Elementary Sorting
      • 3.2.1 - Insertion Sort
      • 3.2.2 - Selection Sort
      • 3.2.3 - Bubble Sort
      • 3.2.4 - Shell Sort
    • 3.3 - Sophisticated Sorting
      • 3.3.1 - Quick Sort
      • 3.3.2 - Merge Sort
      • 3.3.3 - Heap Sort
      • 3.3.4 - Radix Sort
      • 3.3.5 - Bucket Sort
      • 3.3.6 - Counting Sort
    • 3.4 - Summary
  • 4 - Graph
    • 4.1 - Intro
    • 4.2 - Graph Traversal
    • 4.3 - Spanning Tree
      • 4.3.1 - Kruskal's algorithm
      • 4.3.2 - Prim's algorithm
      • 4.3.3 - Sollin's algorithm
    • 4.4 - Shortest Path Length
      • 4.4.1 - Dijkstra's algorithm
      • 4.4.2 - Bellman-Ford algorithm
      • 4.4.3 - Floyd-Warshall algorithm
    • 4.5 - AOV Network
    • 4.6 - AOE Network
    • 4.7 - Others
Powered by GitBook
On this page
  • 1. 定義
  • 2. 運作
  • 3. 應用
  1. 2 - Tree & Binary Tree

2.5 - Heap

介紹heap、運作及其應用

1. 定義

  1. 可以分為max-heap與min-heap(以max-heap為例)

  2. 本身是complete binary tree(適合用array存放)

  3. 所有節點的parent值都大於其children

  4. root具有最大值

      40
     /  \
    20   10
   /  \
  5    9

2. 運作

2.1 Insert x in heap

  1. 將x存放於最後一個節點的下一個位置

  2. 往上和父點比較,若父點叫小則交換,直到root。

2.2 Delete max in heap

  1. 取出root的最大值

  2. 將最後一個節點刪除,並放到root中

  3. 從root往下調整,直到root為最大值

2.3 Build a Heap(Top-Down)

從上到下使用insert()來建立Heap。

Time:

2.4 Build a Heap(Bottom-Up)

  1. 先將資料以Complete Binary Tree呈現

  2. 從最後一個父點開始調整成Heap,直到Root

Time:

Time = O(n)

void AdjustBottomUp(char *heap, int i, int n){
    int j = 2*i+1;     //左子點
    while(j <= n){
        if (j < n)
            if (heap[j] < heap[j+1])
                Swap(&heap[j], &heap[j+1]); 左右子點交換
        if (heap[i] > heap[j]) 
            break;
        else{
            Swap(&heap[j/2], &heap[j]); //父點與子點交換
            j=j*2+1; 
            i=(j-1)/2;
        }
    }
}

void BuildHeap(char *heap){
    int n = strlen(heap);
    for (int i = n/2-1; i>=0; i--){ //i從最後一個父點開始
        AdjustBottomUp(heap, i, n-1);
    }    
}

void Swap(char *a, char *b){
    char temp = *a;
    *a = *b;
    *b = temp;
}

3. 應用

  1. 製作priority queue的最佳資料結構,因為insert(),delete_max()皆只需O(logn)。

  2. Heap Sort

Previous2.4 - Binary Search TreeNext2.6 - Thread Binary Tree

Last updated 6 years ago

∑i=1Nlog(i)=log(n!)≃O(nlogn)\sum_{i=1}^{N} log(i) = log(n!)\simeq O(nlogn)∑i=1N​log(i)=log(n!)≃O(nlogn)

假設height= KKK ,節點總數= nnn ,某一節點位於第i階(level)

則調整節點最大成本為 K−iK-iK−i ,level i最多有 2i−12^{i-1}2i−1 個節點

總成本: ∑i=1K(k−i)2i−1=∑i=1K−1(k−i)2i−1=S\sum_{i=1}^{K}(k-i)2^{i-1} = \sum_{i=1}^{K-1}(k-i)2^{i-1}=S∑i=1K​(k−i)2i−1=∑i=1K−1​(k−i)2i−1=S

S=(k−1)20+(k−2)21+...+2k−22S=+(k−1)21+....+2∗2k−2+2k−12S−S=−(k−1)20+21+22+23+...+2k−1S = (k-1)2^0+(k-2)2^1+...+2^{k-2}\\2S = \quad\quad\quad\quad+(k-1)2^1+....+2*2^{k-2}+2^{k-1}\\2S-S=-(k-1)2^0+2^1+2^2+2^3+...+2^{k-1}S=(k−1)20+(k−2)21+...+2k−22S=+(k−1)21+....+2∗2k−2+2k−12S−S=−(k−1)20+21+22+23+...+2k−1 S=2k−212−1−(k−1)=2k−k−1(k=log2(n+1))\\S=\frac{2^k-2^1}{2-1}-(k-1)\\\quad=2^k-k-1\quad(k=log_{2}(n+1))S=2−12k−21​−(k−1)=2k−k−1(k=log2​(n+1))

S=(n+1)−logn(n+1)−1S=(n+1)-log_{n}(n+1)-1S=(n+1)−logn​(n+1)−1