1 / 31

RED BLACK TREE

Learn about red-black trees, a type of self-balancing binary search tree, that maintain balance and efficient search, insert, and delete operations. Explore the properties, rotations, and examples of insertion and deletion.

adamsk
Télécharger la présentation

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. RED BLACK TREE

  2. Group Members • Bushra Shabbir SP10-BCS-020 • Mehreen Arif SP10-BCS-040 • Misbah Kiran SP10-BCS-042

  3. Introduction • Binary Tree: is a tree structure in which each node has at most 2 children. • Binary Search Tree: Each node has a value, where the left sub-tree node contains only values lesser or equal to it’s parent, and the right sub-tree contains nodes of greater or equal value to it’s parents.

  4. 1 2 3 4 5 Binary Search Tree • In the case of a Binary Search Tree search, insert and delete all have a running time in the order of O (log n) for n items in best case. • A Binary Search tree is not self balancing, so in worst case its running time is O (n). As in case of first 5 integers:

  5. 11 2 12 1 7 15 5 8 Balanced Search Tree • Balanced Means: the longest path is no more then twice as long as the shortest path.

  6. Red Black Tree • A red black tree is a type of binary search tree that is self balancing. Red-black trees aim to keep the tree balanced by • Coloring each node in the tree with either red or black • Preserving a set of properties that guarantee that the deepest path in the tree is not longer than twice the shortest one.

  7. Properties • A red-black tree has the following properties: • Every node is colored either Red or Black. • By convention, the root is always Black • Each NULL pointer is considered to be a Black “node”. • If a node is Red, then both of its children are Black. (If not then Red Violation Occurs) • Every path from a node to a NULL contains the same number of Black nodes. (If not then Black Violation occurs)

  8. Height of RBT • Height of a node: • H (x) = number of edges in a longest path to a leaf. • Black-height of a node x, bh (x): • Bh (x)= number of black nodes (including nil)on the path from x to leaf, not counting x. • Black-height of a red-black tree is the black-height of its root.

  9. Rotation • Rotation needs to be talked about before Insert and Delete. • Operations (Tree-Insert and Tree-Delete) can destroy the properties of a red black tree. To restore sometimes pointers in the tree need to be change. This is called “Rotation”. • There are 2 types of rotations, left and right. Note: Sometimes to restore the red black properties, some nodes have to have their colors changed from red to black or black to red.

  10. X Y z Y X V Z W V W Left Rotation Here are the steps involved in for left rotation: • Assume node x is the parent and node y is a non-leaf right child. • Let y be the parent and x be its left child. • Let y’s left child be x’s right child.

  11. Y X z X Y V Z W V W Right Rotation Here are the steps involved in for right rotation: • Assume node x is the parent and node y is a non-leaf left child. • Let y be the parent and x be its right child. • Let y’s right child be x’s left child.

  12. Insertion Insertion can be done in the order of O (log n). It follows closely the insertion into a binary search tree, except • The Node inserted is red • A restore function must be called to fix red-black properties that might be violated.

  13. Example of Inserting Sorted Numbers 1 2 3 4 5 6 7 8 9 10 11 1 1 Insert 1. A leaf so red. Realize it is root so recolor to black. 13

  14. Insert 2 1 2 make 2 red. Parent is black so done. 14

  15. Insert 3 1 2 3 2 1 3 Insert 3. Parent is red. Parent's sibling is black(null) 3 is outside relative to grandparent. Rotate parent and grandparent 15

  16. Insert 4 2 1 3 2 1 3 4 On way down see2 with 2 red children.Recolor 2 red andchildren black.Realize 2 is rootso color back to black When adding 4parent is blackso done. 16

  17. Insert 5 2 1 3 4 5 5's parent is red.Parent's sibling isblack (null). 5 isoutside relative tograndparent (3) so rotateparent and grandparent thenrecolor 17

  18. Finish insert of 5 2 1 4 3 5 18

  19. Insert 6 2 1 4 3 5 On way down see4 with 2 redchildren. Make4 red and childrenblack. 4's parent isblack so no problem. 19

  20. Finishing insert of 6 2 1 4 3 5 6 6's parent is blackso done. 20

  21. Insert 7 2 1 4 3 5 6 7 7's parent is red.Parent's sibling isblack (null). 7 isoutside relative tograndparent (5) so rotate parent and grandparent then recolor 21

  22. Finish insert of 7 2 1 4 3 6 5 7 22

  23. Insert 8 2 1 4 3 6 5 7 On way down see 6with 2 red children.Make 6 red andchildren black. Thiscreates a problembecause 6's parent, 4, isalso red. Must performrotation. 23

  24. Still Inserting 8 2 1 4 3 6 5 7 Re-colored nowneed torotate 24

  25. Finish inserting 8 4 2 3 6 5 7 1 8 Again Re-colored 25

  26. Insert 9 4 2 3 6 5 7 1 8 9 On way down see 4 has two red childrenso recolor 4 red and children black. Realize 4 is the root so recolor black 26

  27. Finish Inserting 9 4 2 3 6 5 8 1 7 9 After rotations and re-coloring 27

  28. Insert 10 4 2 3 6 5 8 1 7 9 10 On way down see 8 has twored children so change 8 tored and children black 28

  29. Insert 11 4 2 3 6 5 8 1 7 9 10 11 Again a rotation isneeded. 29

  30. Finish inserting 11 4 2 3 6 5 8 1 7 10 9 11 30

  31. Deletion • Deletion, like insertion, should preserve all the RBT properties. • The properties that may be violated depends on the color of the deleted node. • Red – OK. Why? • Black?Black Violation • Steps: • Do regular BST deletion. • Fix any violations of RBT properties that may result.

More Related