Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.

Similar presentations


Presentation on theme: "Lecture 12: Balanced Binary Search Trees Shang-Hua Teng."— Presentation transcript:

1 Lecture 12: Balanced Binary Search Trees Shang-Hua Teng

2 Insertion and Deletion on dynamic sets Insert(S,x) –A modifying operation that augments the set S with the element pointed by x Delete –Given a pointer x to an element in the set S, removes x from S –Notice that this operation uses a pointer to an element x, not a key value

3 Querying on dynamic sets Search(S,k) –given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or NIL if no such element belongs S Minimum(S) –on a totally ordered set S that returns a pointer to the element S with the smallest key Maximum(S) Successor(S,x) –Given an element x whose key is from a totally ordered set S, returns a pointer to the next larger element in S, or NIL if x is the maximum element Predecessor(S,x)

4 Binary Search Trees All operations can be supported in – O(h) time, where h = height of tree What is the height of a binary search tree? –worst case: h = O(n) when tree is just a linear string of left or right children Today we’ll see how to maintain h = O(lg n)

5 Restructuring Binary Search Trees: Rotations Our basic operation for changing tree structure is called rotation: Preserves binary search tree properties O(1) time…just changes some pointers C rightRotate(y) leftRotate(x) y x AB x A y BC

6 Balanced Binary Search Tree Apply rotations during insertion and deletion to ensure that the height of the tree is O(lg n). Define a balanced condition for simple maintenance. Example: for each internal node, the heights of the left and right subtree differs by at most 1 –AVL trees (homework) We will discuss a scheme that uses color to balance the tree –Red-black trees

7 Red-Black Tree: Coloring nodes with red and black 30 70 85 5 60 80 10 90 15 20 50 4055 65

8 Properties of Red-Black Trees A Red-Black tree satisfies the following properties: 1Every node is colored either red or black 2The root is black 3Every leaf (NIL, NULL) is black 4If a node is red, both of its children are black. 5Every path from a node to a leaf reference has the same number of black nodes

9 Red-Black Tree 30 70 85 5 60 80 10 90 15 20 50 4055 65

10 Height of Red-Black trees If every path from the root to a null reference contains B black nodes, then there must be at least 2 B - 1 black nodes in the tree. Since the root is black and there cannot be two consecutive red nodes on a path, the height of a red-black tree is at most 2log(N + 1)

11 RB Trees: Worst-Case Time So we’ve proved that a red-black tree has O(lg n) height Corollary: These operations take O(lg n) time: –Minimum(), Maximum() –Successor(), Predecessor() –Search() Insert() and Delete(): –Will also take O(lg n) time –But will need special care since they modify tree

12 Red-black trees -- Insert 1.Find the location for the target node, x. 2.Insert x and color it red. Why red? Maintain the black height property. Potential problem: the parent of x is also red! Method: move this violation up the tree while always maintaining the black height property. Perform rotations and re-colorings as necessary

13 RedBlackTreeInsert(z) treeInsert(z); x->color = RED; // Move red-red up tree, maintaining black height as invariant: while (z!=root and p(z)->color == RED) if (p(z) == p(p(z))->left) y = p(p(z))->right; if (y->color == RED) p(z)->color = BLACK; y->color = BLACK; p(p(z))->color = RED; x = p(p(z)); else // y->color == BLACK if (z = p(z)->right) z = p(z); leftRotate(z); p(z)->color = BLACK; p(p(z))->color = RED; rightRotate(p(p(z))); else // x->p == p(p(z))->right (same as above, but with “right” & “left” exchanged) Case 1: uncle is RED Case 2 Case 3

14 Insert: Case 1 if (y->color == RED) p(z)->color = BLACK; y->color = BLACK; p(p(z))->color = RED; z= p(p(z)); Case 1: “uncle” is red In figures below, all  ’s are equal-black-height subtrees C A D  B   z y  C A D  B  new z Change colors of some nodes, preserving: all downward paths have equal b.h. The while loop now continues with z’s grandparent as the new z case 1

