1 / 19

Lecture 23 Red Black Tree Chapter 10 of textbook 1. Binary Search Tree (BST) Threaded Tree

Lecture 23 Red Black Tree Chapter 10 of textbook 1. Binary Search Tree (BST) Threaded Tree AVL Tree Red Black Tree (RBT) Splay Tree. 4. Red Black Trees.

pmae
Download Presentation

Lecture 23 Red Black Tree Chapter 10 of textbook 1. Binary Search Tree (BST) Threaded Tree

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 23 Red Black Tree Chapter 10 of textbook 1. Binary Search Tree (BST) • Threaded Tree • AVL Tree • Red Black Tree (RBT) • Splay Tree

  2. 4. Red Black Trees • A Red-Black Tree (RBT) is a self-balancing BST in which every node has a color of red or black, and satisfying five properties: • The color of a node is either red or black. • The color of the root node is always black. • All leaf nodes are black. • Every red node has both the children colored in black. • Every simple path from a given node to any of its leaf nodes has equal number of black nodes.

  3. Example of Red Black Trees 16 9 27 7 11 21 45 NULL 8 NULL NULL NULL NULL 36 63 NULL NULL NULL NULL NULL NULL

  4. Property of Red Black Trees • Property: the longest path from the root node to any leaf node is no more than twice as long as the shortest path from the root to any other leaf in that tree. • The height of red black of n nodes is O(log n) • Search RBT of n nodes, can be done in time O(log n)

  5. Proof of the property 16 7 11 21 45 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL • Let T denote the RBT, merge all red nodes to their parents, derive T’h’=height(T’) >= height(T) /2 = h/2n’ = node_count(T’) <= node_count(T) <= n • All leaves are of the same level number, non-leaf node has degree 2,3,4. T’ has more nodes than full binary tree of height h’ n > = n’ >= 2h’ >= 2h/2 h/2 <= log n, h = 2 log n , h = O(log n)

  6. Inserting a Node in a Red Black Tree • Inserting new node into RBT as BST and set the new node red. If the resulting colored BST is not RBT, the do property restoring (rebalancing) depending on following cases: Case 1: The new node N is added as the root of the tree. N is repainted black as the root of the tree is always black. Since N adds one black node to every path at once, Property 5 that says all paths from any given node to its leaf nodes has equal number of black nodes is not violated.

  7. Inserting a Node in a Red Black Tree Case 2: The new node's parent P is blackThe derived tree is a RBT. Why?Both children of every red node are black, so property 4 is not invalidated. Property 5: the new node N has two black leaf children, but because N is red, the paths through each of its children have the same number of black nodes.

  8. Inserting a Node in a Red Black Tree G G P U P U N N C D E C D E A? B A? B Case 3: If both the parent (P) and the uncle (U) are red Repaint the both nodes (P and U) black, and repaint grandparent G red. If the parent of G is red, go back to case 1 taking G as new node

  9. Inserting a Node in a Red Black Tree G G N U P U N P A? D? E C E B? C A? B Case 4: The parent P is red and the uncle U is black and N is the right child of P and P is the left child of G Do a left rotation at P, paint U read, then go to case 3

  10. Inserting a Node in a Red Black Tree G P P U N G U N C D E B C A? D E A? B Case 5: The parent P is red and the uncle U is black and the new node N is the left child of P, and P is the left child of its parent G. Do a right rotation on G, repaint P black, and G red.

  11. Example of inserting Inserting Insert 9 8 7 3 5 2 into a red-black tree in that order.

  12. Deleting a Node from a Red Black Tree • While deleting a node, if its color is red then we can simply replace it with its child, which must be black. All paths through the deleted node will simply pass through one less red node, and both the deleted nodes’ parent and child must be black, so none of the properties will be violated. • Another simple case is when we delete a black node that has a red child. In this case property 4 (both children of every red node are black) and property 5 (all paths from any given node to its leaf nodes has equal number of black nodes) could be violated, so to restore them just repaint the deleted node’s child with black.

  13. Deleting a Node from a Red Black Tree • Complex situation arises when both the nodes to be deleted as well as its child are black. In this case, we begin by replacing the node to be deleted with its child. This will violate property 5 which states that all paths from any given node to its leaf nodes have equal number of black nodes. Therefore, the tree needs to be rebalanced. There are several cases to consider. Case 1: N is the new root. In this case, we have removed one black node from every path, and the new root is black, so none of the properties are violated.

  14. Deleting a Node from a Red Black Tree S P P SR N S N SL SL SR D E C A? B A? B C D D E Case 2: Sibling S is red. In this case, interchange the colors of P and S, and then rotate left at P. In the resultant tree, S will become N's grandparent.

  15. Deleting a Node from a Red Black Tree P P N S N S SL SR SL SR A? B A? B D E F G D E F G Case 3: P, S, and S's children are black. Repaint S with red color. rebalancing procedure on P, starting at case 1.

  16. Deleting a node from a Red Black Tree P P N S N S SL SR SL SR A? B A? B D E F G D E F G Case 4: If S and S's children are black, but P is red. Interchange the colors of S and P. Although this will not affect the number of black nodes on paths going through S, but it will add one black node to the paths going through N, making up for the deleted black node on those paths.

  17. Deleting a Node from a Red Black Tree SL S S D SR SL SR E F G D E F G Case 5: If N is the left child of P and S is black, S’s left child is red, S's right child is black. Do a right rotation at S. After the rotation, S's left child becomes S's parent and N's new sibling. Also interchange the colors of S and its new parent. • Note that now all paths still have equal number of black nodes, but N has a black sibling whose right child is red, so we fall into case 6. P P D N N D

  18. Deleting a Node from a Red Black Tree S P P N S N D E SR C C A? B A? B D E Case 6: S is black, S's right child is red, and N is the left child of its parent P. Do a left rotation is done at P to make S the parent of P and S's right child. After the rotation, the colors of P and S are interchanged and S’s right child is colored black. SR

  19. Example of deleting from RBT Delete 2, 5, 3, 7, 8, 9 from the RBT

More Related