Upcoming SlideShare
×

# Binary Search Trees - AVL and Red Black

2,149 views

Published on

Advanced Data Structures Lecture 3 Slides - AVL Trees, Red Black Trees

CS6213, GWU

Published in: Education, Business, Technology
5 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total views
2,149
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
0
0
Likes
5
Embeds 0
No embeds

No notes for slide

### Binary Search Trees - AVL and Red Black

1. 1. CS 6213 – Advanced Data Structures
2. 2.  Instructor Prof. Amrinder Arora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well  TA Iswarya Parupudi iswarya2291@gwmail.gwu.edu ADS - AVL, Red Black Trees CS6213 - Arora - L3 2
3. 3. Record / Struct Basics Arrays / Linked Lists / Stacks / Queues Graphs / Trees / BSTs Trie, B-Tree CS 6213 Advanced Splay Trees Union Find Generics Applications Multi Threading ADS - AVL, Red Black Trees CS6213 - Arora - L3 3
4. 4.  Objectives  Deletions, Insertions, Searches, all in log n time  Credits:  Prof. Roger Crawfis (Ohio State)  Sarah Buchanan (UCF)  Rafee Memon (CMU) ADS - AVL, Red Black Trees CS6213 - Arora - L3 4
5. 5.  Requiring balancing at the root is not enough. ADS - AVL, Red Black Trees CS6213 - Arora - L3 5
6. 6.  Impose some properties (different properties based on different implementations)  Perform rotations (localized operations) that maintain BST property and adjust the height  Left Rotation, Right Rotation ADS - AVL, Red Black Trees CS6213 - Arora - L3 6
7. 7. Left-Rotate(T, x) x  Right-Rotate(T, y) y  ADS - AVL, Red Black Trees y   x   CS6213 - Arora - L3 7
8. 8.  Rotations are the basic tree-restructuring operation for almost all balanced search trees.  Rotation takes a BST and a node,  Changes pointers to change the local structure, and  Won’t violate the binary-search-tree property.  Left rotation and right rotation are inverses. ADS - AVL, Red Black Trees CS6213 - Arora - L3 8
9. 9. Left-Rotate (T, x) 1. y  right[x] // Set y. 2. right[x]  left[y] //Turn y’s left subtree into x’s right subtree. 3. if left[y]  nil[T ] 4. then p[left[y]]  x 5. p[y]  p[x] // Link x’s parent to y. 6. if p[x] = nil[T ] 7. then root[T ]  y Left-Rotate(T, x) y x 8. else if x = left[p[x]]   Right-Rotate(T, y) x y 9. then left[p[x]]  y     10. else right[p[x]]  y 11. left[y]  x // Put x on y’s left. 12. p[x]  y ADS - AVL, Red Black Trees CS6213 - Arora - L3 9
10. 10.  The pseudo-code for Left-Rotate assumes that  right[x]  nil[T ], and  root’s parent is nil[T ].  Left Rotation on x, makes x the left child of y, and the left subtree of y into the right subtree of x.  Pseudocode for Right-Rotate is symmetric: exchange left and right everywhere.  Time: O(1) for both Left-Rotate and Right-Rotate, since a constant number of pointers are modified. ADS - AVL, Red Black Trees CS6213 - Arora - L3 10
11. 11.  Invented by Adelson-Velskii and Landis.  A binary search tree with a balance condition.  Easy to maintain.  Ensures the depth is O(log n)  Balance condition – for every node in the tree, the height of the left and right subtrees can differ by at most 1. ADS - AVL, Red Black Trees CS6213 - Arora - L3 11
12. 12.  Definition: A tree rooted at node v with L and R as left and right subtrees is an AVL tree if:  | height(L) - height(R)| ≤ 1  L is an AVL tree; and  R is an AVL tree. ADS - AVL, Red Black Trees CS6213 - Arora - L3 12
13. 13.  Are these AVL trees? ADS - AVL, Red Black Trees CS6213 - Arora - L3 13
14. 14. 5 7 8 2 1 4 7 8 2 1 3 4 3 5  We note that both of them are still binary search trees, although not both are AVL trees ADS - AVL, Red Black Trees CS6213 - Arora - L3 14
15. 15.  Mathematical definition: A tree rooted at node n is an AVL tree if:  |height(L) - height(R)| ≤ 1; and  L is an AVL tree; and  R is an AVL tree.  Using this definition, how can we check if a tree is a valid AVL tree?  Definition is recursive, so how about recursively?  How about bottom up traversal? ADS - AVL, Red Black Trees CS6213 - Arora - L3 15
16. 16.  public boolean isAVL: For a tree rooted at node n:  Base case?  Could check for leaf node, but for elegance…  n is null → for simplicity, return valid  Constraint?  Check that heights of sub-trees are within 1 of each other (assume we have a getHeight function)  Recursive case?  Check that left and right sub-trees are AVL trees ADS - AVL, Red Black Trees CS6213 - Arora - L3 16
17. 17. public boolean isAVL(Node n) { if (n == null) return true; // base case int heightL = getHeight(n.leftChild); int heightR = getHeight(n.rightChild); int heightDiff = Math.abs(heightL – heightR); return (isAVL(n.leftChild) && isAVL(n.rightChild) && heightDiff <= 1); } ADS - AVL, Red Black Trees CS6213 - Arora - L3 17
18. 18.  When we do an Insertion  Update all the balancing information for the nodes on the path back to the root.  As we are updating the balance information on the path up to the root, we may find a node whose new balance violates the AVL condition.  What if the insertion violated the AVL tree property?  We do a simple modification to the tree known as a rotation. ADS - AVL, Red Black Trees CS6213 - Arora - L3 18
19. 19.  4 cases, but two are symmetrical with the other two  1) Left outer subtree ↔ 4) Right outer subtree  2) Left inner subtree ↔ 3) Right inner subtree ADS - AVL, Red Black Trees CS6213 - Arora - L3 19
20. 20.  Let us call the node that must be rebalanced α  Since any node has at most 2 children, and a height imbalance requires that α’s 2 subtrees’ height differ by 2, there are 4 violation cases: 1) An insertion into the left subtree of the left child of α. 2) An insertion into the left subtree of the right child of α. 4) An insertion into the right subtree of the right child of α. α An insertion into the right subtree of the left child of α. 3) α ADS - AVL, Red Black Trees α α CS6213 - Arora - L3 20
21. 21.  Outside cases (left-left or right-right), fixed by a single rotation: 1) An insertion into the left subtree of the left child of α. 4) An insertion into the right subtree of the right child of α. 6 6 α 8 2 2 7 α 1 5 7 8 9 4 3 ADS - AVL, Red Black Trees CS6213 - Arora - L3 21
22. 22.  Inside cases (right-left or left-right), fixed by a double rotation: 2) An insertion into the right subtree of the left child of α. 3) An insertion into the left subtree of the right child of α. 7 6 α 9 2 2 7 α 1 6 8 9 8 4 5 ADS - AVL, Red Black Trees CS6213 - Arora - L3 22
23. 23.  We want X up a level and Z down a level.  We want k2 as the new root.  Pretend the tree is flexible, grab hold of k2, letting gravity take hold.  k3 > k2 (by BST property), so k3 becomes k2’s right subtree.  X and Z can remain attached to the same point,  and Y becomes k3’s subtree to maintain BST property. k k 3 2 k k Z 2 k 1 3 X 1 X k Y Y Z X ADS - AVL, Red Black Trees CS6213 - Arora - L3 23
24. 24.  Symmetric case k k 2 1 k X k 1 k 3 2 k Y 3 Z Y Z Z ADS - AVL, Red Black Trees CS6213 - Arora - L3 24
25. 25.  Place k2 as the new root.  This forces k1 to be k2’s left child and k3 to be its right child.  Then the 4 subtrees A,B,C,D fall according to BST rules. k k 3 k 3 2 k k Z 1 1 k X 2 Y ADS - AVL, Red Black Trees k k 1 D 3 k A A 2 B B C D C CS6213 - Arora - L3 25
26. 26.  A Double Rotation is like 2 single rotations.  Example: Left-right double rotation. k Single Left Rotation at K1 3 k D 2 k k 2 A 3 k D 1 k 1 B C A B C k 2 k 1 ADS - AVL, Red Black Trees k 3 CS6213 - Arora - L3 A B C 26 D
27. 27.  In an insert there is at most one node that needs to be rebalanced.  But in a delete there may be multiple nodes to be rebalanced.  Technically only one rebalance that happens at a node, but once that happens it may affect the ancestral nodes. ADS - AVL, Red Black Trees CS6213 - Arora - L3 27
28. 28.  If the node is a leaf, remove it.  Retrace back up the tree starting with the parent of deleted node to the root, adjusting the balance factor as needed.  If it’s not a leaf, replace with the largest in its left subtree (inorder predecessor) and remove that node.  You could also replace with the smallest in its right subtree (inorder successor)  If the predecessor was not a leaf, you may need to adjust the pointers.  After deletion, retrace the path back up the tree, starting with the parent of the replacement, to the root, adjusting the balance factor as needed. ADS - AVL, Red Black Trees CS6213 - Arora - L3 28
29. 29. 32 16 8 48 24 40 28 36 56 44 52 60 58 ADS - AVL, Red Black Trees 62 CS6213 - Arora - L3 29
30. 30. 32 16 48 24 40 28 36 56 44 52 60 58 ADS - AVL, Red Black Trees 62 CS6213 - Arora - L3 30
31. 31. 32 24 16 48 28 40 36 56 44 52 60 58 ADS - AVL, Red Black Trees 62 CS6213 - Arora - L3 31
32. 32. 48 32 56 24 16 ADS - AVL, Red Black Trees 52 40 28 36 44 60 58 62 CS6213 - Arora - L3 32
33. 33.  Actual Implementation for Binary Search Trees  O(log n) average height – Why is that? (We didn’t prove it for sure.)  If we initially have an AVL tree:  Insertion requires at most 2 rotations  Deletion requires at most O(log n) rotations ADS - AVL, Red Black Trees CS6213 - Arora - L3 33
34. 34.  A variation of binary search trees to ensure that the tree is somewhat balanced.  Height is O(lg n), where n is the number of nodes.  Operations take O(lg n) time in the worst case.  Red-Black Tree Properties:  Every node is either red or black.  The root is black.  Every leaf (nil) is black.  If a node is red, then both its children are black.  For each node, all paths from the node to descendant leaves contain the same number of black nodes. ADS - AVL, Red Black Trees CS6213 - Arora - L3 34
35. 35.  Binary search tree + 1 bit per node: the attribute color, which is either red or black.  All other attributes of BSTs are inherited:  key, left, right, and p.  All empty trees (leaves) are colored black.  We use a single sentinel, nil, for all the leaves of red-black tree T, with color[nil] = black.  The root’s parent is also nil[T] ADS - AVL, Red Black Trees CS6213 - Arora - L3 35
36. 36. 26 Remember: every internal node has two children, even though nil leaves are not usually shown. 41 17 30 47 38 50 nil[T] ADS - AVL, Red Black Trees CS6213 - Arora - L3 36
37. 37.  Height of a node:  h(x) = number of edges in a longest path to a leaf.  Black-height of a node x, bh(x):  bh(x) = number of black nodes (including nil[T ]) on the path from x to leaf, not counting x.  Black-height of a red-black tree is the black-height of its root.  By Property 5, black height is well defined. ADS - AVL, Red Black Trees CS6213 - Arora - L3 37
38. 38. h=4 26 bh=2  Height of a node: h(x) = # of edges in a longest path to a leaf.  Black-height of a node bh(x) = # 17 h=1 bh=1 h=3 41 bh=2 of black nodes on path from x to leaf, not counting x.  How are they related?  bh(x) h=2 30 bh=1 ≤ h(x) ≤ 2 bh(x) h=1 bh=1 38 ADS - AVL, Red Black Trees nil[T] h=2 47 bh=1 h=1 50 bh=1 CS6213 - Arora - L3 38
39. 39. Consider a node x in an RB tree: The longest descending path from x to a leaf has length h(x), which is at most twice the length of the shortest descending path from x to a leaf. Proof: # black nodes on any path from x = bh(x) (prop 5)  # nodes on shortest path from x, s(x). (prop 1) But, there are no consecutive red (prop 4), and we end with black (prop 3), so h(x) ≤ 2 bh(x). Thus, h(x) ≤ 2 s(x). QED ADS - AVL, Red Black Trees CS6213 - Arora - L3 39
40. 40.  Lemma 13.1: A red-black tree with n internal nodes has height at most 2 lg(n+1). ADS - AVL, Red Black Trees CS6213 - Arora - L3 40
41. 41. 1. 2. 3. 4. 5. Every node is either red or black. The root is black. Every leaf (nil) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes. ADS - AVL, Red Black Trees CS6213 - Arora - L3 41
42. 42.  Insertion must preserve all red-black properties.  Should an inserted node be colored Red? Black?  Basic steps:  Use Tree-Insert from BST (slightly modified) to insert a node x into T.  Procedure RB-Insert(x).  Color the node x red.  Fix the modified tree by re-coloring nodes and performing rotation to preserve RB tree property.  Procedure RB-Insert-Fixup. ADS - AVL, Red Black Trees CS6213 - Arora - L3 42
43. 43.  Problem: we may have one pair of consecutive reds where we did the insertion.  Solution: rotate it up the tree and away…  Three cases have to be handled… ADS - AVL, Red Black Trees CS6213 - Arora - L3 43
44. 44. p[p[z]] new z C C p[z] y A D z   A  D   B   z is a right child here. Similar steps if z is a left child.  B    p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red.  Make p[z] and y black  Make p[p[z]] red  now z and p[z] are not both red.  restores property 5.  The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels). ADS - AVL, Red Black Trees CS6213 - Arora - L3 44
45. 45. B C p[z] z  A  A  y B  C      Make p[z] black and p[p[z]] red.  Then right rotate on p[p[z]]. Ensures property 4 is maintained.  No longer have 2 reds in a row.  p[z] is now black ADS - AVL, Red Black Trees  no more iterations. CS6213 - Arora - L3 45
46. 46. C C p[z] p[z]  y A  y B z  z B     Left rotate around p[z], p[z] and z switch roles  A   now z is a left child, and both z and p[z] are red.  This now becomes case 2. ADS - AVL, Red Black Trees CS6213 - Arora - L3 46
47. 47.  Symmetric to cases 2 and 3 in that parent of z is right child. ADS - AVL, Red Black Trees CS6213 - Arora - L3 47
48. 48.  O(lg n) time to get through RB-Insert up to the call of RB-Insert- Fixup.  Within RB-Insert-Fixup:  Each iteration takes O(1) time.  Each iteration but the last moves z up 2 levels.  O(lg n) levels  O(lg n) time.  Thus, insertion in a red-black tree takes O(lg n) time.  Note: there are at most 2 rotations overall. ADS - AVL, Red Black Trees CS6213 - Arora - L3 48
49. 49.  Deletion, like insertion, should preserve all the RB properties.  First do regular BST deletion  Find the node  If not a leaf, find the predecessor  If predecessor is a node with a single child, then adjust the pointers  Now, we need to fix any violations of RB properties that may result.  The properties that may be violated depends on the color of the deleted node.  Red – We are lucky.  Black? We need to consider 3 cases ADS - AVL, Red Black Trees CS6213 - Arora - L3 49
50. 50.  The parent is red, and the children of sibling node are black Recolor parent to black and sibling to red ADS - AVL, Red Black Trees CS6213 - Arora - L3 50
51. 51.  The parent is black, and the children of sibling node are black  Unlike Case 1, we cannot finish in one move, but the problem can be shifted “upwards”. B is now “double black”. ADS - AVL, Red Black Trees CS6213 - Arora - L3 51
52. 52.  The parent is red, and the children of sibling node are not both black  We can do a rotation ADS - AVL, Red Black Trees CS6213 - Arora - L3 52
53. 53.  Deletion of elements in array A ADS - AVL, Red Black Trees CS6213 - Arora - L3 53
54. 54.  Review ArrayList.java source code of Java. ADS - AVL, Red Black Trees CS6213 - Arora - L3 54