2.2 - Binary Tree
介紹Binary Tree的定義、基本定理及表示方法
可以為空(node=0),若不為空則必須有Root及左右子樹
2.1 在Binary Tree中,第
i level中最多
2i−1 有個nodes
數學歸納法:
for level = 1, 21−1=1個nodes
if level = n-1 holds, show that level = n also holds
for level =n , nodes of n = 2*nodes of n-1 2∗2(i−1)−1=2i−1
2.2 高度
k 的Binary Tree,最多nodes數為
2k−1 ;最少nodes數為
k
20+21+...+2k−1+2k=2−12k−20=2k−1 2.3 在一個非空二元樹,若degree為0的nodes數(leaf)有
n0 ,degree為2的nodes數有
n2 ,則
n0=n2+1
假設: n 為nodes總數 n=n0+n1+n2
n1為degree=1的nodes數
B為branches總數(links) B=0n0+1n1+2n2
n=B+1,⟹n0+n1+n2=1n1+2n2+1⟹n0=n2+1 可分為:left-skewed及right-skewed binary tree
擁有最多節點數的Binary Tree( 2k−1 )
高度為k且擁有n個節點,且滿足 2k−1−1<n≤2k−1
節點編號與高度為k的Full Binary Tree的前n個節點編號一一對應
若complete binary tree的某個節點編號為i,則i的
左子點編號為 2i ,若 2i>n 則無左子點
右子點編號為 2i+1 ,若 2i+1>n 則無右子點
父點編號為 [2i] ,若 [2i]<1 則無父點
Binary Tree中每個non-leaf節點必有兩個子點,即 n1=0
按照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節省空間