Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI Trees and Red/Black Trees

Similar presentations


Presentation on theme: "CSCI Trees and Red/Black Trees"— Presentation transcript:

1 CSCI 104 2-3-4 Trees and Red/Black Trees
Mark Redekopp David Kempe

2 Definition 2-3-4 trees are very much like 2-3 trees but form the basis of a balanced, binary tree representation called Red-Black (RB) trees which are commonly used [used in C++ STL map & set] We study them mainly to ease understanding of RB trees 2-3-4 Tree is a tree where Non-leaf nodes have 1 value & 2 children or 2 values & 3 children or 3 values & 4 children All leaves are at the same level Like 2-3 trees, trees are always full and thus have an upper bound on their height of log2(n) a 2 Node a 3 Node 1 2 4 a 4 Node a valid tree 2 4 7 13 21

3 2-, 3-, & 4-Nodes 4-nodes require more memory and can be inefficient when the tree actually has many 2 nodes template <typename T> struct Item234 { T val1; T val2; T val3; Item234<T>* left; Item234<T>* midleft; Item234<T>* midright; Item234<T>* right; int nodeType; }; a 2 Node a 3 Node _ _ _ a 4 Node _ _ _

4 2-3-4 Search Trees Similar properties as a 2-3 Search Tree 4 Node:
Left subtree nodes are < l Middle-left subtree > l and < r Right subtree nodes are > r a 2 Node a 3 Node m l r <m > m <l > l && < r > r a 4 Node l m r <l > l && < m > m && < r > r

5 2-3-4 Insertion Algorithm
Key: Rather than search down the tree and then possibly promote and break up 4-nodes on the way back up, split 4 nodes on the way down To insert a value, 1. If node is a 4-node Split the 3 values into a left 2-node, a right 2-node, and promote the middle element to the parent of the node (which definitely has room) attaching children appropriately Continue on to next node in search order 2a. If node is a leaf, insert the value 2b. Else continue on to the next node in search tree order Insert 60, 20, 10, 30, 25, 50, 80 Key: 4-nodes get split as you walk down thus, a leaf will always have room for a value Empty Add 60 Add 20 Add 10 Add 30 20 20 60 20 60 10 60 10 30 60

6 2-3-4 Insertion Algorithm
Key: Split 4 nodes on the way down To insert a value, 1. If node is a 4-node Split the 3 values into a left 2-node, a right 2-node, and promote the middle element to the parent of the node (which definitely has room) attaching children appropriately Continue on to next node in search order 2a. If node is a leaf, insert the value 2b. Else continue on to the next node in search tree order Insert 60, 20, 10, 30, 25, 50, 80 Key: 4-nodes get split as you walk down thus, a leaf will always have room for a value Add 25 Add 50 Split first, then add 50 20 30 20 20 50 10 25 50 60 10 10

7 2-3-4 Insertion Algorithm
Key: Split 4 nodes on the way down To insert a value, 1. If node is a 4-node Split the 3 values into a left 2-node, a right 2-node, and promote the middle element to the parent of the node (which definitely has room) attaching children appropriately Continue on to next node in search order 2a. If node is a leaf, insert the value 2b. Else continue on to the next node in search tree order Insert 60, 20, 10, 30, 25, 50, 80 Key: 4-nodes get split as you walk down thus, a leaf will always have room for a value Add 80 20 30 20 30 80 10 25 50 60 10 25

8 2-3-4 Insertion Exercise 1 Add 55 20 30 10 25

9 2-3-4 Insertion Exercise 2 Add 58 10 25 50 55 80

10 2-3-4 Insertion Exercise 3 Add 57 30 20 60 10 25 80

11 2-3-4 Insertion Exercise 3 Resulting Tree 30 20 55 60 10 25 50 57 58 80

12 B-Trees 2-3 and trees are just instances of a more general data structure known as B-Trees Define minimum number of children (degree) for non-leaf nodes, d Non-root nodes must have at least d-1 keys and d children All nodes must have at most 2d-1 keys and 2d children 2-3-4 Tree (d=2) Used for disk-based storage and indexing with large value of d to account for large random-access lookup time but fast sequential access time of secondary storage

13 B Tree Resources

14 Tree Rotations

15 What values might be in the subtree rooted here
BST Subtree Ranges Consider a binary search tree, what range of values could be in the subtree rooted at each node At the root, any value could be in the "subtree" At the first left child? At the first right child? What values might be in the subtree rooted here (-inf, inf) (-inf,inf) z x ( ) ( ) ( ) ( ) y d a y ( ) ( ) ( ) ( ) x c b z ( ) ( ) ( ) ( ) a b c d

16 BST Subtree Ranges Consider a binary search tree, what range of values could be in the subtree rooted at each node At the root, any value could be in the "subtree" At the first left child? At the first right child? (-inf, inf) (-inf, inf) z x (-inf, z) (z, inf) (-inf, x) (x, inf) y d a y (x, y) (y, inf) (-inf, y) (y,z) x c b z (-inf, x) (x,y) (y,z) (z,inf) a b c d

