Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:

Similar presentations


Presentation on theme: "Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:"— Presentation transcript:

1 Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis: 1. Finding any node will take O(log N) time. 2. Inserting any node will take O(log N) time. 3. Deleting any node will take O(log N) time.

2 What is a Red-Black Tree? Other than being a Binary Search Tree it has these special coloring properties: 1. Every node is colored either red or black. 2. The root is always black. 3. If a node is red, its children must be black. 4. Every path from a node to a null reference must contain the same number of black nodes. Yes rule #4 is somewhat confusing Don’t Panic we will go over it

3 Rule #4 Every path from a node to a null reference must contain the same number of black nodes. B DA C 1 23 45 X Y 1. Side X you count 1 black node to any null reference. 2. Side Y you count 1 black node to any null reference. This Balance must be maintained through out the tree.

4 More on Coloring 1. On any path from the root to a leaf, red nodes must not be adjacent. 2. However, any number of black nodes may appear in a sequence. 3. Every time you add a node it begins as red. 4. All null references are defined to be black.

5 Now to Insert a node Case 1a) x’s uncle is Red, and x is right child. B DA C 1 23 45 x y B DA C 1 23 45 New x X represents the newly inserted node

6 Insertion A DB C 12 3 45 x y A DB C 12 3 45 New x Case 1b) x’s uncle is Red, and x is left child Note: For all Cases when x is root change x to black.

7 Insertion Case 2) x’s uncle is Black, and x is a right child. Note: Triangles represent Null reference. C B A C B A 12 44 3 1 32 x Left rotation of A

8 Insertion Case 3) x’s uncle is Black, x is left child. C B A 12 4 3 B C A 1 2 4 3 Right rotation and recolor.

9 When Inserting a Node Remember: 1. Insert nodes one at a time, and after every Insertion balance the tree. 2. Every node inserted starts as a Red node. 3. Consult the cases, Every time two Red nodes touch must rebalance at that point. 4. The root will always be Black.

10 Deleting a Node And you thought Inserting a Node was fun, lets try Deleting. Rules: If we delete a Red node, tree is Still a Red-Black tree. Assume we delete a Black node: Let x be the child of the deleted node and w be its sibling. If (x is Red, change it to Black and stop) Else If (x is Black, mark it double black and apply the following)

11 Deleting a Node Case 1) x’s sibling is red. 65 2143 654 12 3 AC D E B C A E D B X stays at the same black height, Continue down the tree. x x w w Single rotation and Recolor.

12 Deleting a Node Case 2a) x’s sibling is black, x’s parent is black 6543 12 B D C A E 6543 12 B C A E D x Re-color w, Decrease x height by one and continue up tree. w x

13 Deleting a Node Case 2b) x’s sibling is black, x’s parent is red. 6543 12 B C A E D 6543 12 D C A E B Swap x’s parent and x’s, siblings color. Terminal case is Red-Black Tree. x w

14 Deleting a Node Case 3) x’s parent is either, x’s sibling is black, x’s siblings left child is red, x’s siblings right child is black. 6543 12 D B A E C x w 65 4 3 12 C B A E D x w X’s black height stays same, change to case 4.

15 Deleting a Node Case 4) x’s parent is either, x’s sibling is black, x’s siblings left child is either, x’s siblings right child is red. B C B D E D AE AC 65 43 6543 12 12 xw Terminal case, tree is Red-Black tree.

16 Deleting a Node To deleting a node that has two children, we replace the deleted node with the smallest node on its right sub-tree. You cannot use given cases unless the node to be deleted has one or fewer children.

17 Java Demo http://gauss.ececs.uc.edu/RedBlackMozilla/redblack.html Great Demo that shows step by step Insertion and Deletion. Also contains Java source code.

18 Review for Quiz Insertion: 1. When building a tree, insert One node at a time. 2. After inserting a node, consult your cases and balance the tree. Deletion: 3. After deleting a node, consult cases, and balance tree. Remember: 4. No two Red nodes can touch. 5. All paths through tree must contain same number of Black nodes. 6. Must always have Binary Search Tree balance.


Download ppt "Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:"

Similar presentations


Ads by Google