1 / 50

Tree (2): heap, deap, 2-3 tree, 2-3-4tree

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 二種方法.

lowri
Download Presentation

Tree (2): heap, deap, 2-3 tree, 2-3-4tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tree (2):heap, deap, 2-3 tree, 2-3-4tree Lai Ah Fur

  2. 堆積(heap) • max tree: 任一節點之鍵值不小於其子節點之鍵值 • min tree: 任一節點之鍵值不大於其子節點之鍵值 • max heap: 完整二元樹且為 max tree • min heap: 完整二元樹且為 min tree

  3. Which are heaps? Nonheaps?

  4. Different heaps constructed with the same elements

  5. 建立heap二種方法 • 已知左右子樹皆為heap,插入節點在樹根,調整成heap • 範例:依下列輸入資料,建立heap(先建立完整二元樹,再調整) • 26,5,77,1,61,11,59,15,48,19 • (1)先建立完整二元樹 (2)從有子樹的節點開始:節點編號由大而小,依序調整

  6. 26 26 26 1 26 5 1 5 5 5 11 11 77 77 77 59 59 59 59 59 19 19 19 19 61 11 11 77 77 11 15 1 1 5 1 15 26 15 15 15 48 48 48 48 48 19 61 61 61 61 Example:26,5,77,1,61,11,59,15,48,19 create min heap modify

  7. 建立heap (2) • 已知二元樹為heap,插入節點於最後節點(滿足完整二元樹之定義),調整成heap • 範例:依下列輸入資料,建立heap(一面輸入,一邊調整成heap) • 26,5,77,1,61,11,59,15,48,19,0

  8. 1 5 11 26 61 77 建立min heap 5 26 5 5 heapify Input 77 Input 1 26 77 5 26 77 26 1 Input 61,11 heapify 1 1 heapify 5 77 5 77 26 26 61 11 • 26,5,77,1,61,11,59,15,48,19,0

  9. 1 5 11 15 19 77 59 26 48 61 1 1 5 11 Input 59,15 5 11 heapify 26 61 77 59 15 61 77 59 15 26 1 5 11 heapify Input 48,19 15 61 77 59 26 48 19 • 26,5,77,1,61,11,59,15,48,19,0

  10. 1 0 5 11 1 11 Input 0 15 heapify 19 77 59 15 5 77 59 26 48 61 0 26 48 61 19

  11. exercise • 以上述二種方法, 重建立max heap

  12. heap之插入&&刪減 • heap之插入(insertion) • 將欲插入之節點置於heap最後節點之後,成為新的最後節點(滿足完整二元樹之定義),再由插入處往樹根調整(heapify)成heap • heap之刪減(deletion) • 刪除樹根(max heap之最大值或min heap之最小值),取出最後節點,置於heap之樹根處,由樹根往下調整成heap

  13. Enqueuing an element to a heap

  14. delqueuing an element to a heap

  15. 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

  16. 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

  17. 5 4 5 5 45 45 45 45 5 45 40 40 40 40 8 8 8 8 25 25 25 25 4 5 10 10 40 8 25 10 20 20 20 20 19 19 19 15 15 15 15 10 19 10 4 9 9 9 9 29 29 29 29 20 15 19 9 29 insert Insert 4 j i heapify 交換 heapify 比partner小(4<19), 必須交換(move 19 to node j)

  18. 5 5 45 45 40 40 8 8 30 25 5 10 10 45 15 15 19 19 9 9 29 29 40 20 20 30 25 8 25 10 20 15 19 9 29 insert Insert 30 比partner大(30>19),不必交換 heapify

  19. 4 45 40 8 25 5 20 11 15 10 9 30 Delete: fetch max 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

  20. 8 45 40 9 25 10 15 19 20 30  Delete min 5 Fetch min 8 45 45  40 40 8 9 25 25 10 10 tmp 20 20 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

  21. 2-3 tree • 2-node:分支度(degree)為2的節點,含有一筆element • 3-node:分支度(degree)為3的節點,含有二筆element

  22. 2-3 tree • 內部節點可為2-node或3-node的找尋樹• • 所有失敗節點(外部節點)都在同一level• • 3-node節點鍵值大小關係: • 左子樹內之節點鍵值< 節點中之左鍵值< 中間子樹內之節點鍵值< 節點中之右鍵值< 右子樹內之節點鍵值 • 2-node節點鍵值大小關係: • 左子樹內之節點鍵值<節點中之鍵值<右子樹內之節點鍵值 • 高度為h的2-3樹,資料筆數(鍵值)為2h-1~3h-1• • 資料筆數(鍵值)為n的2-3樹,高度為┌log3(n+1)┐~┌log2(n+1)┐

  23. 插入 • 2-node:直接插入即可• • 3-node:插入後節點含有三筆資料(鍵值),須split node,將節點一分為二,各含一筆資料,居中間值之資料提升到上一level• • 若上一level也是3-node,繼續此split node步驟,直到樹根• • 樹的因插入節點而成長,二元找尋樹(AVL樹)往樹葉成長,2-3樹則是朝樹根成長•

  24. C B 10 40 80 20 An example 2-3 tree A

  25. C B 20  10 80 70  30  10  40 70 40 10 20 80 80 40  20 A A C B D B C Example of insertion <<<< (a) 70 inserted (b) 30 inserted B is 3-node, so Create a new node D 最大值放D,最小值放B,中間值放B的parent

  26. 10 70 80 20 60 30 40 30 10 70 10 20 30 60 80 20 40 40 80 A D B C G A F D B E C New node A 70 C D B E Insertion of 60 into the 2-3 tree

  27. 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樹則是朝樹根縮減.

  28. x z y z x x y y x z ? ? z ? y ? r r p q q p a b c d a b c d r r p q q p a b c d a b c d The three cases for rotation in a 2-3 tree (a) p is the left child of r (b) p is the middle child of r

  29. w x z x w z y y a a q q p p e b c d b c (c) p is the right child of r

  30. r r x y z y x x x z y y p q p a b c b c a (a) r r p q p d d a b c b c a (b) Combining in a 2-3 tree when p is a the left child of r

  31. A C B D 60 50 10 90 90 60 50 10 95 70 80 80 20 95 20 A C B D Deletion from a 2-3 tree (1) (b) 70 deleted (a) Initial 2-3 tree

  32. 95 50 20 10 60 95 50 10 20 80 80 A C B D A C B D Deletion from a 2-3 tree (2) rotate (c) 90 deleted (d) 60 deleted

  33. B C C B B 20 80 20 10 50 20 10 20 80 80 80 A C B Deletion from a 2-3 tree (3) New root (e) 95 deleted (f) 50 deleted A Combine: move 80 to D’s left sibling C, Delete D combine (g) 10 deleted 可續向上combine, 但已到root • If C is 3-node • ,then rotate

  34. 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)

  35. 4-node節點鍵值大小關係: • 最左子樹內之節點鍵值<節點中之左鍵值<次左子樹內之節點鍵值<節點中之中間鍵值<次右子樹內之節點鍵值<節點中之右鍵值<最右子樹內之節點鍵值 • 3-node節點鍵值 • 左子樹內之節點鍵值<節點中之左鍵值<中間子樹內之節點鍵值<節點中之右鍵值<右子樹內之節點鍵值 • 2-node節點鍵值 • 左子樹內之節點鍵值<節點中之鍵值<右子樹內之節點鍵值 • 高度為h的2-3-4樹,資料筆數(鍵值)為2h-1~4h-1• • 資料筆數(鍵值)為n的2-3-4樹,高度為┌log4(n+1)┐~┌log2(n+1)┐

  36. 插入 • 從樹根往樹葉找尋欲插入之位置(樹葉節點)時,若逢4-node,則先split此4-node,再往下找尋•最後找到之插入位置必非4-node,直接插入即可• • 樹的高度因插入節點而成長,2-3-4樹與2-3樹一樣,都是朝樹根成長(split)•

  37. Insert 4,6,12 3 node 4 node 2 node split Insert 15, cause overflow

  38. Insert 3 Insert 5, cause overflow split

  39. Insert 8 Insert 10

  40. Example 2: insertion Insert 17,cause overflow split

  41. Another split, create a new root node After split, a new overflow occur

  42. 刪除 • 被刪除之資料若不在樹葉節點,如二元找尋樹一般,轉成樹葉節點之刪除• • 從樹根往樹葉找尋欲刪除之節點位置(樹葉節點)時,若逢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•

  43. The Rotate (transfer) operation Delete 4, cause underflow Delete 12, cause underflow

  44. The combine (fusion operation) Delete 13

  45. fusion, cause another underflow Delete 14, cause underflow Second fusion, cause the root to be removed

  46. t t z x y a b c d x y z c d a b Transformation when the 4-node is the root

  47. x w z y w x w z w x x z y y z y e e a b a b c d c d (a) a a b c b c d e d e Transformation when the 4-node is the child of a 2-node (b)

  48. e f e f y w v v x y w z x z a b c d a b c d Transformation when the 4-node is the left child of a 3-node

  49. v w v y w x z x z y f a a f b c d e b c d e Transformation when the 4-node is the left middle child of a 3-node

  50. w u w w v v x x y u x v y v z y w y z x z z p p f q f r q r a b c a b d e c d e (a) q is the left child of a 3-node p p g g q q r f r f a b c a b d e c d e (b) q is the left child of a 4-node Deletion transformation when the nearest sibling is a 3-node

More Related