red black tree
DESCRIPTION
RED BLACK TREE. 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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/1.jpg)
REDRED BLACK TREE BLACK TREE
![Page 2: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/2.jpg)
GroupGroup Members Members
► BushraBushra Shabbir SP10- Shabbir SP10-BCSBCS-020-020► MehreenMehreen Arif SP10- Arif SP10-BCSBCS-040-040► MisbahMisbah Kiran SP10- Kiran SP10-BCSBCS-042-042
![Page 3: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/3.jpg)
IntroductionIntroduction
► Binary Tree:Binary Tree: is a tree structure in which each is a tree structure in which each node has at most 2 children.node has at most 2 children.
► Binary Search Tree:Binary Search Tree: Each node has a value, Each node has a value, where the left sub-tree node contains only where the left sub-tree node contains only values lesser or equal to it’s parent, and the values lesser or equal to it’s parent, and the right sub-tree contains nodes of greater or right sub-tree contains nodes of greater or equal value to it’s parents.equal value to it’s parents.
![Page 4: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/4.jpg)
Binary Search TreeBinary Search Tree
► In the case of a Binary Search Tree In the case of a Binary Search Tree search, search, insert and deleteinsert and delete all have a running time in all have a running time in the order of O (log n) for n items in best the order of O (log n) for n items in best case.case.
► A Binary Search tree is not self balancing, so A Binary Search tree is not self balancing, so in worst case its running time is O (n). As in in worst case its running time is O (n). As in case of first 5 integers: case of first 5 integers:
1
2
3
4
5
![Page 5: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/5.jpg)
Balanced Search TreeBalanced Search Tree
►Balanced MeansBalanced Means:: the longest path is the longest path is no more then twice as long as the no more then twice as long as the shortest path.shortest path.
11
8
1
2
7 15
12
5
![Page 6: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/6.jpg)
Red Red Black TreeBlack Tree
► A A redred black tree black tree is a type of binary search is a type of binary search tree that is self balancing. Red-black trees tree that is self balancing. Red-black trees aim to keep the tree balanced byaim to keep the tree balanced by
► Coloring each node in the tree with either Coloring each node in the tree with either red or blackred or black
► Preserving a set of properties that Preserving a set of properties that guarantee that the deepest path in the tree guarantee that the deepest path in the tree is not longer than twice the shortest one.is not longer than twice the shortest one.
![Page 7: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/7.jpg)
PropertiesProperties►A A redred-black tree has the following -black tree has the following
properties:properties: Every node is colored eitherEvery node is colored either Red Red or Black.or Black. By convention, the root is always BlackBy convention, the root is always Black Each NULL pointer is considered to be a Black Each NULL pointer is considered to be a Black
“node”.“node”. If a node is If a node is RedRed, then both of its children are , then both of its children are
Black. (If not then Black. (If not then Red ViolationRed Violation Occurs) Occurs) Every path from a node to a NULL contains the Every path from a node to a NULL contains the
same number of Black nodes. (If not then Black same number of Black nodes. (If not then Black Violation occurs)Violation occurs)
![Page 8: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/8.jpg)
Height of Height of RRBTBT
►Height of a node:Height of a node: H H ((xx) = number of edges in a longest path ) = number of edges in a longest path
to a leaf.to a leaf.
►Black-height of a node Black-height of a node xx, , bh bh ((xx):): Bh Bh ((xx)) = number of black nodes (including = number of black nodes (including
nil)nil)on the path from on the path from x x to leaf, not counting to leaf, not counting xx..
►Black-height of a red-black tree is the Black-height of a red-black tree is the black-height of its root.black-height of its root.
![Page 9: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/9.jpg)
RotationRotation
► 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.
![Page 10: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/10.jpg)
Left RotationLeft Rotation
Here are the steps involved in for left rotation:Here are the steps involved in for left rotation:► Assume node x is the parent and node y is a Assume node x is the parent and node y is a
non-leaf right child.non-leaf right child.► Let y be the parent and x be its left child.Let y be the parent and x be its left child.
►Let y’s left child be x’s right child.Let y’s left child be x’s right child.
W
z Y
X
V W
X
Z
Y
V
![Page 11: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/11.jpg)
Right RotationRight Rotation
Here are the steps involved in for right Here are the steps involved in for right rotation:rotation:
► Assume node x is the parent and node y is a Assume node x is the parent and node y is a non-leaf left child.non-leaf left child.
► Let y be the parent and x be its right child.Let y be the parent and x be its right child.
►Let y’s right child be x’s left child.Let y’s right child be x’s left child.
W
Y
Z
X
V
W
z X
Y
V
![Page 12: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/12.jpg)
InsertionInsertion
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.
![Page 13: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/13.jpg)
13
Example of Inserting Sorted Example of Inserting Sorted NumbersNumbers
►1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1110 11 1
Insert 1. A leaf so
red. Realize it is
root so recolor
to black.1
![Page 14: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/14.jpg)
14
Insert 2Insert 2
1
2
make 2 red. Parent
is black so done.
![Page 15: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/15.jpg)
15
Insert 3Insert 3
1
2
3
Insert 3. Parent is red. Parent's sibling is black(null) 3 is outside relative to grandparent. Rotate
parent and grandparent2
1 3
![Page 16: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/16.jpg)
16
Insert 4Insert 4
2
1 3
On way down see2 with 2 red children.Recolor 2 red andchildren black.Realize 2 is rootso color back to black
2
1 3
4
When adding 4parent is blackso done.
![Page 17: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/17.jpg)
17
Insert 5Insert 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
![Page 18: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/18.jpg)
18
Finish insert of 5Finish insert of 5
2
1 4
3 5
![Page 19: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/19.jpg)
19
Insert 6Insert 6
2
1 4
3 5
On way down see4 with 2 redchildren. Make4 red and childrenblack. 4's parent isblack so no problem.
![Page 20: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/20.jpg)
20
Finishing insert of 6Finishing insert of 6
2
1 4
3 5
6
6's parent is blackso done.
![Page 21: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/21.jpg)
21
Insert 7Insert 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
![Page 22: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/22.jpg)
22
Finish insert of 7Finish insert of 7
2
1 4
3 6
5 7
![Page 23: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/23.jpg)
23
Insert 8Insert 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.
![Page 24: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/24.jpg)
24
Still Inserting 8Still Inserting 8
2
1 4
3 6
5 7
Re-colored nowneed torotate
![Page 25: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/25.jpg)
25
Finish inserting 8Finish inserting 8
4
2
3
6
5 71
8
Again Re-colored
![Page 26: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/26.jpg)
26
Insert 9Insert 9
4
2
3
6
5 71
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
![Page 27: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/27.jpg)
27
Finish Inserting 9Finish Inserting 9
4
2
3
6
5 81
7 9After rotations and re-coloring
![Page 28: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/28.jpg)
28
Insert 10Insert 10
4
2
3
6
5 81
7 9On way down see 8 has twored children so change 8 tored and children black 10
![Page 29: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/29.jpg)
29
Insert 11Insert 11
4
2
3
6
5 81
7 9
10
11
Again a rotation isneeded.
![Page 30: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/30.jpg)
30
Finish inserting 11Finish inserting 11
4
2
3
6
5 81
7 10
9 11
![Page 31: RED BLACK TREE](https://reader036.vdocuments.mx/reader036/viewer/2022081511/56813152550346895d97cdb7/html5/thumbnails/31.jpg)
DeletionDeletion
► Deletion, like insertion, should preserve all Deletion, like insertion, should preserve all the RBT properties.the RBT properties.
► The properties that may be violated The properties that may be violated depends on the color of the deleted node.depends on the color of the deleted node. RedRed – OK. – OK. Why?Why? Black?Black? Black ViolationBlack Violation
► Steps:Steps: Do regular BST deletion.Do regular BST deletion. Fix any violations of Fix any violations of RRBT properties that BT properties that
may result.may result.