1 / 29

Lecture 12: Balanced Binary Search Trees

Lecture 12: Balanced Binary Search Trees. Shang-Hua Teng. 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

karim
Download Presentation

Lecture 12: Balanced Binary Search Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. y x x A y A B B C 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 rightRotate(y) C leftRotate(x)

  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 15 70 85 60 20 10 80 90 50 65 5 40 55

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

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

  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 2B- 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 • Find the location for the target node, x. • 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. 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 new z C case 1 A D B    Insert: Case 1 C A D y B 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

  15. 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 Insert: Case 2 C case 2 C y A  y B  z  B A  z     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. 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 Insert: Case 3 C case 3 B B  y z A C z A        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 b d d w e a x b x a c c e new w

  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 b b new x d w d a x a c e c e for nodes that may be either red or black

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

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

  28. Red-black trees -- Delete 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 b b c new w a d w a x d c e e

  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.

More Related