Data Structures Balanced Binary Search Tree Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/ E-mail: viren@ee.iitb.ac.in EE-717/453:Advance Computing for Electrical Engineers Lecture 8 (15 Aug 2013)
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 15 Aug 2013 EE-717/EE-453@IITB 2
Example Red Black Tree 10 7 40 45 30 3 8 60 35 1 20 5 25
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. 15 Aug 2013 EE-717/EE-453@IITB 4
Example Red Black Tree 10 7 40 45 3 8 30 60 35 1 20 5 25
Properties • The height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1). 15 Aug 2013 EE-717/EE-453@IITB 6
Properties • Start with a red black tree whose height is h; collapse all red nodes into their parent black nodes to get a tree whose node- degrees are between 2 and 4, height is >= h/2, and all external nodes are at the same level. 15 Aug 2013 EE-717/EE-453@IITB 7
Properties 1 0 7 4 0 4 3 8 3 5 0 6 3 1 2 5 0 5 0 2 5
Properties • Let h’>= h/2 be the height of the collapsed tree. • In worst-case, all internal nodes of collapsed tree have degree 2. • Number of internal nodes in collapsed tree >= 2h’-1. • So, n >= 2h’-1 • So, h <= 2 log2 (n + 1) 15 Aug 2013 EE-717/EE-453@IITB 9
Properties • At most 1 rotation and O(log n) color flips per insert/delete. 15 Aug 2013 EE-717/EE-453@IITB 10
Insert • New pair is placed in a new node, which is inserted into the red-black tree. • New node color options. – Black node => one root-to-external-node path has an extra black node (black pointer). ● Hard to remedy. – Red node => one root-to-external-node path may have two consecutive red nodes (pointers). ● May be remedied by color flips and/or a rotation. 15 Aug 2013 EE-717/EE-453@IITB 11
Classification of 2 Red Nodes/Pointers gp pp L d L p c b • XYz a b – X => relationship between gp and pp. ● pp left child of gp => X = L. – Y => relationship between pp and p. ● p right child of pp => Y = R. – z = b (black) if d = null or a black node. – z = r (red) if d is a red node. 15 Aug 2013 EE-717/EE-453@IITB 12
XYr • Color flip. gp gp pp pp d d p p c c a b a b • Move p, pp, and gp up two levels. • Continue rebalancing if necessary. 15 Aug 2013 EE-717/EE-453@IITB 13
LLb • Rotate. y gp z pp x z y d p x a b c d c a b • Done! • Same as LL rotation of AVL tree. 15 Aug 2013 EE-717/EE-453@IITB 14
LRb • Rotate. y gp z pp x z x d a b c d a p y b c • Done! • Same as LR rotation of AVL tree. • RRb and RLb are symmetric. 15 Aug 2013 EE-717/EE-453@IITB 15
Delete • Delete as for unbalanced binary search tree. • If red node deleted, no rebalancing needed. • If black node deleted, a subtree becomes one black pointer (node) deficient. 15 Aug 2013 EE-717/EE-453@IITB 16
Delete A Black Leaf 1 0 7 4 0 4 3 8 3 5 0 6 3 1 2 5 0 5 0 2 5 • Delete 8.
Delete A Black Leaf 1 0 py 7 4 0 4 y 3 3 5 0 6 3 1 2 5 0 5 0 2 • y is root of deficient subtree. 5 • py is parent of y.
Delete A Black Degree 1 Node 1 0 py 7 40 45 3 8 30 y 60 35 1 20 5 25 • Delete 45. • y is root of deficient subtree.
Delete A Black Degree 2 Node 10 7 40 45 3 8 30 60 35 1 20 5 25 • Not possible, degree 2 nodes are never deleted.
Rebalancing Strategy • If y is a red node, make it black. 10 py 7 40 45 3 8 30 y 60 35 1 20 5 25 15 Aug 2013 EE-717/EE-453@IITB 21
Rebalancing Strategy • Now, no subtree is deficient. Done! 10 py 7 40 45 3 8 30 y 60 35 1 20 5 25 15 Aug 2013 EE-717/EE-453@IITB 22
Rebalancing Strategy • y is a black root (there is no py). • Entire tree is deficient. Done! y 10 7 40 45 3 8 30 60 35 1 20 5 25 15 Aug 2013 EE-717/EE-453@IITB 23
Rebalancing Strategy • y is black but not the root (there is a py). py v y a b • Xcn ● y is right child of py => X = R. ● Pointer to v is black => c = b. ● v has 1 red child => n = 1. 15 Aug 2013 EE-717/EE-453@IITB 24
Rb0 (case 1) py py v y v y a b a b • Color change. • Now, py is root of deficient subtree. • Continue! 15 Aug 2013 EE-717/EE-453@IITB 25
Rb0 (case 2) py py v y v y a b a b • Color change. • Deficiency eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 26
Rb1 (case 1) py v v py y a a b b y • LL rotation. • Deficiency eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 27
Rb1 (case 2) w py py v v y c y a b a w • LR rotation. b c • Deficiency eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 28
Rb2 w py py v v y c y a b a w • LR rotation. b c • Deficiency eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 29
Rr(n) • n = # of red children of v’s right child w. py v y Rr( w a 2) b c 15 Aug 2013 EE-717/EE-453@IITB 30
Rr(0) v py py v a y b y a b • LL rotation. • Done! 15 Aug 2013 EE-717/EE-453@IITB 31
Rr(1) (case 1) w py py v v y c y a b a w • LR rotation. b c • Deficiency eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 32
Rr(1) (case 2) x py py v v y d y a w w a x b b c • Rotation. c d • Deficiency eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 33
Rr(2) x py py v v y d y a w a w b c x b • Rotation. • Deficiency c d eliminated. • Done! 15 Aug 2013 EE-717/EE-453@IITB 34
Red Black Tree • The height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1). • java.util.TreeMap => red black tree 15 Aug 2013 EE-717/EE-453@IITB 35
B-Tree 15 Aug 2013 EE-717/EE-453@IITB 36
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal- memory dictionaries to overcome cache- miss penalties. 15 Aug 2013 EE-717/EE-453@IITB 37
AVL Trees • n = 230 = 109 (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. 15 Aug 2013 EE-717/EE-453@IITB 38
Red-Black Trees • n = 230 = 109 (approx). • 30 <= height <= 60. • When the red-black tree resides on a disk, up to 60 disk access are made for a search. • This takes up to (approx) 6 seconds. • Not acceptable. 15 Aug 2013 EE-717/EE-453@IITB 39
m-way Search Trees • Each node has up to m – 1 pairs and m children. • m = 2 => binary search tree. 15 Aug 2013 EE-717/EE-453@IITB 40
4-Way Search Tree 10 30 35 k > 35 k < 10 10 < k < 30 30 < k < 35 15 Aug 2013 EE-717/EE-453@IITB 41
Maximum # Of Pairs • Happens when all internal nodes are m- nodes. • Full degree m tree. • # of nodes = 1 + m + m2 + m3 + … + mh-1 = (mh – 1)/(m – 1). • Each node has m – 1 pairs. • So, # of pairs = mh – 1. 15 Aug 2013 EE-717/EE-453@IITB 42
Capacity Of m-Way Search Tree m = 2 m = 200 6 - 1 h = 3 7 8 * 10 11 - 1 h = 5 31 3.2 * 10 16 - 1 h = 7 127 1.28 * 10 15 Aug 2013 EE-717/EE-453@IITB 43
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. 15 Aug 2013 EE-717/EE-453@IITB 44
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. 15 Aug 2013 EE-717/EE-453@IITB 45
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. 15 Aug 2013 EE-717/EE-453@IITB 46
Choice Of m • Worst-case search time. – (time to fetch a node + time to search node) * height – (a + b*m + c * log2m) * h where a, b and c are constants. search time 50 400 m 15 Aug 2013 EE-717/EE-453@IITB 47
Recommend
More recommend