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. Theorem
  • 3. Weighted External Path Length
  • 4. 應用
  1. 2 - Tree & Binary Tree
  2. 2.8 Advanced Trees

2.8.4 - Extended Binary Tree

Previous2.8.3 - Symmetric Min-Max HeapNext2.8.5 - AVL Tree

Last updated 6 years ago

1. 介紹

具有n個節點的二元樹,若以Link List表示,則會有n+1條NULLIinks,在這些links加上特殊節點External Nodes,其餘節點稱為Internal Nodes。

圖示:

2. Theorem

使用數學歸納法證明:

代入2.假設:

3. Weighted External Path Length

使用Huffman演算法求出最小Weighted External Path Length

  1. 自加權值集合中取出兩個最小的權值

  2. 此兩權值和他們的和建立延伸二元樹

  3. 將兩個加權值的和重新加入到集合中

  4. 重複1~3直到集合中只剩一個值

4. 應用

  • Encoding/Decoding Tree

  • 每個message的編碼內容

  1. m1=1010

  2. m2=1011

  3. m3=100

  4. m4=00

  5. m5=01

  6. m6=11

Optimal Prefix Code: 當對方收到訊息後,其解碼是唯一

Internal Path Length=∑i=1nL(Root to Internal Nodei)Internal\ Path\ Length=\sum_{i=1}^{n}L(Root \ to\ Internal\ Node_{i})Internal Path Length=∑i=1n​L(Root to Internal Nodei​)

External Path Length=∑j=0nL(Root to External Nodej)External\ Path\ Length=\sum_{j=0}^{n}L(Root \ to\ External\ Node_{j})External Path Length=∑j=0n​L(Root to External Nodej​)

E=I+2NE=I+2NE=I+2N

假設左子樹內部節點數nLn_{L}nL​、內部路徑長 ILI_{L}IL​ 、外部路徑長 ELE_{L}EL​;右子樹內部節點數nRn_{R}nR​、內部路徑長 IRI_{R}IR​ 、外部路徑長 ERE_{R}ER​。

當內部節點數N=0N=0N=0 ,此樹為空樹E=I=0E=I=0E=I=0 ,所以 E=I+2NE=I+2NE=I+2N 成立。

假設內部節點數為 N−1N-1N−1 時,此定理成立。

左子樹滿足 EL=IL+2nLE_{L}=I_{L}+2n_{L}EL​=IL​+2nL​

右子樹滿足 ER=IR+2nRE_{R}=I_{R}+2n_{R}ER​=IR​+2nR​

當內部節點數為 NNN時,

整棵樹的 III、 EEE 分別為:

I=IL+IR+nL+nRE=EL+ER+(nL+1)+(nR+1)I = I_{L}+I_{R}+n_{L}+n_{R}\\E=E_{L}+E_{R}+(n_{L}+1)+(n_{R}+1)I=IL​+IR​+nL​+nR​E=EL​+ER​+(nL​+1)+(nR​+1)

E=EL+ER+(nL+1)+(nR+1)=(IL+2nL)+(IR+2nR)+(nL+1)+(nR+1)=(IL+IR+nL+nR)+2(nL+nR+1)=I+2NE=E_{L}+E_{R}+(n_{L}+1)+(n_{R}+1)\\=(I_{L}+2n_{L})+(I_{R}+2n_{R})+(n_{L}+1)+(n_{R}+1)\\=(I_{L}+I_{R}+n_{L}+n_{R})+2(n_{L}+n_{R}+1)\\ =I+2NE=EL​+ER​+(nL​+1)+(nR​+1)=(IL​+2nL​)+(IR​+2nR​)+(nL​+1)+(nR​+1)=(IL​+IR​+nL​+nR​)+2(nL​+nR​+1)=I+2N

W.E.P.L=∑i=0nEi∗qiW.E.P.L=\sum_{i=0}^{n}E_{i}*q_{i}W.E.P.L=∑i=0n​Ei​∗qi​ ( qiq_{i}qi​ 為外部節點加權值)

圖示範例: W={2, 3, 5, 7, 9, 13}W=\{2,\ 3,\ 5,\ 7,\ 9,\ 13\}W={2, 3, 5, 7, 9, 13}

min WEPL=2∗7+2∗9+2∗13+3∗5+4∗2+4∗3=93min\ WEPL=2*7+2*9+2*13+3*5+4*2+4*3=93min WEPL=2∗7+2∗9+2∗13+3∗5+4∗2+4∗3=93

有6種不同的messages(m1~m6),其出現頻率分別為 m1=239, m2=339, m3=539, m4=739, m5=939, m6=1339m_{1}=\frac{2}{39},\ m_{2}=\frac{3}{39},\ m_{3}=\frac{5}{39},\ m_{4}=\frac{7}{39},\ m_{5}=\frac{9}{39},\ m_{6}=\frac{13}{39}m1​=392​, m2​=393​, m3​=395​, m4​=397​, m5​=399​, m6​=3913​ ,若希望平均解碼時間最小,則該如何編碼?

平均解碼時間= ∑i=1n(解一個mi的時間)∗(mi出現頻率)\sum_{i=1}^{n}(解一個m_{i}的時間)*(m_{i}出現頻率)∑i=1n​(解一個mi​的時間)∗(mi​出現頻率)

平均編碼位元長度= ∑i=1n(mi位元長度)∗(mi出現頻率)\sum_{i=1}^{n}(m_{i}位元長度)*(m_{i}出現頻率)∑i=1n​(mi​位元長度)∗(mi​出現頻率)

1
2
3
4
5