370 likes | 510 Vues
This article explores Red-Black Trees, a type of self-balancing binary search tree that maintains efficient data structuring. It covers fundamental concepts such as properties, node coloring, rotations, and operations like insertion and deletion. Red-Black Trees ensure that the longest path from root to leaf is no more than twice as long as the shortest, significantly improving search, insert, and delete operations. The text offers detailed examples, operational steps for maintaining properties, and highlights the importance of balancing in data structures.
E N D
Group Members • Bushra Shabbir SP10-BCS-020 • Mehreen Arif SP10-BCS-040 • Misbah Kiran SP10-BCS-042
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.
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:
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.
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.
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)
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.
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.
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.
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.
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.
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
Insert 2 1 2 make 2 red. Parent is black so done. 14
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
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
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
Finish insert of 5 2 1 4 3 5 18
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
Finishing insert of 6 2 1 4 3 5 6 6's parent is blackso done. 20
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
Finish insert of 7 2 1 4 3 6 5 7 22
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
Still Inserting 8 2 1 4 3 6 5 7 Re-colored nowneed torotate 24
Finish inserting 8 4 2 3 6 5 7 1 8 Again Re-colored 25
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
Finish Inserting 9 4 2 3 6 5 8 1 7 9 After rotations and re-coloring 27
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
Insert 11 4 2 3 6 5 8 1 7 9 10 11 Again a rotation isneeded. 29
Finish inserting 11 4 2 3 6 5 8 1 7 10 9 11 30
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.