red black trees1109
DESCRIPTION
A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black. A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.TRANSCRIPT
![Page 1: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/1.jpg)
R e dB l a c k
T r e e s
PRAVIN D’SILVA1109
MCA , GOA UNIVERSITY
![Page 2: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/2.jpg)
Acknowledgement
I would like express my sincere gratitude to Dr. Jyoti Pawar, for the support and guidance as well as the feedback she has provided throughout this study.
![Page 3: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/3.jpg)
Definition
•A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black.
•A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
![Page 4: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/4.jpg)
Balanced binary tree
•A non-empty binary tree T is balanced if:1) Left subtree of T is balanced2) Right subtree of T is balanced3) The difference between heights of left subtree and right subtree is not more than 1.
![Page 5: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/5.jpg)
Red-Black tree
•Recall binary search tree▫Key values in the left subtree <= the node
value▫Key values in the right subtree >= the node
value•Operations:
▫insertion, deletion▫Search, maximum, minimum, successor,
predecessor.▫O(h), h is the height of the tree.
![Page 6: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/6.jpg)
Red-black trees• Definition: a binary tree, satisfying:
1. Every node is red or black2. The root is black3. Every leaf is NIL and is black4. If a node is red, then both its children are
black5. For each node, all paths from the node to
descendant leaves contain the same number of black nodes.
• Purpose: keep the tree balanced.• Other balanced search tree:
▫ AVL tree, 2-3-4 tree, Splay tree, Treap
![Page 7: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/7.jpg)
Example of red black trees
Each Red Node can have only Black children
![Page 8: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/8.jpg)
Not a red black tree
![Page 9: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/9.jpg)
Black height of a red black tree• Black height does not count the root itself.• we use "nil leaves" or "null leaves", which contain no
data and merely serve to indicate where the tree ends
7
318
10
811
22
26 NIL pointer
BH=0
BH=1
BH=1, ignore red nodes!!
BH=1BH=2
BH=2
![Page 10: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/10.jpg)
Complexity
•The persistent version of red-black trees requires O(log n) space for each insertion or deletion.
![Page 11: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/11.jpg)
Some operations in log(n)
•Search, minimum, maximum, successor, predecessor.
•Let us discuss insert and delete.
![Page 12: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/12.jpg)
Inserting in a red black tree
•Let k be the key being inserted.•As in case of a BST, we first search for k;
this gives us the place where we have to insert k.
•We create a new node with key k and insert it at this place
•The new node is colored red.
![Page 13: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/13.jpg)
•Since inserted node is Red , the black height of the tree remains unchanged.
•However if the parent node is also red, then we have a double red problem
kk
NO PROBLEM DOUBLE RED PROBLEM
![Page 14: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/14.jpg)
INSERTION : CASE 1•Parent of node (a) must be black (b).•The other child of (b) is black (c) .•Rotation corrects the defect.
b
c
k
a
a
b
c
k
![Page 15: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/15.jpg)
Insertion case 2•Parent of node (a) is red•Parent of (a) must be black (b)•The other child of (b) is also red (c)
b
c
k
a
c|b|a
k
b
c
k
a
h
hh
h h h h h
h h
h+1
h+1
![Page 16: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/16.jpg)
Deletion
•To delete a node we proceed as in a BST•Hence the node which is deleted is the
parent of an external node•Hence it is either a leaf or parent of a leaf•Steps:
▫Search▫Identify▫Leaf, then delete▫If internal node, find successor or
predecessor, swap, then delete the successor or predecessor
![Page 17: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/17.jpg)
a
b
Consider a red black
cc
a
b
cc
![Page 18: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/18.jpg)
a
b
c
a
b
c
b
ac
c
ab
| | a
| b | c
Case 1
h-1
h-1 h-1
h-1
h-1
h-1
h-1 h-1
h-1h-1h-1h-1
h-1 h-1h-1 h-1
![Page 19: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/19.jpg)
Case 2•If parent is a red node (a)•Then it has a child (b) which must be
black•If (b) has no red child•Recoloring solves the problem
a
b
a
b
h-1 h-1
h-1
h-1 h-1
h-1
![Page 20: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/20.jpg)
Case 3
a
b
a
b
h-1 h-1
h-1
h-1 h-1
h-1
![Page 21: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/21.jpg)
Deletion Summary
•In most cases, deletion can be completed by simple rotation/ coloring.
•In case 3, the height of the subtree reduces and so we need to proceed up the tree
•But in case 3, we only recolor the nodes•Thus, if we proceed up the tree, then we
only need to recolor. Eventually we would do a rotation.
![Page 22: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/22.jpg)
References• "Introduction to Algorithms, Third Edition," by Thomas
H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein.
• Lecture Series on Data Structures and Algorithms by Dr. Naveen Garg, Department of Computer Science and Engineering, IIT Delhi. http://nptel.iitm.ac.in
• Lecture 10: Red-black Trees, Rotations, Insertions, Deletions: http://videolectures.net/mit6046jf05_demaine_lec10/
• http://mitpress.mit.edu/algorithms/solutions/chap13-solutions.pdf
• http://www.cs.purdue.edu/homes/ayg/CS251/slides/chap13c.pdf
• Left leaning Red black trees• http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf• http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
![Page 23: Red black trees1109](https://reader035.vdocuments.us/reader035/viewer/2022081603/559139341a28ab0d498b473f/html5/thumbnails/23.jpg)
Thank You