1 / 64

2-3-4 Tree and how it relates to Red Black Tree

2-3-4 Tree and how it relates to Red Black Tree. Outline. B Tree, B* Tree, B+ Tree 2-3 Tree, 2-3-4 Tree Red-Black Tree (RBT) Left-Leaning Red-Black Tree Double Red & Double Black

richardsd
Download Presentation

2-3-4 Tree and how it relates to Red Black Tree

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. 2-3-4Treeand how it relates to Red Black Tree

  2. Outline BTree, B* Tree, B+ Tree 2-3 Tree, 2-3-4 Tree Red-Black Tree (RBT) Left-Leaning Red-Black Tree Double Red & Double Black RBT “Insert” Example       The Same Example with 2-3-4 tree

  3. B Tree

  4. B-Treeisin memory of R. BayerItis a generalization of binary search tree in that a node (or, an entry) can have more than two children [wiki]. Degree 為 d的 B tree:1) 每個 node 含至多 d 個 child pointers (或 d-1個 elements) 2) 每個 node 至少1/2 滿 (即至少[ (d-1)/2]個 elements)

  5. 20 28 10 25 30 B-Tree of Degree 3

  6. B* Tree B-tree 的 node 至少2/3 滿

  7. 20 28 6 15 26 30 10 23 2 4 35 B* Treeof Degree 4

  8. B+ Tree • 含 index pages和 data pages • root node 和 internal nodes 為 index pages (keys only). • leaf nodes 為 data pages (排序的data ) data 即 element (含有 key) • 每個 node 至少 1/2 滿 (Fill Factor 50%).

  9. index page data page B+ Tree This B+ tree:

  10. 2-3 Tree

  11. 2-3 Tree 為 search tree 可為空或: 每個 internal node 為 2-node (有2 child pointers) 或3-node (有3 child pointers) A 2-node 40 B C 3-node 10 20 80

  12. 40 10 20 70 80 2-3 Tree Insertion insertCase 1:插入 70 • 先尋找 70. 發現不在其中. • 須知尋找70時 遇到哪node? 是 含 80 的 node C • node C 只有一個 element, 所以70 可放 C A B C

  13. A 20 40 B D C 10 30 70 80 Figure 3 2-3 Tree insertCase 2: 插入 30 • 會遇到 30 的是 node B • B 為 3-node, 須產生新 node D. • B 含 elements 10, 20, 30 • 其中最大element 30 放D • 最小element 10 放 B. • 中間20放B的parent A 這叫 Split (分裂): 1.產生新nodeD. 2.中間 20 推升上層

  14. 2-3 Tree Insertion (Cont.) insert case 3: 插入 60 • 尋找60會遇 node C • C 為 3-node,需產生新 node E • C含 elements 60,70,80 • 中間值 70 放在C的parent A • 最小值 60放C 最大值80放E • A 為 3-node,產生新 node F • A含 elements 20, 40, 70 • 中間值 40 放在A的parent G (需產生 G) • 最小值 20放A 最大值70放F

  15. G 40 A F 20 70 B D C E 10 30 80 60 2-3 Tree Insertion (Cont.) Figure 4 Insertion of 60 into the 2-3 tree of Figure 3

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

  17. 2-3 Tree Deletion (Cont.) A 50 80 D C B 60 10 20 95 (c) 90 deleted A 20 80 D C B 50 10 95 (d) 60 deleted Next, delete 95

  18. 2-3 Tree Deletion (Cont.) A 20 這叫 Merge (融合): 1.消去 node D 2.上層 80 併入下層 C B 50 80 10 (e) 95 deleted A 20 A C B 20 80 80 10 (g) 10 deleted (f) 50 deleted

  19. 2-3-4 Tree

  20. 2-3-4 Tree 它為 search tree 可為空或: • 每個 internal node 為 2, 3,或 4 node. (2-node有2 child pointers, 3-node有3 child pointers, 4-node有4 child pointers) • 所有external nodes 都在相同 level. 2-3-4 tree 類似2-3 tree, 但它有 4-node 如下圖 50 60 70

  21. 2-3-4 Tree Insertion There are 3 cases for a 4-node: Case 1: It is the root Case 2: Its parent is a 2-node Case 3: Its parent is a 3-node (fig. omitted)

  22. 2-3-4 Tree Insertion • Case 1: It is the root. t t (root) y x y z x z a b c d a b c d Figure1 when the root is a 4-node

  23. 2-3-4 Tree Insertion • Case 2: Its parent is a 2-node z x z e e w x y w y a b c d a b c d Figure 2 when the child of a 2-node is a 4-node

  24. 2-3-4 Tree 2-3-4 tree 轉成binary search tree 則稱為red-black tree red-black tree比2-3-4 tree節省空間 因為2-3-4node 會浪費不少 未存資料的空的空間

  25. Red-Black Tree(RBT)

  26. Red-Black Tree red-black tree 為 binarysearch tree: • 每個 node 不是red就是black • 每個leaf (NULL) 都為black • rednode 的兩個children都為black. • 每個 path 含相同數目的 black nodes. • red node不可接著red node (不可紅紅) A basic red-black tree

  27. Red-Black Tree A red-black tree with n internal nodes has height at most 2log(n+1). Red-Black tree can always be searched in O(log n) time.

  28. S L S L OR c  a L S a c b a b b Red-Black Tree Left- leaning Right- leaning c S for Small L for Large Figure 1 Transforming a 3-node into two red-black nodes

  29. Red-Black Tree M S M L  S L c d a b a d S for Small M, Middle L, Large b c Figure 2 Transforming a 4-node into two red-black nodes

  30. 50 10 70 7 40 60 80 75 90 5 9 30 85 92 1. 將下圖的 Red-Black Tree 轉成 2-3-4 Tree 2. 依序 (1)刪除60 (2)加入83. 再轉回 Red-Black Tree

  31. 50 10 70 5 30 60 75 85 80 7 40 90 9 92 上圖轉成的2-3-4 Tree

  32. 刪除 60 wasted space 70

  33. 加入 8 8 7

  34. 轉回Red-Black Tree

  35. Red Black Tree Saves Space In the example above, the 2-3-4 tree wastes 10 unused space of elements. The corresponding red-black tree cuts this waste!

  36. Left-Leaning Red-Black Tree

  37. LLRBT is easier to implement than RBT, especially the deletionIt requires 3-nodes are left-leaning, thus maintains 1-1 correspondence with 2-3-4 trees (see next page).

  38. L S L c  S a c b a b LL Red-Black Tree Left-Leaning (LL) S for Small L for Large Transforming a 3-node into LL red-black nodes

  39. Double Red & Double Black

  40. During Red-Black Tree insertion, abnormal Double Red may occur as shown next.

  41. Red-Black Tree Insertion 我們要對左圖 insert 4 1) 依 binary search tree   把 4 當 3 的 right child 2) 依 red black tree 新加入者為red 故 3,4 形成右圖 Double Red 違反Red Rule 2 2 1 3 3 1 4

  42. Double Red in 2-3-4 Tree 1 1 2 2 3 3 已滿, 此時 insert 4 這 node 爆掉了,故要調整之 4 對應的 Red-Black Tree: 2 3 4 此時 叫 Double Red雙紅, 表示原來 node 爆掉了 1 3 4

  43. During Red-Black Tree deletion, again, abnormal Double Black may occur as shown next.

  44. Double Black in 2-3-4 Tree 7 5 3 7 3 8 8 此時Delete 5 對應的 Red-Black Tree: 7 8 Double BLACK 雙黑線,表示 其中有個空 2-3-4 node.故要調整之 3

  45. RBT “Insert” Example This is textbook exercise 12.7 Insert 30, 40, 20, 90, 10, 50, 70, 60, 80 to an initially empty red-black tree.

  46. RBT insert 30 When any node is inserted, it must be red. The root 30 must be black.

  47. RBT insert 40 Because 40 is greater than 30, 40 is inserted as the right child of 30.

  48. RBT insert 20 Because 20 is less than 30, 20 is inserted as the left child of 30.

  49. RBT insert 90 Case 1: red uncle 20 1.change parent 40 & uncle 20 to black 2.change granspa 30 to red The root 30 must be black.

  50. RBT insert 10 Because 10 is less than 20, 10 is inserted as the left child of 20.

More Related