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. 種類
  • 4. 表示方法
  1. 2 - Tree & Binary Tree

2.2 - Binary Tree

介紹Binary Tree的定義、基本定理及表示方法

Previous2.1 - TreeNext2.3 - Binary Tree Traversal

Last updated 6 years ago

1. 定義

  1. 可以為空(node=0),若不為空則必須有Root及左右子樹

  2. 左右子樹必須為Binary Tree

  3. 左右子樹有方向之別

2. 基本定理及證明

2.1 在Binary Tree中,第 iii level中最多 2i−12^{i-1}2i−1 有個nodes

數學歸納法:

  1. for level = 1, 21−1=12^{1-1}=121−1=1個nodes

  2. if level = n-1 holds, show that level = n also holds

  3. for level =n , nodes of n = 2*nodes of n-1 2∗2(i−1)−1=2i−12*2^{(i-1)-1} = 2^{i-1}2∗2(i−1)−1=2i−1

2.2 高度 kkk 的Binary Tree,最多nodes數為 2k−12^k-12k−1 ;最少nodes數為 kkk

20+21+...+2k−1+2k=2k−202−1=2k−12^{0}+2^{1}+...+2^{k-1}+2^{k} = \frac{2^{k}-2^{0}}{2-1}=2^{k}-120+21+...+2k−1+2k=2−12k−20​=2k−1

2.3 在一個非空二元樹,若degree為0的nodes數(leaf)有 n0n_{0}n0​ ,degree為2的nodes數有 n2n_{2}n2​ ,則 n0=n2+1n_{0}=n_{2}+1n0​=n2​+1

3. 種類

  • Skewed Binary Tree

可分為:left-skewed及right-skewed binary tree

left                       right
    O                      O
   /                        \
  O                          O
 /                            \
O                              O
  • Full Binary Tree

  • Complete Binary Tree

  1. 節點編號與高度為k的Full Binary Tree的前n個節點編號一一對應

即:
         1
        / \
       2   3
      / \ / \
     4  5 6  7
    / \ /
   8  910

若complete binary tree的某個節點編號為i,則i的

  • Strict Binary Tree

4. 表示方法

4.1 使用Array儲存

按照full binary tree的節點編號儲存到對應的矩陣indice中

  • 優點:

    • 容易存取左右子點及父點

    • 對於full/complete binary tree沒有儲存空間浪費

  • 缺點:

    • 節點增減較麻煩

    • 對於skewed binary tree非常浪費空間

4.2 使用Linklist儲存

[point to left child][data][point to right child]

  • 優點:

    • 節點增減容易

    • 對於skewed binary tree相較於array節省空間

  • 缺點:

    • 不易存取父點

    • links空間浪費約50%

假設: nnn 為nodes總數 n=n0+n1+n2n = n_{0}+n_{1}+n_{2}n=n0​+n1​+n2​

n1n_{1}n1​為degree=1的nodes數

BBB為branches總數(links) B=0n0+1n1+2n2B = 0n_{0}+1n_{1}+2n_{2}B=0n0​+1n1​+2n2​

n=B+1,  ⟹  n0+n1+n2=1n1+2n2+1  ⟹  n0=n2+1n =B+1,\newline \implies n_{0}+n_{1}+n_{2}=1n_{1}+2n_{2}+1\newline \implies n_{0} = n_{2}+1n=B+1,⟹n0​+n1​+n2​=1n1​+2n2​+1⟹n0​=n2​+1

擁有最多節點數的Binary Tree( 2k−12^{k}-12k−1 )

高度為k且擁有n個節點,且滿足 2k−1−1<n≤2k−12^{k-1}-1<n≤2^{k}-12k−1−1<n≤2k−1

左子點編號為 2i2i2i ,若 2i>n2i>n2i>n 則無左子點

右子點編號為 2i+12i+12i+1 ,若 2i+1>n2i+1>n2i+1>n 則無右子點

父點編號為 [i2][\frac{i}{2}][2i​] ,若 [i2]<1[\frac{i}{2}]<1[2i​]<1 則無父點

Binary Tree中每個non-leaf節點必有兩個子點,即 n1=0n_{1}=0n1​=0