樹
Michael Tsai 2010/10/22
三周後要期中考拉
感謝同學們在加分題建議 .
我會好好研讀 + 反省 ~
今日菜單
•
討論考試形式•
樹•
普通樹•
二元樹•
爬樹•
堆 ( 啥東東 )期中考 !!
•
我的想法 :•
關書•
A4 大小一張 , 雙面 , 抄到你開心為止 ( 期末考沿用 )•
題目都是問答題 ( 寫 algorithm, 證明題 , 問 complexit y)•
請把答案寫清楚 , 部分正確就有部分給分•
請發表意見 .•
作弊的直接砍頭 ( 當掉 + 送學校議處 )樹
http://www.ahneninfo.com/de/ahnentafel.htm
樹的定義
•
Definition: A tree is a finite set of one or more n odes such that•
(1) There is a specially designated node called the root.•
(2) The remaining nodes are partitioned into disjo int sets , where each of these sets is a tree.•
(3) are called the subtrees of the root.•
注意以上為遞迴定義•
一個 node 沒有子樹的話 , 是不是樹 ?•
沒有 node 是不是樹 ?•
右邊的是不是樹 ?•
違反了什麼規則 ?•
樹的字典
•
Root•
Node•
Degree•
Leaf•
Terminal nodes•
Children•
Siblings•
Degree of a tree•
Ancestors•
Level•
Height or DepthA
B C D
E F G H I J
K L M
怎麼在記憶體裡面記一棵樹呢 ?
A
B C … ㄅ
1 2 k
…
•
這樣有什麼壞處 ?•
算算看浪費了多少空間•
假設 degree of tree = k, 總共有 n 個 nodes•
有多少個 child 欄位是空的 ?•
總共有個欄位•
但是整棵樹有幾個 branch? 個•
Data Child 1 Child 2 Child 3 … Child k
左小孩 - 右兄弟姊妹 表示法
•
Left child-right sibling representation•
觀察 :•
1. 每個 node 最多只有一個最左邊的 child ( 是廢話 )•
2. 每個 node 也最多只有一個最靠近他的右邊的 sibling ( 也是廢話 )Data left child Right sibling
來畫一下 怎麼用 LC-RS 表示這棵樹
?
A
B C D
E F G H I J
K L M
A
B C D
E F G H I J
K L M
左小孩 - 右兄弟姊妹 樹
•
可以變成 degree-two tree•
也就是說 , 是一種把普 通的樹轉成 degree-two 樹的方法•
Root 沒有右邊的 child ( 也就是說原本的 LC-RS 樹裡面 root 不會有兄弟 姊妹 - 廢話 )A
B C D
E F G H I J
K L M
A B
C
D E
F
G
H I M J
K
L
Binary Tree
•
Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and t he right subtree.•
注意 : 可以是沒有 node•
比較 : 一般 tree 不可以沒有 node•
注意 : children 在左邊或右邊是不一樣的 ( 有順序 )•
比較 : 一般 tree 的 children 順序沒有差一些證明
•
1. 在 level i 的 node 數目最多為•
證明 : 用歸納法•
i=1 時 , 為 root 那一層 , 所以只有一個 node, 也就是最 多有個 node. ( 成立 )•
假設 i=k-1 的時候成立 level k-1 最多有個 node•
那麼 i=k 的時候 , 最多有幾個 node?•
因為是 binary tree, 所以每個 node 最多有兩個 children•
因此最多有 node ( 得證 )•
兩些證明 ( 誤 )
•
2. 一棵 depth 為 k 的 binary tree, 最多有個 node, .•
證明 :•
利用 1 的結果•
則總共 node 數目最多為•
. 喔耶 .•
一些證明 part 3
• 3. 對於任何不是空的 binary tree, 假設為 leaf node 數 目 , 為 degree 2 的 node 數目 , 則 .
• 證明 :
• 假設 n 為所有 node 樹木 , 為 degree 1 的 node 數目 ,
• 則 . (1)
• 假設 B 為 branch 的數目 , 則 .(2)
• 而且 (3). ( 只有 root 沒有往上連到 parent 的 branch, 其 他的 node 正好每個人一個 )
• (4)
• (4) 減 (1) 得 . 喔耶 .
•
Full binary tree
•
Definition: a full binary tree of depth k is a bina ry tree of depth k having nodes, .•
也就是說 depth k 的樹裡面最多 node 樹木的 ( 滿了 )•
1
2 3
4 5 6 7
depth=3 的 full binary tree
Complete binary tree
•
Definition: A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes n umbered from 1 to n in the full binary tree of dept h k.1
2 3
4 5
1
2 3
4 5 6
1 2
1
2 3
5 6
Yes Yes Yes No
1 2
4 5
No
Complete binary tree 的高度
•
如果一個 complete binary tree 有 n 個 node, 那麼樹的 高度為 ?•
Hint: 高度為 k 的 full binary tree 有個 node•
答 :•
如何在記憶體裡面表示 binary tree?
•
方法一•
提示 : 12 3
4 5 6 7
對應到 :
[0] [1] [2] [3] [4] [5] [6] [7]
舉例 :
A
B D
C E
空 A B D 空 C E 空
[0] [1] [2] [3] [4] [5] [6] [7]
壞處是什麼 ?
最糟的狀況浪費了多少空間 ?
“skewed binary tree” 歪斜 binary tree
規則們
•
如果有一個 node 在 array 的 index 是 I, parent(i)=? (i ndex)•
答案 :•
leftChild(i) 的 index=?•
答案 :•
rightChild(i) 的 index=?•
答案 :•
證明 : 可以用歸納法 . 請自己看課本 p. 202 (Lemma 5.4)
•
1
2 3
4 5 6 7
如何在記憶體裡面表示 binary tree?
•
方法 2: Linked Representation•
每個 node 都用 malloc 拿一塊新的•
如果需要找到 parent, 可以加一個新的欄位 parentData left child right child
left child right child data
left A righ t left B righ
t
left C righ t root
Binary Tree Traversal
•
有一棵 binary tree 後 , 我們要怎麼把樹的每一個 node 都走一遍呢 ?•
到某一個 node 的時候 , 有三件事情可以做 :•
1. 走左邊的 child 那邊的 node 們 ( 用 L 表示 Left bran ch)•
2. 走右邊的 child 那邊的 node 們 ( 用 R 表示 Right bra nch)•
3. 處理自己這個 node 的資料 ( 用 V 表示 Visit)1
2 3
4 5 6 7
Binary Tree Traversal
•
如果 L 一定要在 R 之前 , 那麼有三種•
VLR: preorder•
LVR: inorder•
LRV: postorder•
請同學說明 preorder, inorder, postorder traversal 分 別順序是如何雍穎
昱廷 立中
雅喬 偉誠
Binary tree with arithmetic expression
•
每個 arithmetic expression 都可以建立一個 expression tree•
Preorder prefix•
Inorder infix•
Postorder postfix•
請同學試試看•
( 亂寫一個 expression 看看 )*
- 5
2 3
Binary Tree Traversal
•
可以用 recursive 寫法來做 traversal ( 會很簡潔 ):void inorder(treePointer ptr) { inorder(ptr->leftChild);
visit(ptr);
inorder(ptr->rightChild);
}
那如果不要用 recursive 寫法呢 ?
•
用 Stack for(;;) {for(;node;node=node->leftChild) push(node);
node=pop();
if (!node) break;
printf(“%d”, node->data);
node=node->rightChild;
}
Level-order traversal
• 如果改成用 queue 呢 ?
add(ptr);
for(;;) {
ptr=delete();
if (ptr) {
printf(“%d”, ptr->data);
if (ptr->leftChild) add(ptr->leftChild);
if (ptr->rightChild) add(ptr->rightChild);
} else break;
}
Priority Queues
•
一種每次都可以拿到 priority 最高的 element 的 queue•
直接來定義 operations•
Push(element) 把 element 放進 queue 裡面•
Pop() 把 element 拿出來 . 這個 element 有最高的 prior ity•
( 可以想像 , 放進去的時候有做一些排序 )•
另外也有 empty, full 等等的 operation•
請同學想想看 , 要怎麼用已經學過的東西來做 priority q ueue?•
Linked List?Heap
•
Definition: A max tree is a tree in which the key v alue in each node is no smaller (larger) than the k ey values in its children (if any).•
Definition: A max heap is a complete binary tree th at is also a max tree. A min heap is a complete bin ary tree that is also a min tree.•
有了 heap, 我們要怎麼用它來 做 priority queue?•
Root 是不是永遠都是最大 ?14
12
10 8 6
7
Push 一個 element 到 Heap
•
加入的時候每次都能夠繼續維 持是一個 max heap•
怎麼加 ?•
1. 既然是 complete binary tree, 所以一定要加在下一 個該出現的地方 , 把新的 el ement 放在那邊 .•
2. 循序往 root 的方向移動 , 一直到不違反 parent > chil d 的規則為止14
12
10 8 6
7
20
從 Heap Pop 一個 element
•
從 root 拿走一個 element•
調整位置 , 繼續維持是一個 max heap•
1. 首先既然是 complete binary tree 拿掉的位置就沒有別的選擇 .•
2. 把拿掉的位置的 element,拿到 root 的地方 . 和 child 中比較大
的比較 . 如果比其小則與其交換 . 重複以上步驟直到不 再違反 parent > child 的規則為止 .
21
15
14 10
20