Upload
gyles-manning
View
220
Download
0
Embed Size (px)
DESCRIPTION
RED BLACK TREES RB trees is a Binary Tree with one extra bit of storage per node; its color, which can be either RED or BLACK. Each node of the tree contains fields color, key, left, right, parent. Red-Black Trees ensure that longest path is no more than twice as long as any other path so that the tree is approximately BALANCED.
Citation preview
CSE - 5311 Advanced Algorithms
Instructor : Gautam Das
Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi
Contents
RED – BLACK Trees History Properties Rotations Insertion
Hashing Union Find Algorithm
RED BLACK TREES RB trees is a Binary Tree with one extra bit of storage
per node; its color, which can be either RED or BLACK.
Each node of the tree contains fields color, key, left, right, parent.
Red-Black Trees ensure that longest path is no more than twice as long as any other path so that the tree is approximately BALANCED.
RED BLACK TREESSTRUCTURAL PROPERTIES:
Every node is colored red or black. The Root is Black. Every “leaf” (Nil) is colored black. Both children of a red node are black. Every simple path from a child of node X
to a leaf has the same number of black nodes.
RED BLACK TREESPoints to remember: This number is known as the black-height
of X(bh(X)). A RB tree with n internal nodes has a
height of almost 2log(n+1). Maximum path length is O(log n). Finding an element is real quick in RB
trees, i.e,., it takes O(log n) time. Insertion and Deletion take O(log n) time.
RED BLACK TREESROTATIONS Insertion and Deletion modify the tree, the
result may violate the properties of red black trees. To restore these properties rotations are done.
We can have either LEFT rotation or RIGHT rotation by which we must change colors of some of the nodes in the tree and also change the pointer structure.
RED BLACK TREES LEFT ROTATE
RIGHT ROTATE
a c
a b b c When we do a LEFT rotation on a node x we assume that its right child
y is not nil ,i.e x may be any node in the tree whose right child is not Nil.
It makes y the new root of the sub tree, with x as y’s left child and y’s left child as x’s right child.
x
y
y
x
RED BLACK TREES The Idea to insert is that we traverse the
tree to see where it fits , assuming that it fits at the end, and initially color the inserted node RED, then traverse up again.
Coloring rule while insertion If the father node and uncle node of the
inserted node are red then make father and uncle as BLACK and grand father as RED.
RED BLACK TREES
C
A D
B
a
b c
d e
C
DA
a
b c
d eB
After Recoloring
Case 1a: Father and Uncle are Red , problem node is right child
Recolor it to
BLACK
RED BLACK TREESCase 1b: Father and Uncle are Red , problem node is left child
C
B D
A
a b
c d e
C
B D
A
a b
cd e
After Recoloring
Recolor it to
BLACK
RED BLACK TREESCase 2a: Father red and Uncle Black, problem node is left child
C
B
A
a b
d
B
CA
d
cba
After Rotation
c
D
D
RED BLACK TREESCase 2b: Father red and Uncle Black, problem node is right child
C
A
B
a
b
d
C
A
B
dc
ba
After Rotation
c
D D
apply 2a for the above tree
RED BLACK TREES
11
2 14
1
4
7 15
5 8
11
2 14
1
4
7 15
5 8InsertNode
4
Apply
Case 1b
Example:
RED BLACK TREES
11
2 14
1
4
7 15
5 8
11
7 14
2
4
8 15
51
Apply
Case 2b
RED BLACK TREES
11
7 14
2
4
8 15
51
7
2 11
1
4 15
145 8
Apply
Case 2a
HASHING Has table is an effective data structure for
implementing dictionaries. Although searching for an element in hash table in
the worst case is Θ(n) time, under reasonable assumptions the expected time to search for an element is O(1).
With hashing this element is stored in slot h(k) i.e we use a hash function h to compute the slot
from the key k. (h maps the universe U of keys into the slots of a hash table T[0…m-1])
h :U {0,1,2…..m-1} Two keys may hash to the same slot. This is
called collision.
UNION FIND
Basics: Applications involve grouping n elements into a collection
of disjoint sets. The important operations are
MAKE-SET(x): Creates a new set UNION(x,y): Unites the dynamic sets that contain x and y into a
new set that is the union of these two sets. FIND-SET(x): Returns a pointer to the representative of the set
containing x The number of union operations is atmost n-1.
MAKE-SET OPERATION Makes a singleton set Every set should have a name which should be any
element of the set Make-Set(1) Make-Set(2) * * * * *
Make-Set(n)
UNION OPERATION
Initially each number is a set by itself. From n singleton sets gradually merge to form a set. After n-1 union operations we get a single set of n
numbers.
1 3
2
4
UNION :Merge two sets and create a new set
FIND OPERATION
Every set has a name. Thus Find(number) returns name of the
set. It can be performed any number of times
on the sets. The time taken for a find operation is O(n)
whereas for Union operation it is O(1).
LINKED LIST REPRESENTATION
Every Set is represented as linked list where the first element is the name of the set.
We have the array of elements which have pointers to the elements in the linked lists.
5 1
3 4 7
10
5 2
1 3
4 7
10
2
‘3’ is head of this set
‘5’ is head if this set
’10’ is head of this singleton set
For ‘n’ Unions and ‘m’ Finds, then time taken is n+mn.
If we have a pointer from each element in the set to the head, then the time to find operation is O(1).
NOTE: If m is large, Find : O(n+mn) Union: O(1)
LINKED LIST REPRESENTATION
‘3’ is the head
‘5’ is the head
LINKED LIST REPRESENTATION
The 2 sets are being merged by connecting 5 and 7
Each element pointing to the head i,e ‘3’ in this example
In this case the union takes O(n2+m) time.
If we assume that the smaller set is attached to the end of the larger set in Union operation, then UnionO(n) and Find O(1)
But in the AMORTIZED ANALYSIS, Average time taken for union is O(log n).
So ‘n’ Unions and ‘m’ Finds take (nlog n +m) time.
To guarantee O(log n) time for Union, instead of pointing each element to the main head, point the Heads’ of the individual sets to a main head.
If we consider path lengths to combine two trees(L1 is the path length of tree1 and L2 is the path length of tree2), then
If L1>L2 or L2>L1, path length doesn’t change i.e. It is still the longer path.
If L1=L2, then the path length is L2+1 if the head of tree1 is pointed to head of tree2 L1+1 if the head of tree2 is pointed to head of tree1