Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Module 3

Advanced Data Structure


RED BLACK TREES
Introduction

Need Of Red-Black Tree

R-B Tree is a balanced BST

Binary Search Tree

Is this a BST?

BST

Compiled by Kaajal SharmaPage 1


Right Skew/Left Skew

Compiled by Kaajal SharmaPage 2


We need a self balanced tree,Red-Black Tree is a self balanced tree

But AVL is also self balanced

AVL gurantees time conplexity O(logn) for all cases

So Why Red-Black Tree

if tree is large many rotations are required to balance the tree

-Red-Black tree maximum 2 rotations required /some

Compiled by Kaajal SharmaPage 3


-sometimes recolouring is also sufficient to balance

R-B Tree AVL

roughly height balanced strictly height balanced

insertion/deletion faster searching is faster

few rotations rotations are more

Depends on the situation which tree to use

properties of Red-Black Tree

-It is a self balancing BST

-Every node is coloured either Red or Black

-The root node is always Black

-Every leaf which is null is black

-If a node is Red its children must be Black(the red rule)

-Every path from a node to a null link must contain the same number of black
nodes(the path rule)

-The leaves of Red-Black tree would always be black

Compiled by Kaajal SharmaPage 4


Question is ,is AVL tree a R-B tree

*AVL tree is a subset of Red-Black tree (if we colour AVL tree and see) but
every Red-Black tree is not a AVL tree because RB is roughly height
balanced

Examples

Is it a red black tree?

Compiled by Kaajal SharmaPage 5


1)

2)

Compiled by Kaajal SharmaPage 6


3)

4)

Compiled by Kaajal SharmaPage 7


-its a R-B tree

-Add one node value 1,colour B

Violates the rule

Change the colour to Red,property is set,now it is a R-B Tree

-Add one more node,since parent is Red ,child will be black,according to


property,but it violates path rule,so new node cannot be added.

(one more property)

Compiled by Kaajal SharmaPage 8


*The longest path from the root node is no more than twice the shortest
path.

5)

Compiled by Kaajal SharmaPage 9


Compiled by Kaajal SharmaPage 10

You might also like