1 / 18

Analysis of Red-Black Tree

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.

zoey
Télécharger la présentation

Analysis of Red-Black 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. 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. X Y C 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. A D B 1 4 5 2 3

  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 X represents the newly inserted node Case 1a) x’s uncle is Red, and x is right child. New x C C y A D A D x B B 1 4 5 1 4 5 2 3 2 3

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

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

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

  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. B D Single rotation and Recolor. x w E B D A x w 5 6 A C C E 1 2 1 2 3 4 3 4 5 6 X stays at the same black height, Continue down the tree.

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

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

  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. B B w w D C x x A A E C 3 D 1 2 1 2 4 3 4 5 6 E 5 6 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 D x w D A B E A C 1 2 C E 5 6 1 2 3 4 3 4 5 6 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 Great Demo that shows step by step Insertion and Deletion. Also contains Java source code. http://gauss.ececs.uc.edu/RedBlackMozilla/redblack.html

  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.

More Related