17 Right Rotation Define a right rotation as taking a left child, making it the parent and making the original parent the new right child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) z y Right rotate of y & z (-inf, z) (z, inf) y d x z (-inf, y) (y,z) c ___ ___ x ___ ___ (-inf, x) (x,y) a b

18 Right Rotation Define a right rotation as taking a left child, making it the parent and making the original parent the new right child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) z y Right rotate of y & z (-inf, z) (z, inf) y d x z (-inf, y) (y,z) a b x c c d (-inf, x) (x,y) (y,z) (z, inf) (-inf, x) (x,y) a b

19 Left Rotation Define a left rotation as taking a right child, making it the parent and making the original parent the new left child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) y x Left rotate of y & x (-inf, x) (x, inf) x z a y (x, y) (y, inf) ___ ___ ___ ___ b z (y,z) (z,inf) c d

20 Left Rotation Define a left rotation as taking a right child, making it the parent and making the original parent the new left child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) y x Left rotate of y & x (-inf, x) (x, inf) x z a y (x, y) (y, inf) a b c d b z (-inf, x) (x,y) (y,z) (z, inf) (y,z) (z,inf) c d

21 Rotations Define a right rotation as taking a left child, making it the parent and making the original parent the new right child Where do subtrees a, b, and c belong? Use their ranges to reason about it… (-inf, inf) (-inf, inf) y x Right rotate of x & y (-inf, y) (y, inf) (-inf, x) (x, inf) x c a y (x, y) (y, inf) (-inf, x) (x,y) Left rotate of y & x a b b c

22 Rotation's Effect on Height
When we rotate, it serves to re-balance the tree z y Right rotate of y and z h+1 y x z h h+2 h+3 x c h h h h h h h For now let's always specify the two nodes involved in a rotation (i.e. right rotate y and z). We could just specify one node if we assume that node is the parent or the child, but this often gets new programmers into trouble when they forget the assumption. Better to be explicit

23 "Balanced" Binary Search Trees
Red Black Trees

24 Red Black Trees A red-black tree is a binary search tree
Only 2 nodes (no 3- or 4-nodes) Can be built from a tree directly by converting each 3- and 4- nodes to multiple 2-nodes All 2-nodes means no wasted storage overheads Yields a "balanced" BST "Balanced" means that the height of an RB-Tree is at MOST twice the height of a tree Recall, height of tree had an upper bound of log2(n) Thus height or an RB-Tree is bounded by 2*log2n which is still O(log2(n))

25 Red Black and 2-3-4 Tree Correspondence
Every 2-, 3-, and 4-node can be converted to… At least 1 black node and 1 or 2 red children of the black node Red nodes are always ones that would join with their parent to become a 3- or 4-node in a tree a 2-node m m m a 4 Node S = Small M = Median L = Large s m l s l a b c d s l a 3 Node l s a b c or

26 Red Black Trees Below is a tree and how it can be represented as a directly corresponding RB-Tree Notice at most each node expands to 2 level of red/black nodes Q: Thus if the height of the tree was bound by log2n, then the height of an RB-tree is bounded by? A: 2*log2n = O(log2n) 20 20 30 10 25 Equivalent RB-Tree 10 30 25 60 50 80

27 Red-Black Tree Properties
Valid RB-Trees maintain the invariants that… 1. No path from root to leaf has two consecutive red nodes (i.e. a parent and its child cannot both be red) Since red nodes are just the extra values of a 3- or 4-node from trees you can't have 2 consecutive red nodes 2. Every path from leaf to root has the same number of black nodes Recall, trees are full (same height from leaf to root for all paths) Also remember each 2, 3-, or 4- nodes turns into a black node plus 0, 1, or 2 red node children 3. At the end of an operation the root should always be black 4. We can imagine leaf nodes as having 2 non-existent (NULL) black children if it helps

28 Red-Black Insertion Insertion Algorithm: fixTree involves either
1. Insert node into normal BST location (at a leaf location) and color it RED 2a. If the node's parent is black (i.e. the leaf used to be a 2-node) then DONE (i.e. you now have what was a 3- or 4-node) 2b. Else perform fixTree transformations then repeat step 2 on the parent or grandparent (whoever is red) fixTree involves either recoloring or 1 or 2 rotations and recoloring Which case of fixTree you perform depends on the color of the new node's "aunt/uncle" Insert 10 grandparent 30 aunt/ uncle parent 20 40 x 10

29 fixTree Cases 1. G G G P U P U P G U N P U N N G 2. G G P G U P N U P
Recolor P U P U N P G U N P U N c N c a b a b G 2. G G Recolor P G U N P N U P U P U a N a N b c b c Note: For insertion/removal algorithm we consider non-existent leaf nodes as black nodes 3. Recolor Root R R

30 fixTree Cases 4. G P P G N P G P U N G N c U c U N U 5. G G N P U N U
1 Rotate / Recolor Right rotate of P,G P U N G N c U c U a b N c a b c U a b a b 5. G G N 2 Rotates / Recolor Right rotate of N,G & Recolor Left rotate of N,P P U N U P G a N P c a b c U b c a b P G P N G a N U a b c U b c

