Red black trees1109

Preview:

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.

Citation preview

R e dB l a c k

T r e e s

PRAVIN D’SILVA1109

MCA , GOA UNIVERSITY

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.

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.

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.

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.

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

Example of red black trees

Each Red Node can have only Black children

Not a red black tree

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

Complexity

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

Some operations in log(n)

•Search, minimum, maximum, successor, predecessor.

•Let us discuss insert and delete.

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.

•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

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

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

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

a

b

Consider a red black

cc

a

b

cc

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

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

Case 3

a

b

a

b

h-1 h-1

h-1

h-1 h-1

h-1

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.

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

Thank You

Recommended