Tree and Binary Tree 樹與二元樹
Oct 12, 2023#Data Structure
紀錄一些學習 Tree 的筆記。包括:
- Tree、Binary Tree 的定義
- 用何種資料結構來實作 Tree、Binary Tree
- Binary Tree 相關的定理。
🐳 Tree 樹
🦀 定義
- 由至少一個 node 組成,不得為空。
- 至少有一個特定 node 稱為 root。
- 其餘 nodes 分為
~ 個集合,稱為 subtree of root。
以下圖的 tree 為例說明用來描述 tree 的名詞:
graph TB
A((A))-->B((B))
A-->C((C))
A-->D((D))
B-->E((E))
B-->F((F))
B-->G((G))
C-->H((H))
D-->I((I))
D-->J((J))
- node’s degree:一個 node 擁有的 subtree(子樹)的個數。例如:A 的 degree 是 3,D 的 degree 是 2,J 的 degree 是 0。
- root:樹中最上層的 node,也是唯一一個 parent 為 null 的 node。例如:A 是這個 tree 的 root。
- leaf:沒有 child/subtree 的 node 稱為 leaf。例如:E、F、G、H、I、J 都是 leaf。
- non-leaf:不是 leaf 的 node 稱為 non-leaf。例如:A、B、C、D 都是 non-leaf。
- parent and child:以箭頭的指向來區分,被指向者為 child,指向者為 parent,例如:A 是 BCD 的 parent,BCD 是 A 的 children。
- sibling:擁有相同 parent 的 node 們,互相稱為 sibling,例如:A 是 BCD 的 parent,所以 BCD 是 siblings。
- ancestors:如圖中,以 F 的角度觀察,所有能夠以「指向 parent」的方式找到的 node,都稱為 F 的 ancestors,因此 AB 為 F 的 ancestors。
- descendants:如圖中,以 A 的角度觀察,所有能夠以「parent 指向 child」的方式找到的 node,都稱為 A 的 descendant,因此整棵樹除了 A 以外皆為 A 的 descendant,以此類推。
- node’s level:root 的 level 為 0 或 1,其他 node 的 level 為其 parent 的 level 加一。
- height:樹的高度,也就是 node’s level 取最大值。
- tree’s degree:所有 node’s degree 取最大值。
- forest:由 tree 所形成的集合,可以為空。
flowchart TB
subgraph forest
direction TB
A((A))-->B((B))
A-->C((C))
B-->E((E))
B-->F((F))
B-->G((G))
C-->H((H))
D((D))-->I((I))
D-->J((J))
end
🦀 Representations 樹的表示方法
use linked list to represent tree directly
用 linked list 來表示 tree,假設

以上面的 node structure 來表示下圖的 tree:
flowchart TB
A((A)) --> B((B))
A --> C((C))
A --> D((D))
C --> E((E))
C --> F((F))
會變成:

這個方法極度浪費記憶體空間。
child-sibling
還是用 linked list 來表示 tree,但是換一個角度來重新設計 node structure,來解決前一個方法浪費記憶體空間的問題。
來重新設計 node structure 成如下圖的結構:

- child:pointer,指向 「left-most」 child
- sibling:pointer,指向 node 右邊的 sibling
現在以上面重新設計的 node structure 來表示下圖的 tree:
flowchart TB
A((A)) --> B((B))
A --> C((C))
A --> D((D))
C --> E((E))
C --> F((F))
會變成:

括號法
用 父點(子點...子點) 表示父與子點之間的組成關係,可以巢狀表示。
舉個例子,以括號法來表示下圖的 tree:
flowchart TB
A((A)) --> B((B))
A --> C((C))
A --> D((D))
B --> E((E))
B --> F((F))
C --> G((G))
D --> H((H))
D --> I((I))
D --> J((J))
可以寫成:A(B(EF)C(G)D(HIJ))
🐳 Binary Tree 二元樹
🦀 定義
由至少一個 node 組成,可以為空,若不為空,則滿足:
- 有 root 及左右子樹
- 左右子樹也是 binary tree
🦀 Binary Tree 的三個定理
假設:root level = 1,【定理一】:
The
level in a binary tree has at most nodes.
證明:用數學歸納法
假設:root level = 1,【定理二】:
The binary tree with height
, has at most nodes, at least nodes.
證明:
假設:root level = 1,【定理三】:
For a non-empty binary tree, if there are
leaves and degree-2 nodes, then .
證明:
🦀 Binary Tree 的種類
| 種類 | 定義 |
|---|---|
| Skewed Binary Tree |
|
| Full Binary Tree |
|
| Complete Binary Tree |
|
| Strict Binary Tree |
|
🦀 Binary Tree Representations
有兩種表示二元樹的方法,分別是用 array 或是 linked list。下面表格是針對這兩種方式的優缺點比較:
| Array | Linked List | |
|---|---|---|
| 優點 |
|
|
| 缺點 |
|
|
🐳 Resource
- Tree(樹): Intro(簡介) by Chiu CC