31 Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Empty Insert 10 Insert 20 Insert 30 Case 4: Left rotate and recolor Violates consec. reds 10 10 10 20 30 20 20 10 30 Insert 15 Insert 25 20 20 20 20 10 30 10 30 10 30 10 30 Case 2: Recolor Case 3: Recolor root 15 15 15 15 25

32 Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Insert 12 20 20 20 30 10 30 10 12 30 12 15 25 25 10 15 25 12 Case 5: Right Rotate… 15 Case 5: … Right Rotate and recolor Insert 5 20 20 12 30 12 30 10 15 25 10 15 25 Case 1: Recolor 5 Recursive call "fix" on 12 but it's parent is black so we're done 5

33 Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Insert 3 20 20 12 30 12 30 10 15 25 5 15 25 5 3 Case 4: Rotate 10 3

34 Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Insert 8 20 20 12 12 30 12 30 5 20 5 15 25 5 15 25 3 10 15 30 3 10 3 10 8 25 8 Case 2: Recolor 8 Case 4: Rotate 12

35 Insertion Exercise 1 Insert 27 12 5 20 G 3 10 15 30 P 8 25 N 27

36 Insertion Exercise 1 G P N This is case 5. Left rotate around P
12 12 5 5 20 G 20 3 10 15 30 3 10 15 27 P 8 25 8 25 30 N 27 This is case 5. Left rotate around P Right rotate around N Recolor

37 Insertion Exercise 2 Insert 40 12 5 20 G 3 10 15 27 P A 8 25 30 N 40

38 Insertion Exercise 2 G P A N Aunt and Parent are the
12 12 5 5 20 20 G 3 10 15 27 3 10 15 27 P A 8 25 30 8 25 30 N 40 40 Aunt and Parent are the same color. So recolor aunt, parent, and grandparent.

39 Insertion Exercise 2 G A P N Aunt and Parent are the
12 12 A P 5 5 20 20 N 3 10 15 27 3 10 15 27 8 25 30 8 25 30 40 40 Aunt and Parent are the same color. So recolor aunt, parent, and grandparent.

40 Insertion Exercise 3 Insert 50 12 5 20 3 10 15 27 G 8 25 30 P 40 N 50

41 Insertion Exercise 3 G P N Remember, empty nodes are black.
12 12 5 5 20 20 3 10 15 27 3 10 15 27 G 8 25 30 8 25 40 P 40 30 50 N 50 Remember, empty nodes are black. Do a left rotation around P and recolor.

42 Insertion Exercise 4 G A P N 12 5 20 3 10 15 27 8 25 40 30 50 45

43 Insertion Exercise 4 G A P N Aunt and Parent are the same color.
12 12 5 5 20 20 3 10 15 27 3 10 15 27 G 8 8 25 40 25 40 A P 30 50 30 50 45 45 N Aunt and Parent are the same color. Just recolor.

44 Insertion Exercise 4 12 G 5 20 A P 3 10 15 27 8 25 40 N 30 50 45

45 Final Result 12 5 27 40 3 10 20 15 25 30 50 8 45

46 Insertion Exercise 5 G A P N 12 5 27 40 3 10 20 8 15 25 30 50 9 45

47 Insertion Exercise 5 12 5 27 40 3 9 20 15 25 30 50 8 10 45

48 RB-Tree Visualization & Links

49 RB Tree Implementation

50 Hints Implement private methods: findMyUncle() AmIaRightChild()
AmIaLeftChild() RightRotate LeftRotate Need to change x's parent, y's parent, b's parent, x's right, y's left, x's parent's left or right, and maybe root (-inf, inf) (-inf, inf) y x Right rotate of x,y (-inf, y) (y, inf) (-inf, x) (x, inf) x c a y Left rotate of y,x (x, y) (y, inf) (-inf, x) (x,y) a b b c

51 Hints You have to fix the tree after insertion if…
Watch out for traversing NULL pointers node->parent->parent However, if you need to fix the tree your grandparent… Cases break down on uncle's color If an uncle doesn't exist (i.e. is NULL), he is (color?)… (-inf, inf) (-inf, inf) y x Right rotate of x,y (-inf, y) (y, inf) (-inf, x) (x, inf) x c a y Left rotate of y,x (x, y) (y, inf) (-inf, x) (x,y) a b b c

52 For Print

53 fixTree Cases 1. G G G P U P U P G U N P U N N G 2. G G P G U P N U P
Recolor P U P U N P G U N P U N c N c a b a b G 2. G G Recolor P G U N P N U P U P U a N a N b c b c Note: For insertion/removal algorithm we consider non-existent leaf nodes as black nodes 3. Recolor Root R R

54 fixTree Cases 4. G P P G N P G P U N G N c U c U N U 5. G G N P U N U
1 Rotate / Recolor Right rotate of P,G P U N G N c U c U a b N c a b c U a b a b 5. G G N 2 Rotates / Recolor Right rotate of N,G & Recolor Left rotate of N,P P U N U P G a N P c a b c U b c a b P G P N G a N U a b c U b c


Download ppt "CSCI Trees and Red/Black Trees"

Similar presentations


Ads by Google