Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 23 Red Black Tree Chapter 10 of textbook

Similar presentations


Presentation on theme: "Lecture 23 Red Black Tree Chapter 10 of textbook"— 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 27 9 7 11 NULL 8 21 45 36 63

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) proof: Merge the red nodes to their black parents and get a new tree. Resulting tree will have nodes branching 2, 3, or 4 child. Observation: new tree's black height h' is going to be the same as the black height of the original tree. Observation: new tree's height h' can be at least half of the height of the original tree h. (i.e. we have h' >= h/2) A complete binary tree with height h' has 2^h' -1 internal nodes. remember step 2, our new tree is branching 2,3, or 4 in internal nodes. thus, number of internal nodes of new tree is more than 2^h' -1 the original tree has even more internal nodes than the new tree (since we merged the red nodes to heir black parents). Thus, n >= 2^h' -1 the rest is algebra: n+1 >= 2^h' (take logarithm of both sides) lg(n+1) >= h' (we know that h' >= h/2 from step 4 above) Finally we get 2lg(n+1) >= h. This completes the proof.

5 Proof of the property Let T denote the RBT, merge all red nodes to their parents, derive T’ h’=height(T’) >= height(T) /2 = h/2 n’ = 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) 16 7 11 NULL 21 45

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 black The 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
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 G P U N A? B C D E

9 Inserting a Node in a Red Black Tree
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 G A? N B? C D? E U P B

10 Inserting a Node in a Red Black Tree
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. G P U N A? B C D E

11 Example of inserting Inserting
Insert 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
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. P N S A? B SL C SR D E

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

16 Deleting a node from a Red Black Tree
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. P N S A? B SL SR F G D E

17 Deleting a Node from a Red Black Tree
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. N P N P S SL D SR F G E S SL SR F G D E D D

18 Deleting a Node from a Red Black Tree
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. P N S A? B SR D E C SR

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


Download ppt "Lecture 23 Red Black Tree Chapter 10 of textbook"

Similar presentations


Ads by Google