11 DS and Algorithm Tree
11 DS and Algorithm Tree
Objectives
In this session, you will learn to:
Implement a threaded binary tree
Implement a height balanced binary tree
Store data in a graph
Ver. 1.0
Session 16
Ver. 1.0
Session 16
node
toheader
locate
the
Algorithm
node
to be deleted and its parent in
a threaded binary tree.
2.
3.
. 65 .
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
header node
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
parent
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
parent
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
header node
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
parent
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
header node
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
currentNode
. 65 .
parent
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
2.
3.
header node
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
2.
3.
header node
currentNode
. 65 .
parent
currentNode
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
2.
3.
header node
. 65 .
parent
currentNode
. 40 .
30
. 72 .
. 50 .
69
60
Ver. 1.0
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
2.
3.
header node
. 65 .
parent
currentNode
. 40 .
30
parent
. 50 .
69
60
Ver. 1.0
. 72 .
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
2.
3.
header node
. 65 .
currentNode
. 40 .
30
parent
. 50 .
69
60
Ver. 1.0
. 72 .
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
2.
3.
header node
. 65 .
currentNode
. 40 .
30
parent
. 50 .
69
60
Ver. 1.0
. 72 .
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
2.
3.
header node
. 65 .
currentNode
. 40 .
30
parent
. 50 .
69
60
Ver. 1.0
. 72 .
80
currentNode.
Mark the header node as parent.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
.
. 40 .
30
Ver. 1.0
currentNode.
2.
Mark the header node as parent.
3.
Repeat steps a, b, c, d, and e until the node
header node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
65
left child and go to step 3.
currentNode
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
72
parent
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
69
80
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
currentNode
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
60
. 50 .
Session 16
header node
. 65 .
. 40 .
30
parent
. 50 .
69
60
Ver. 1.0
currentNode.
2.
Mark the header node as parent.
3.
Repeat steps a, b, c, d, and e until the node
to be searched is found or currentNode
becomes NULL:
a.
Mark currentNode as parent.
b.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a link:
i.
Make currentNode point to its
left child and go to step 3.
c.
If the value to be searched is less
than that of currentNode, and the left
child of the currentNode is a thread:
72
i.
Make currentNode as NULL
and go to step 3.
d.
If the value to be searched is greater
than that of currentNode, and the
right child of the currentNode is a link:
i.
Make currentNode point to its
80
right child and go to step 3.
e.
If the value to be searched is greater
than that of currentNode, and the
currentNode
right child of the currentNode is a
thread:
i.
Mark currentNode as NULL
and go to step 3.
Session 16
Ver. 1.0
Session 16
header node
65
new node
Ver. 1.0
Session 16
header node
65
new node
Ver. 1.0
Session 16
header node
Delete operation
complete
65
new node
Ver. 1.0
Session 16
Delete node 60
. 40 .
30
2.
3.
4.
. 72 .
. 50 .
69
60
Ver. 1.0
1.
80
Session 16
Delete node 60
. 65 .
. 40 .
30
1.
2.
3.
4.
. 72 .
. 50 .
69
80
parent
60
currentNode
Ver. 1.0
Session 16
Delete node 60
. 65 .
. 40 .
30
1.
2.
3.
4.
. 72 .
. 50 .
69
80
parent
60
currentNode
Ver. 1.0
Session 16
Delete node 60
. 65 .
. 40 .
30
1.
2.
3.
4.
. 72 .
. 50 .
69
80
parent
60
currentNode
Ver. 1.0
Session 16
Delete node 60
. 65 .
. 40 .
30
1.
2.
3.
4.
. 72 .
. 50 .
69
80
parent
60
currentNode
Ver. 1.0
Session 16
Delete node 60
. 65 .
. 40 .
30
1.
2.
3.
4.
. 72 .
. 50 .
69
80
parent
60
currentNode
Ver. 1.0
Session 16
Delete node 60
. 65 .
. 40 .
30
1.
2.
3.
4.
. 72 .
. 50
69
80
parent
Deletion complete
60
currentNode
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Delete node 50
Algorithm
Let us nowtoconsider
delete aanode
case in
with
which
one
thechild
nodefrom
to be
a deleted
threaded
has one child
binary
(left
tree.
or right).
2.
3.
. 65 .
4.
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
60
8.
9.
Ver. 1.0
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
8.
9.
Ver. 1.0
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
8.
9.
Ver. 1.0
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
8.
9.
Ver. 1.0
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
child
Ver. 1.0
8.
9.
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
child
Ver. 1.0
8.
9.
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
child
Ver. 1.0
8.
9.
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
child
Ver. 1.0
8.
9.
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
child
Ver. 1.0
8.
9.
Session 16
Delete node 50
2.
3.
. 65 .
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
60
child
Ver. 1.0
8.
9.
Session 16
Delete node 50
2.
3.
. 65 .
predecessor
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
8.
60
child
Ver. 1.0
successor
9.
Session 16
Delete node 50
2.
3.
. 65 .
predecessor
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
8.
60
child
Ver. 1.0
successor
9.
Session 16
Delete node 50
2.
3.
. 65 .
predecessor
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
currentNode
8.
60
child
Ver. 1.0
successor
9.
Session 16
Delete node 50
2.
3.
. 65 .
predecessor
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
8.
60
child
Ver. 1.0
successor
9.
Session 16
Delete node 50
2.
3.
. 65 .
predecessor
4.
parent
. 40 .
. 72 .
6.
. 50 .
30
5.
69
80
7.
currentNode
Deletion
complete
60
child
Ver. 1.0
8.
successor
9.
Session 16
Write an algorithm
deletehaving
a node, which has two children
Algorithm
to delete to
a node
fromchildren
a threaded
tree. binary 2. Locate the inorder successor of
two
frombinary
a threaded
currentNode. Mark it as
tree.
Inorder_suc and its parent as
Inorder_parent.
Ver. 1.0
3.
4.
b.
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
. 40 .
. 50
30
45
Ver. 1.0
. 72 .
69
80
a.
b.
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 40 .
. 50
30
45
Ver. 1.0
. 72 .
69
80
a.
b.
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 40 .
. 72 .
. 50
30
69
Inorder_parent
45
80
a.
b.
Inorder_suc
Ver. 1.0
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 40 .
. 72 .
. 50
30
69
Inorder_parent
45
80
a.
b.
Inorder_suc
Ver. 1.0
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 45 .
. 72 .
. 50
30
69
Inorder_parent
45
80
a.
b.
Inorder_suc
Ver. 1.0
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 45 .
. 72 .
. 50
30
69
Inorder_parent
45
80
a.
b.
Inorder_suc
Ver. 1.0
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 45 .
. 72 .
. 50
30
69
Inorder_parent
45
80
a.
b.
Inorder_suc
Ver. 1.0
Session 16
Delete node 40
1.
2.
3.
4.
. 65 .
currentNode
. 45 .
30
. 72 .
50
69
80
Inorder_parent
45
Inorder_suc
Ver. 1.0
a.
b.
Deletion complete
Session 16
Ver. 1.0
Session 16
9
10
11
12
13
14
15
Ver. 1.0
Session 16
9
10
11
12
13
14
15
Ver. 1.0
Session 16
9
10
11
12
13
14
15
Ver. 1.0
Session 16
4
5
6
12
7
8
9
10
14
10
11
12
13
11
13
15
14
15
Ver. 1.0
Session 16
12
Ver. 1.0
10
14
11
13
15
Session 16
12
Ver. 1.0
10
14
11
13
15
Session 16
Ver. 1.0
Session 16
-1
40
40
40
-1
30
50
30
50
0
60
30
50
0
10
Ver. 1.0
Session 16
Ver. 1.0
Session 16
-2
40
40
-2
30
50
30
50
0
55
60
35
45
55
0
34
Ver. 1.0
Session 16
Answer:
One
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Ver. 1.0
Session 16
X
Pl
Xl
Ver. 1.0
Xr
Session 16
-1
-2
-10
X
Pl
Xl
Ver. 1.0
Xr
Session 16
-1
After rotation
-1
Pl
Xl
Ver. 1.0
Xr
Xr
Pl
Xl
Xr
Xr
Session 16
P
0
Y
Pl
Xr
Yl
Ver. 1.0
Yr
Session 16
-1
-2
P
01
-1
0
Y
Pl
Xr
Yl
Yl
Ver. 1.0
Yr
Session 16
-2
P
1
-1
Y
Pl
Xr
Yl
Ver. 1.0
Yr
Session 16
-2
P
1
-2
After single right rotation
-1
-1
Pl
Pl
Xr
Yl
Ver. 1.0
-1
Yr
Yl
Yr
Xr
Session 16
P
1
-2
After single right rotation
-1
-1
Pl
Pl
Xr
Yl
Ver. 1.0
-1
Yr
Yl
Yr
Xr
Session 16
-1
-1
-1
X
Pl
Yl
Yl
Yr
Ver. 1.0
Pl
Yr
Xr
Xr
Session 16
Ver. 1.0
Session 16
P
0
X
Pr
Xl
Ver. 1.0
Xr
Session 16
P
0
1
X
Pr
Xl
Xl
Ver. 1.0
Xr
Session 16
P
1
X
Pr
Xl
Ver. 1.0
Xr
Session 16
X
After single right rotation
X
Pr
Xl
Ver. 1.0
Xr
Xl
Xr
Pr
Session 16
P
0
X
0
Pr
Xl
Yl
Ver. 1.0
Yr
Session 16
P
-1
0
X
-10
Pr
Xl
Yl
Ver. 1.0
Yr
Session 16
P
-1
X
-1
Pr
Xl
Yl
Ver. 1.0
Yr
Session 16
P
-1
X
-1
Pr
Xl
Yl
Ver. 1.0
Yr
Session 16
-1
After single left rotation
-1
-1
-1
Pr
Pr
Yr
Xl
Yl
Yr
Yl
Xl
Ver. 1.0
Session 16
-1
After single left rotation
-1
-1
-1
Pr
Pr
Yr
Xl
Yl
Yr
Yl
Xl
Ver. 1.0
Session 16
P
After single right rotation
Pr
Yr
Yl
Xl
Yr
Pr
Yl
Xl
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Tree is balanced
50
40
30
60
55
80
10
35
32
0
50
Ver. 1.0
Session 16
Tree is balanced
50
40
30
60
55
80
10
35
32
01
50
0
40
Ver. 1.0
Session 16
40
30
60
55
80
10
35
32
Tree becomes
is balanced
unbalanced
1
2
50
10
40
0
30
Ver. 1.0
Session 16
40
30
60
55
80
10
35
32
Tree is now
balanced
0
40
Ver. 1.0
30
50
Session 16
40
30
60
55
80
10
35
32
Tree is balanced
now
balanced
-1
0
40
0
-1
0
30
50
0
60
Ver. 1.0
Session 16
40
30
60
55
80
10
35
32
Tree becomes
is balanced
unbalanced
-1
-2
40
0
-1
-2
30
50
10
60
0
55
Ver. 1.0
Session 16
40
30
60
55
80
10
35
32
Tree becomes
unbalanced
-2
40
0
-2
30
50
1
60
0
55
Ver. 1.0
Session 16
50
40
30
60
55
80
10
35
32
Tree becomes
unbalanced
-2
40
0
-2
30
50
1
-1
55
60
0
55
60
Ver. 1.0
Session 16
50
40
30
60
55
80
10
35
32
Tree is
becomes
now
unbalanced
balanced
-2
-1
40
0
-2
0
30
55
0
-10
50
60
0
60
Ver. 1.0
Session 16
40
30
60
55
80
10
35
32
Tree becomes
is now
balanced
unbalanced
-2
-1
40
0
-1
0
30
55
0
-10
50
60
0
80
Ver. 1.0
Session 16
50
40
30
60
55
80
10
35
32
Tree is now
balanced
-2
0
55
40
0
-1
40
30
60
55
00
0
-1
30
5050
80
60
0
80
Ver. 1.0
Session 16
Tree is balanced
50
40
30
60
55
80
10
35
32
01
55
10
-1
40
60
0
1
30
50
80
0
10
Ver. 1.0
Session 16
Tree is balanced
50
40
30
60
55
80
10
35
32
1
55
Ver. 1.0
-1
40
60
1
0
30
50
80
10
35
Session 16
40
30
60
55
80
10
35
32
Tree is becomes
balanced
unbalanced
1
2
55
1
2
-1
40
60
-10
30
50
80
1
0
10
35
0
32
Session 16
40
30
60
55
80
10
35
32
Tree is becomes
unbalanced
2
55
2
-1
40
60
-1
30
50
80
10
35
0
32
Session 16
50
40
30
60
55
80
10
35
32
2
55
2
-1
40
60
-12
35
50
80
30
35
10
32
Session 16
50
40
30
60
55
80
10
35
32
Tree is now
balanced
0
55
0
-1
35
60
-1
30
40
0
80
10
32
50
Session 16
Answer:
There can be two situations where a double rotation is required
to balance the tree:
If the pivot node is right heavy and the right child of pivot node is
left heavy
If the pivot node is left heavy and the left child of pivot node is
right heavy
Ver. 1.0
Session 16
Ver. 1.0
Session 16
-1
40
-1
70
-1
30
50
-1
60
80
10
35
53
90
0
32
Ver. 1.0
Session 16
-1
40
-1
70
-1
30
50
-1
60
80
10
35
53
90
0
32
Ver. 1.0
Session 16
Delete 53
1
55
1
2
-1
40
-1
70
-1
0
30
50
-1
60
80
10
35
53
90
0
32
Ver. 1.0
Session 16
-1
40
-1
70
0
30
50
-1
60
80
10
35
90
0
32
Session 16
-1
40
-1
70
0
30
50
-1
60
80
10
35
90
0
32
Session 16
1
55
-1
40
2
35
70
0
50
-1
60
80
30
90
10
32
Session 16
-1
35
0
70
-1
30
40
-1
60
80
10
32
50
90
Session 16
Delete 60
0
55
-1
-2
35
0
70
-1
30
40
-1
60
80
10
32
50
90
Session 16
35
0
-1
30
Ver. 1.0
80
40
10
32
50
70
90
Session 16
Delete 50
1
55
0
1
35
0
-1
0
30
Ver. 1.0
80
40
10
32
50
70
90
Session 16
Delete 40
1
55
2
1
35
0
30
Ver. 1.0
80
40
10
32
70
90
Session 16
Delete 40
1
2
55
35
Ver. 1.0
80
30
70
90
10
32
Session 16
55
35
80
30
70
90
10
32
Ver. 1.0
Session 16
-1
30
80
10
35
70
90
0
32
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Undirected graph
Directed graph
Complete graph
Cyclic graph
Answer:
3. Complete graph
Ver. 1.0
Session 16
Ver. 1.0
Session 16
Ver. 1.0
Session 16