15 Insert: Case 2 if (z == p(z)->right) z = p(z); leftRotate(z); // continue with case 3 code Case 2: –“Uncle” is black –Node z is a right child Transform to case 3 via a left-rotation case 2 B  z C A  y  C B A  z  y  Transform case 2 into case 3 (z is left child) with a left rotation This preserves: all downward paths contain same number of black nodes

16 Insert: Case 3 p(z)->color = BLACK; P(p(z))->color = RED; rightRotate(p(p(z))); Case 3: –“Uncle” is black –Node z is a left child Change colors; rotate right case 3 y C B A  z   B A z  C  Perform some color changes and do a right rotation Again, preserves: all downward paths contain same number of black nodes

17 Red-black trees -- Insert What we described is what happens when p[z] is a left child. The case when it is a right child is symmetrical. Note: –In case 3 we fixed the problem by a single rotation (single or double). –In case 1 we recolored the nodes and the problem may have propagated upwards. –In any case, we need at most one restructuring of the tree at the level. Total insert time : O(lgn)

18 Red-black trees -- Delete Let z be the node that we want to delete. Let y be the actual node that we delete. –if z has at most one non-NIL child, then y is z –if z has two children, then y is z's successor Let x be the child of y. –if y is z's successor, x is its right child (possibly a NIL child) –if y is z, x is its non-NIL child, or NIL

19 RB-Delete The code is similar to the delete operation for a BST References to nil are replaced with the sentinnel nil[T] This allows the assignment in line 7 to be unconditional since the nil is like other nodes If the node y that is removed is black, the fixup routine restores the black height of the tree

20 Red-black trees -- Delete When y is eliminated, x becomes the new child of y's parent. If y was red, no properties are violated. If y was black, we need to restructure the tree Idea! Let's transfer y's blackness to x –if x was red, it becomes black. all properties are now satisfied –if x was black, it becomes doubly black. the red-or-black property is violated!

21 Red-black trees -- Delete To solve the double-black problem: –propagate the extra "blackness" up the tree until we find a red node –then, just make it black -- OR -- we reach the root –then, just eliminate the extra black -- OR -- the problem gets fixed by rotating

22 Parameter x was the removed node’s sole child (may be nil) which has a double black count The loop moves x with its double count up the tree until: –x points to a red node which is colored black –x points to the root and the extra black count is discarded –rotations and recoloring can solve the problem

23 Red-black trees -- Delete There are four cases we need to consider, some of which reduce to others. In all of these cases, the next action depends on the color of x’s sibling, w, and its children.

24 Red-black trees -- Delete Case 1 w is red => it has black children switch colors of w, p[w] and left-rotate p[w] Result: one of the other two cases x w a b c d e x new w a b c d e

25 Red-black trees -- Delete Case 2 w is black with black children Take one black off x and w and add an extra to p[x]. Repeat the whole procedure for p[x] as the new x x w a b c d e for nodes that may be either red or black a b c d e new x

26 Red-black trees -- Delete Case 3 w is black, left[w] is red, right[w] is black Double rotate and recolor x w a b c d e a b c d e

27 Red-black trees -- Delete Case 4 w is black, right[w] is black (we don’t care about left[w]) color[w] = color1 color[b] = color[e] = black left-rotate at b AND WE ARE DONE! x w a b c d e color1 color2 b d color1 c e color2a

28 Red-black trees -- Delete x w a b c d e a b c d e new w Case 3 Notes The first half of the rotation in case 3 is a single rotation that actually results is the situation covered by case 4. Here is exactly what happens in this intermediate step: w is black, left[w] is red, right[w] is black Switch colors of w, left[w] and right rotate w

29 Red-black trees -- Delete Running time of "fixing" algorithm: –Cases 1, 3 terminate after some constant color changes and at most three rotations. –Case 2 may cause us to travel all the way to the root : O(lgn) –Total running time for fixing algorithm: O(lgn) Total time for Delete : O(lgn) –even if the fixing algorithm is not called, it will take O(lgn) if it needs to find the successor.


Download ppt "Lecture 12: Balanced Binary Search Trees Shang-Hua Teng."

Similar presentations


Ads by Google