Download presentation
Presentation is loading. Please wait.
Published byMilo Richardson Modified over 9 years ago
1
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal nodes.
2
Extended Binary Tree external nodeinternal node
3
2-3 Tree Definition Every internal node is either a 2-node or a 3-node. A 2-node has one key and 2 children/subtrees. All keys in left subtree are smaller than this key. All keys in right subtree are bigger than this key. 8 2-node L R
4
2-3 Tree Definition A 3-node has 2 keys and 3 children/subtrees; first key is smaller than second key. 1 3 3-node RML All keys in left subtree are smaller than first key. All keys in middle subtree are bigger than first key and smaller than second key. All keys in right subtree are bigger than second key. All external nodes are on the same level.
5
Example 2-3 Tree 15 20 8 4 1 35 630 409 17 1 3 8 2-node 3-node
6
Minimum # Of Pairs/Elements Happens when all internal nodes are 2-nodes.
7
Minimum # Of Pairs/Elements Number of nodes = 2 h – 1, where h is tree height (excluding external nodes). Each node has 1 (key, value) pair. So, minimum # of pairs = 2 h – 1
8
Maximum # Of Pairs/Elements Happens when all internal nodes are 3-nodes. Full degree 3 tree. # of nodes = 1 + 3 + 3 2 + 3 3 + … + 3 h-1 = (3 h – 1)/2. Each node has 2 pairs. So, # of pairs = 3 h – 1.
9
2-3 Tree Height Bounds 2 h – 1 <= n <= 3 h – 1. log 3 (n+1) <= h <= log 2 (n+1).
10
Node Structure 2-node uses LC, P1, and MC. 3-node uses all fields. May have optional parent field. Only internal nodes are represented! LCP1MCP2RC
11
Search 15 20 8 4 1 35 630 409 17 External nodes not shown.
12
Insert 15 20 8 4 1 35 630 409 17 Insert pair with key = 16.
13
Insert 15 20 8 4 1 35 630 409 Move P1 to P2. Insert as P1. Now insert a pair with key = 2. New pair goes into a 3-node. 16 17
14
Insert Into A Leaf 3-node Insert new pair so that the 3 keys are in ascending order. Move third key into a new 2-node. 1 2 3 1 2 3 Insert second key and pointer to new 2-node into parent. 31 2
15
Insert 15 20 8 4 1 35 630 409 Insert a pair with key = 2. 16 17
16
Insert 15 20 8 4 5 630 409 16 17 3 1 2 Insert a pair with key = 2 plus a pointer into parent.
17
Insert Now, insert a pair with key = 18. 15 20 8 1 2 4 5 630 409 16 17 3
18
Insert Into A Leaf 3-node Insert new pair so that the 3 keys are in ascending order. Move third key into a new 2-node. 16 17 18 16 17 18 Insert second key and pointer to new 2-node into parent. 1816 17
19
Insert Insert a pair with key = 18. 15 20 8 1 2 4 5 630 409 16 17 3
20
Insert Insert a pair with key = 17 plus a pointer into parent. 15 20 8 1 2 4 5 630 4093 18 16 17
21
Insert Into A Nonleaf 3-node Insert new pair and pointer so that the 3 keys are in ascending order. Move third key and 3 rd and 4 th pointers into a new 2-node. Insert second key and pointer to new 2-node into parent. 15 17 20 15 17 20 2015 17
22
Insert Insert a pair with key = 17 plus a pointer into parent. 15 20 8 1 2 4 5 630 4093 18 16 17
23
Insert Insert a pair with key = 17 plus a pointer into parent. 8 1 2 4 5 630 409316 17 15 18 20
24
Insert Now, insert a pair with key = 7. 1 2 4 5 630 409316 15 18 20 8 17
25
Insert Into A Nonleaf 3-node Insert new pair and pointer so that the 3 keys are in ascending order. Move third key and 3 rd and 4 th pointers into a new 2-node. 5 6 7 5 6 7 Insert second key and pointer to new 2-node into parent. 75 6
26
Insert Now, insert a pair with key = 7. 1 2 4 5 630 409316 15 18 20 8 17
27
Insert Insert a pair with key = 6 plus a pointer into parent. 30 40 1 2 4 9316 15 18 20 8 17 5 7 6
28
Insert Into A Nonleaf 3-node Insert new pair and pointer so that the 3 keys are in ascending order. Move third key and 3 rd and 4 th pointers into a new 2-node. 2 4 6 2 4 6 Insert second key and pointer to new 2-node into parent. 62 4
29
Insert Insert a pair with key = 6 plus a pointer into parent. 30 40 1 2 4 9316 15 18 20 8 17 5 7 6
30
Insert Insert a pair with key = 4 plus a pointer into parent. 30 40 1 9 3 16 15 18 20 8 17 6 4 2 57
31
Insert Insert a pair with key = 8 plus a pointer into parent. There is no parent. So, create a new root. 30 40 1 9 3 16 15 18 20 6 8 2 57 4 17
32
Insert Height increases by 1. 30 40 1 9 3 16 15 18 20 6 2 57 4 17 8
33
Delete Delete the pair with key = 8. Transform deletion from interior into deletion from a leaf. Replace by largest in left subtree. 15 20 8 1 2 4 5 630 409 16 17 3
34
Delete From A Leaf Delete the pair with key = 16. 3-node becomes 2-node. 15 20 8 1 2 4 5 630 409 16 17 3
35
Delete From A Leaf Delete the pair with key = 17. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If so borrow a pair and a subtree via parent node. 15 20 8 1 2 4 5 630 4093 17
36
Delete From A Leaf Delete the pair with key = 20. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair. 15 30 8 1 2 4 5 693 20 40
37
Delete From A Leaf Delete the pair with key = 30. Deletion from a 3-node. 3-node becomes 2-node. 30 40 8 1 2 4 5 693 15
38
Delete From A Leaf 8 1 2 4 5 693 15 40 Delete the pair with key = 3. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If so borrow a pair and a subtree via parent node.
39
Delete From A Leaf 8 1 2 5 94 15 40 Delete the pair with key = 6. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair. 6
40
Delete From A Leaf 8 1 4 59 15 40 Delete the pair with key = 40. Deletion from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair. 2
41
Delete From A Leaf 8 1 4 5 Parent pair was from a 2-node. Check one sibling and determine if it is a 3-node. If not, combine with sibling and parent pair. 2 9 15
42
Delete From A Leaf 1 4 5 Parent pair was from a 2-node. Check one sibling and determine if it is a 3-node. No sibling, so must be the root. Discard root. Left child becomes new root. 9 15 2 8
43
Delete From A Leaf 1 4 5 Height reduces by 1. 9 15 2 8
44
2-3-4 Trees Problems with 2-3 trees. LCP1MCP2RC 2-3 node structure 2-nodes waste space. Overhead of moving a pair and pointers when changing between 2-node and 3-node use. Extend to 2-3-4 tree, which may be represented as a binary tree.
45
2-3-4 Tree Definition Every internal node is either a 2-, 3-, or 4-node. 2- and 3-nodes have same properties as in a 2-3 tree. A 4-node has 3 keys and 4 children; 1 st key is smaller than 2 nd key which is smaller than 3 rd key. All keys in left subtree are smaller than 1 st key. All keys in 2 nd subtree are bigger than 1 st key and smaller than 2 nd key. All keys in 3 rd subtree are bigger than 2 nd key and smaller than 3 rd key. All keys in right subtree are bigger than 3 rd key. All external nodes are on the same level.
46
4-node 10 30 35 k < 10 10 < k < 30 30 < k < 35 k > 35
47
Minimum # Of Pairs Happens when all internal nodes are 2-nodes.
48
Minimum # Of Pairs Number of nodes = 2 h – 1, where h is tree height (excluding external nodes). Each node has 1 (key, value) pair. So, minimum # of pairs = 2 h – 1
49
Maximum # Of Pairs Happens when all internal nodes are 4-nodes. Full degree 4 tree. # of nodes = 1 + 4 + 4 2 + 4 3 + … + 4 h-1 = (4 h – 1)/3. Each node has 3 pairs. So, # of pairs = 4 h – 1.
50
2-3-4 Tree Height Bounds 2 h – 1 <= n <= 4 h – 1. log 4 (n+1) <= h <= log 2 (n+1).
51
Node Structure 2-node uses LC, P1, and LMC. 3-node uses LC, P1, LMC, P2, and RMC. 4-node uses all fields. Optional parent field. Only internal nodes are represented! LCP1LMCP2RCRMCP3
52
Two-Pass Insert Move down from root to a leaf. Insert in leaf. If leaf now has 4 pairs, split as below. ABCDE 10 20 30 40 Insert 20 and pointer to new 3-node into parent, as was done for 2-3 trees. 10 A B 20 30 40 CDE
53
Two-Pass Delete Transform interior delete to leaf delete. Delete from a 3-node or 4-node leaf reduces leaf degree. Delete from a 2-node leaf. Check one sibling and determine if it is a 3- or 4- node. If so, borrow a pair and a subtree via parent node. If not, combine with sibling 2-node and in-between pair in parent. Continue up the tree if parent was a 2- node.
54
One-Pass Operations No bottom-to-top pass. Can pipeline inserts. Can pipeline deletes from leaf nodes.
55
Top-Down Insert Bottom-up pass is triggered when new pair is inserted into a 4-node leaf. Split 4-nodes on the way down so you never insert into a 4-node leaf! Look before you leap! If the node you are about to move to is a 4-node, split it into two 2-nodes. Then move to a 2-node.
56
Cases For 4-node Move The 4-node we attempt to move to may be: The root. Child of a 2-node. Child of a 3-node. It cannot be the child of a 4-node, because we will never be at a 4-node.
57
Root Is A 4-node Height of tree increases by 1. Compare with y and then move to x or z. xyz abc d x y z a bcd
58
4-node Left Child Of 2-node No change in height of subtree. Compare with x and then move to w or y. 4-node right child of 2-node is similar. wxy abc d z e w z y a bcd x e
59
4-node Left Child Of 3-node No change in height of subtree. Compare with w and then move to v or x. 4-node middle or right child of 3-node is similar. vwx abc d f zy e v x a b cd wyz fe
60
Top-Down Delete Bottom-up pass is triggered when deletion is from a 2-node leaf. Look before you leap! May start at a 2-node root but may not be at any other 2-node. If the node you are about to move to is a 2- node, make it a 3-node or 4-node. Then move to the 3-node 4-node.
61
Cases For 2-node Move Moving to a 2-node root is permitted. No other move to a 2-node is permitted. Other attempts to move to a 2-node may be classified as below. The 2-node’s nearest sibling is also a 2-node. The 2-node’s nearest sibling is a 3-node. The 2-node’s nearest sibling is a 4-node. In each of the preceding cases, the 2-node’s parent may be a 2-node root, a 3-node, or a 4- node.
62
Root Is 2-node Leaf Delete root. Tree becomes empty. x
63
Moving To 2-node Whose Nearest Sibling Is 2-node Current node is 2-node => at root. xyz abc d x y z a bcd Height decreases by 1. Reapply moving rules before you move down.
64
No change in height of subtree. Moving to middle or right child is similar. Current node is 4-node is also similar. wxy abc d z e w z y a bcd x e Current node is 3-node. Moving to w. Moving To 2-node Whose Nearest Sibling Is 2-node
65
Moving To 2-node Whose Nearest Sibling Is 3-node Current node is 2-node => at root. w x a bc z d y e y z dea x b w c Moving to w. No change in height of tree. Moving to right child 2-node is similar.
66
Moving To 2-node Whose Nearest Sibling Is 3-node No change in height of subtree. Moving to middle or right child 2-node is similar. Current node is 4-node is also similar. Current node is 3-node. Moving to v. v z ac w f b y d x e y z da x f e w b v c
67
Moving To 2-node Whose Nearest Sibling Is 4-node Current node is 2-node => at root. Moving to u. No change in height of tree. Moving to right child 2-node is similar. u v a b c x d w e y f w a v b u c yx def
68
Moving To 2-node Whose Nearest Sibling Is 4-node No change in height of subtree. Moving to middle or right child 2-node is similar. Current node is 4-node is also similar. Current node is 3-node. Moving to u. u z ac v g b x d w e y f z a w g v b u c yx def
69
Binary Tree Representation Of 2-3-4 Trees Problems with 2-3-4 trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among 2-, 3-, and 4-node use. Represented as a binary tree for improved space and time performance. 2-3-4 node structure LCP1LMCP2RCRMCP3
70
Representation Of 4-node xyz abc d a bcd z y x
71
Representation Of 3-node xy ab c a bc y x a b y x c or
72
Representation Of 2-node a x b x a b
73
Example 10 7 8 1 5 30 40 20 25 35 45 60 3
74
Properties Of Binary Tree Representation Nodes and edges are colored. The root is black. Nonroot black node has a black edge from its parent. Red node has a red edge from its parent. Can deduce edge color from node color and vice versa. Need to keep either edge or node colors, not both.
75
Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes
76
Red Black Trees Colored Edges Definition Binary search tree. Child pointers are colored red or black. Pointer to an external node is black. No root to external node path has two consecutive red pointers. Every root to external node path has the same number of black pointers.
77
2-3-4 & Red-Black Equivalence 10 7 8 1 5 30 40 20 25 35 45 60 3
78
Red Black Tree The height of a red black tree that has n (internal) nodes is between log 2 (n+1) and 2log 2 (n+1). C++ STL implementation java.util.TreeMap => red black tree
79
Top-Down Insert Mimic 2-3-4 top-down algorithm. Split 4-nodes on the way down.
80
Root Is A 4-node xyz abc d x y z a bcd a bcd z y x a bcd z y x
81
4-node Left Child Of 2-node wxy abc d z ew z y a bcd x e z w y x abcd e z w y x abcd e
82
4-node Left Child Of 3-node vwx abc d f zy e v x a b cd wyz fe y v x w abcd z ef y v x w abcd z ef
83
vwx abc d f zy e v x a b cd wyz fe y v x w ab z e f c d y v x w abcd z ef
84
4-node Middle Child Of 3-node v w y x bc z a f d e x w v y a dbc z e f wxy bcd e f zv a wa bc f y de vxz
85
d z w y x bc v a f e x w v y a dbc z e f wxy bcd e f zv a wa bc f y de vxz
86
4-node Right Child Of 3-node One orientation of 3-node requires color flip. Other orientation requires RR rotation.
87
Top-Down Delete Mimic 2-3-4 top-down delete. Color flip followed by possible rotation.
88
Red-Black Analysis Less memory required than by 2-3-4 representation. Less time required by 4-node splits when red-black representation is used. O(log n) rotations per insert/delete.
89
B-Trees Extension of 2-3 and 2-3-4 trees to higher degree trees. Used to represent very large dictionaries that reside on disk.
90
AVL Trees n = 2 30 = 10 9 (approx). 30 <= height <= 43. When the AVL tree resides on a disk, up to 43 disk access are made for a search. This takes up to (approx) 4 seconds. Not acceptable.
91
Red-Black Trees n = 2 30 = 10 9 (approx). 30 <= height <= 60. When the AVL tree resides on a disk, up to 60 disk access are made for a search. This takes up to (approx) 6 seconds. Not acceptable.
92
m-way Search Trees Each node has up to m – 1 pairs and m children. m = 2 => binary search tree.
93
4-Way Search Tree 10 30 35 k < 10 10 < k < 30 30 < k < 35 k > 35
94
Maximum # Of Pairs Happens when all internal nodes are m-nodes. Full degree m tree. # of nodes = 1 + m + m 2 + m 3 + … + m h-1 = (m h – 1)/(m – 1). Each node has m – 1 pairs. So, # of pairs = m h – 1.
95
Capacity Of m-Way Search Tree
96
Definition Of B-Tree Definition assumes external nodes (extended m-way search tree). B-tree of order m. m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least ceil(m/2) children. External (or failure) nodes on same level.
97
2-3 And 2-3-4 Trees B-tree of order m. m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least ceil(m/2) children. External (or failure) nodes on same level. 2-3 tree is B-tree of order 3. 2-3-4 tree is B-tree of order 4.
98
B-Trees Of Order 5 And 2 B-tree of order m. m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least ceil(m/2) children. External (or failure) nodes on same level. B-tree of order 5 is 3-4-5 tree (root may be 2-node though). B-tree of order 2 is full binary tree.
99
Minimum # Of Pairs n = # of pairs. # of external nodes = n + 1. Height = h => external nodes on level h + 1. level# of nodes 1 1 2 >= 2 3 >= 2*ceil(m/2) h + 1 >= 2*ceil(m/2) h-1 n + 1 >= 2*ceil(m/2) h-1, h >= 1
100
Minimum # Of Pairs m = 200. n + 1 >= 2*ceil(m/2) h-1, h >= 1 height# of pairs 2 >= 199 3 >= 19,999 4 >= 2 * 10 6 – 1 5 >= 2 * 10 8 – 1 h <= log ceil(m/2) (n+1)/2 + 1
101
Choice Of m Worst-case search time. (time to fetch a node + time to search node) * height (a + b*m + c * log 2 m) * h where a, b and c are constants. m search time 50400
102
Bottom-Up Insert 15 20 8 4 1 35 630 409 Insertion into a full leaf triggers bottom-up node splitting pass. 16 17
103
Split An Overfull Node a i is a pointer to a subtree. p i is a dictionary pair. m a 0 p 1 a 1 p 2 a 2 … p m a m ceil(m/2)-1 a 0 p 1 a 1 p 2 a 2 … p ceil(m/2)-1 a ceil(m/2)-1 m-ceil(m/2) a ceil(m/2) p ceil(m/2)+1 a ceil(m/2)+1 … p m a m p ceil(m/2) plus pointer to new node is inserted in parent.
104
Worst-Case Disk Accesses 15 20 4 1 35 630 4013 16 17 7 12 9 8 10 Insert 2. Insert 18. Insert 14.
105
Worst-Case Disk Accesses Assume enough memory to hold all h nodes accessed on way down. h read accesses on way down. 2s+1 write accesses on way up, s = number of nodes that split. Total h+2s+1 disk accesses. Max is 3h+1.
106
Average Disk Accesses Start with empty B-tree. Insert n pairs. Resulting B-tree has p nodes. # splits 2. # pairs >= 1+(ceil(m/2) – 1)(p – 1). s avg <= (p – 2)/(1+(ceil(m/2) – 1)(p – 1)). So, s avg < 1/(ceil(m/2) – 1). m = 200 => s avg < 1/99. Average disk accesses < h + 2/99 + 1 ~ h + 1. Nearly minimum.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.