Tree (2): heap, deap, 2-3 tree, 2-3-4tree Lai Ah Fur
堆積(heap) max tree: 任一節點之鍵值不小於其子節點之鍵值 min tree: 任一節點之鍵值不大於其子節點之鍵值 max heap: 完整二元樹且為 max tree min heap: 完整二元樹且為 min tree
Which are heaps? Nonheaps?
Different heaps constructed with the same elements
建立heap二種方法 已知左右子樹皆為heap,插入節點在樹根,調整成heap 26,5,77,1,61,11,59,15,48,19 (1)先建立完整二元樹 (2)從有子樹的節點開始:節點編號由大而小,依序調整
create min heap Example:26,5,77,1,61,11,59,15,48,19 26 5 77 1 61 11 59 modify 26 5 77 1 19 11 59 15 48 61 26 5 77 1 19 11 59 15 48 61 26 1 11 5 19 77 59 15 48 61 1 5 11 15 19 77 59 26 48 61
建立heap (2) 已知二元樹為heap,插入節點於最後節點(滿足完整二元樹之定義),調整成heap 26,5,77,1,61,11,59,15,48,19,0
建立min heap 5 26 5 heapify 5 Input 77 Input 1 26 77 5 26 77 26 1 11 1 5 61 77 1 heapify 5 77 5 77 26 26 61 11 26,5,77,1,61,11,59,15,48,19,0
1 1 Input 59,15 5 11 5 11 heapify 26 61 77 59 15 61 77 59 15 26 1 15 11 1 5 19 77 59 26 48 61 5 11 heapify Input 48,19 15 61 77 59 26 48 19 26,5,77,1,61,11,59,15,48,19,0
1 5 11 1 11 Input 0 15 19 77 59 heapify 15 5 77 59 26 48 61 26 48 61 19
exercise 以上述二種方法, 重建立max heap
heap之插入&&刪減 heap之插入(insertion) heap之刪減(deletion) 將欲插入之節點置於heap最後節點之後,成為新的最後節點(滿足完整二元樹之定義),再由插入處往樹根調整(heapify)成heap heap之刪減(deletion) 刪除樹根(max heap之最大值或min heap之最小值),取出最後節點,置於heap之樹根處,由樹根往下調整成heap
Enqueuing an element to a heap
delqueuing an element to a heap
Deap A DEAP is a double-ended heap that supports the double-ended priority queue operations of insert, delete min, and delete max. 完整二元樹 (a complete binary tree) 樹根節點不含資料項目,左子樹是min-heap,右子樹是max-heap partner:左子樹節點i之鍵值必小於等於右子樹相對位置節點j之鍵值 若節點j不存在,則節點i之鍵值必小於等於節點j之父節點之鍵值. j=i+2└log i┘-1,if(j>n)j=j/2
Deap用途 作為double ended priority queue: insert an element with arbitrary key delete an element with the largest key delete an element with the smallest key
insert 5 45 10 8 25 40 15 19 9 29 20 5 45 10 8 25 40 15 19 9 29 20 Insert 4 j i 5 45 10 8 25 40 15 4 9 29 20 19 heapify 5 45 4 8 25 40 15 10 9 29 20 19 交換 4 45 5 8 25 40 15 10 9 29 20 19 heapify 比partner小(4<19), 必須交換(move 19 to node j)
insert 5 45 10 8 25 40 15 19 9 29 20 5 45 10 8 25 40 15 19 9 29 20 30 Insert 30 比partner大(30>19),不必交換 5 45 10 8 30 40 15 19 9 29 20 25 heapify
Delete: fetch max heapify 11 : 11>8(partner) 4 45 5 8 25 40 15 10 9 30 20 11 heapify 4 40 8 25 5 20 15 10 9 30 11 tmp 4 40 11 8 25 5 : 11>8(partner) : insert tmp into empty position 20 15 10 9 30
Delete min 20 heapify 5 45 Fetch min 8 45 40 40 8 25 9 25 10 10 20 20 tmp 15 19 9 30 15 19 30 heapify : 20<40(parent of partner 20),no change is needed, so insert 20 into the empty position 8 45 10 9 25 40 15 19 20 30
2-3 tree 2-node:分支度(degree)為2的節點,含有一筆element
2-3 tree 內部節點可為2-node或3-node的找尋樹• 所有失敗節點(外部節點)都在同一level• 左子樹內之節點鍵值< 節點中之左鍵值< 中間子樹內之節點鍵值< 節點中之右鍵值< 右子樹內之節點鍵值 2-node節點鍵值大小關係: 左子樹內之節點鍵值<節點中之鍵值<右子樹內之節點鍵值 高度為h的2-3樹,資料筆數(鍵值)為2h-1~3h-1• 資料筆數(鍵值)為n的2-3樹,高度為┌log3(n+1)┐~┌log2(n+1)┐
插入 2-node:直接插入即可• 3-node:插入後節點含有三筆資料(鍵值),須split node,將節點一分為二,各含一筆資料,居中間值之資料提升到上一level• 若上一level也是3-node,繼續此split node步驟,直到樹根• 樹的因插入節點而成長,二元找尋樹(AVL樹)往樹葉成長,2-3樹則是朝樹根成長•
An example 2-3 tree A 10 20 40 80 C B
Example of insertion <<<< (a) 70 inserted 10 20 40 30 A D B 70 80 C 10 20 40 70 80 A C B 10 20 40 80 C B (a) 70 inserted (b) 30 inserted B is 3-node, so Create a new node D 最大值放D,最小值放B,中間值放B的parent
Insertion of 60 into the 2-3 tree 10 20 40 30 A D B 70 80 C New node 10 20 30 A D B 60 70 80 F E C 40 G A 20 40 70 B D C E 10 30 60 80 Insertion of 60 into the 2-3 tree
Deletion of the 2-3 tree 被刪除之資料若在3-node(樹葉節點),直接刪除 被刪除之資料若在2-node,須進行rotate或combine: rotate:往左或右兄弟調一筆資料,經由父節點旋轉過來. combine:左(右)兄弟均為2-node,不能調資料過來,即不能進行rotate,與父節點、左(右)兄弟節點合併成一節點.此時父節點被刪除一筆資料, 若父節點為3-node,直接刪除即可. 若父節點為2-node 繼續此combine步驟,直到樹根. 樹的高度因刪除節點而縮減,二元找尋樹(AVL樹)往樹葉縮減,2-3樹則是朝樹根縮減.
The three cases for rotation in a 2-3 tree x ? y z r q p a b c d a b c d (a) p is the left child of r x y z ? r q p a b c d a b c d (b) p is the middle child of r
(c) p is the right child of r w z x y q p b c d e a b c (c) p is the right child of r
Combining in a 2-3 tree when p is a the left child of r x y r q p a b c a b c (a) x z y r q p a b c a b c d (b)
Deletion from a 2-3 tree (1) 10 20 50 80 60 70 A C B 90 95 D 10 20 50 80 60 A C B 90 95 D (b) 70 deleted (a) Initial 2-3 tree
Deletion from a 2-3 tree (2) 10 20 50 80 60 A C B 95 D 10 20 80 50 A C B 95 D rotate (c) 90 deleted (d) 60 deleted
Deletion from a 2-3 tree (3) New root (e) 95 deleted (f) 50 deleted 10 20 50 80 A C B A 20 80 B 10 20 80 C B Combine: move 80 to D’s left sibling C, Delete D combine 20 80 C B (g) 10 deleted 可續向上combine, 但已到root If C is 3-node ,then rotate
2-3-4 tree: (2,4) tree Every node has at most four children. 定義 2-node:分支度(degree)為2的節點,含有一筆element• 3-node:分支度(degree)為3的節點,含有二筆element• 4-node:分支度(degree)為4的節點,含有三筆element• 內部節點可為2-node,3-node或4-node的找尋樹• All the external node have the same depth.所有失敗節點(外部節點)都在同一level The height of a (2,4) tree storing n items is ⊝(log n)
高度為h的2-3-4樹,資料筆數(鍵值)為2h-1~4h-1• 4-node節點鍵值大小關係: 最左子樹內之節點鍵值<節點中之左鍵值<次左子樹內之節點鍵值<節點中之中間鍵值<次右子樹內之節點鍵值<節點中之右鍵值<最右子樹內之節點鍵值 3-node節點鍵值 左子樹內之節點鍵值<節點中之左鍵值<中間子樹內之節點鍵值<節點中之右鍵值<右子樹內之節點鍵值 2-node節點鍵值 左子樹內之節點鍵值<節點中之鍵值<右子樹內之節點鍵值 高度為h的2-3-4樹,資料筆數(鍵值)為2h-1~4h-1• 資料筆數(鍵值)為n的2-3-4樹,高度為┌log4(n+1)┐~┌log2(n+1)┐
插入 從樹根往樹葉找尋欲插入之位置(樹葉節點)時,若逢4-node,則先split此4-node,再往下找尋•最後找到之插入位置必非4-node,直接插入即可• 樹的高度因插入節點而成長,2-3-4樹與2-3樹一樣,都是朝樹根成長(split) •
Insert 4,6,12 3 node 4 node 2 node split Insert 15, cause overflow
Insert 3 Insert 5, cause overflow split
Insert 8 Insert 10
Example 2: insertion Insert 17,cause overflow split
Another split, create a new root node After split, a new overflow occur
刪除 被刪除之資料若不在樹葉節點,如二元找尋樹一般,轉成樹葉節點之刪除• 從樹根往樹葉找尋欲刪除之節點位置(樹葉節點)時,若逢2-node,則先進行rotate或combine,再往下找尋•最後找到之刪除位置必非2-node,直接刪除即可• 樹的高度因刪除節點而縮減,2-3-4樹與2-3樹一樣,都是朝樹根縮減(combine) • Restructuring:假設從p節點往下應移到q節點時: 1•p節點是樹葉(q節點為外部節點),直接刪除 若p節點是2-node,刪除後變成空樹• 2•若q節點不是2-node,直接由p節點移到q節點,無須restructuring• 3•若q節點是2-node,且q節點之鄰近兄弟有3-node或4-node:進行rotate• 4•若q節點是2-node,且q節點之鄰近兄弟皆為2-node:進行combine• 刪除
The Rotate (transfer) operation Delete 4, cause underflow Delete 12, cause underflow
The combine (fusion operation) Delete 13
fusion, cause another underflow Delete 14, cause underflow Second fusion, cause the root to be removed
Transformation when the 4-node is the root z x y a b c d t a b c d
Transformation when the 4-node is the child of a 2-node x y z a b a b c d e c d (a) x y z w b c b c d e a d e (b)
Transformation when the 4-node is the left child of a 3-node v w x y z a b e c d a b c d f
Transformation when the 4-node is the left middle child of a 3-node x y v z b c d e a b c d e f
Deletion transformation when the nearest sibling is a 3-node p q r w z v x y f a b c d e a b c d e (a) q is the left child of a 3-node (b) q is the left child of a 4-node g u a b c d e