red black trees1109

23
R e d B l a c k T r e e s PRAVIN D’SILVA 1109 MCA , GOA UNIVERSITY

Upload: pravin-dsilva

Post on 29-Jun-2015

960 views

Category:

Education


1 download

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

R e dB l a c k

T r e e s

PRAVIN D’SILVA1109

MCA , GOA UNIVERSITY

Page 2: Red black trees1109

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

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

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

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

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

Example of red black trees

Each Red Node can have only Black children

Page 8: Red black trees1109

Not a red black tree

Page 9: Red black trees1109

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

Complexity

•The persistent version of red-black trees requires O(log n) space for each insertion or deletion.

Page 11: Red black trees1109

Some operations in log(n)

•Search, minimum, maximum, successor, predecessor.

•Let us discuss insert and delete.

Page 12: Red black trees1109

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

•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

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

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

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

a

b

Consider a red black

cc

a

b

cc

Page 18: Red black trees1109

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

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

Case 3

a

b

a

b

h-1 h-1

h-1

h-1 h-1

h-1

Page 21: Red black trees1109

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

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

Thank You