Professional Documents
Culture Documents
Objectives
Write
Deleteanoperation
algorithminto
a locate
threaded
thebinary
node to
tree
berefers
deleted
to in
thea
threaded
process ofbinary
removing
tree. the specified node from the threaded
binary tree.
Before implementing a delete operation, you first need to
locate the node to be deleted.
This requires you to implement a search operation.
After the search operation, the node to be deleted is marked
as the currentNode and its parent as parent.
Once you
Before
However, iflocate
implementing
the treetheisnode
anot
deleteto be
empty, deleted
operation, and
there canyoubeits parent,
first need
three you
to
cases:
can Node
checkrelease
whether
to be thedeleted
memory
the aof
treeis is thenode
empty
leaf node to be deleted after
or not.
adjusting
The Node the
tree is
to belinks
empty ifand
deleted the threads
hasleftone
childappropriately.
of(left
child the or
header
right) node is a
thread
Nodepointing to itself.
to be deleted has two children
If the tree is empty, an error message is shown.
header node
header node
65
new node
To delete this node, make the left child of the header node
as a thread pointing to itself.
header node
65
new node
To delete this node, make the left child of the header node
as a thread pointing to itself.
Now release the memory of the node to be deleted.
header node
Delete operation
65 complete
new node
one nodes in the tree, you need 2. If currentNode is the left child of
parent:
another algorithm to delete a leaf a. Set the left thread field of
node. . 65 . parent as zero.
b. Make the left child field of
parent as a thread pointing
to the inorder predecessor
. 40 . . 72 . c.
of currentNode.
Go to step 4.
currentNode
currentNode
currentNode
currentNode
currentNode
currentNode
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
child 9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
child 9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
child 9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
child 9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
child 9. Release the memory for currentNode.
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
thread of predecessor to zero.
child 9. Release the memory for currentNode.
predecessor
. 65 . b.
child.
Go to step 4.
parent 4. If currentNode is the left child of parent:
a. Make the left child of parent point to
child.
. 40 . . 72 . 5.
b. Go to step 6.
If currentNode is the right child of parent:
a. Make the right child of parent point to
child.
b. Go to step 6.
6. Find the inorder successor and inorder
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
successor thread of predecessor to zero.
child 9. Release the memory for currentNode.
predecessor
. 65 . b.
child.
Go to step 4.
parent 4. If currentNode is the left child of parent:
a. Make the left child of parent point to
child.
. 40 . . 72 . 5.
b. Go to step 6.
If currentNode is the right child of parent:
a. Make the right child of parent point to
child.
b. Go to step 6.
6. Find the inorder successor and inorder
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
successor thread of predecessor to zero.
child 9. Release the memory for currentNode.
predecessor
. 65 . b.
child.
Go to step 4.
parent 4. If currentNode is the left child of parent:
a. Make the left child of parent point to
child.
. 40 . . 72 . 5.
b. Go to step 6.
If currentNode is the right child of parent:
a. Make the right child of parent point to
child.
b. Go to step 6.
6. Find the inorder successor and inorder
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
currentNode point to its predecessor and set the left
currentNode thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
successor thread of predecessor to zero.
child 9. Release the memory for currentNode.
predecessor
. 65 . b.
child.
Go to step 4.
parent 4. If currentNode is the left child of parent:
a. Make the left child of parent point to
child.
. 40 . . 72 . 5.
b. Go to step 6.
If currentNode is the right child of parent:
a. Make the right child of parent point to
child.
b. Go to step 6.
6. Find the inorder successor and inorder
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
currentNode point to its predecessor and set the left
thread of successor to zero.
b. Go to step 9.
8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
successor thread of predecessor to zero.
child 9. Release the memory for currentNode.
predecessor
. 65 . b.
child.
Go to step 4.
parent 4. If currentNode is the left child of parent:
a. Make the left child of parent point to
child.
. 40 . . 72 . 5.
b. Go to step 6.
If currentNode is the right child of parent:
a. Make the right child of parent point to
child.
b. Go to step 6.
6. Find the inorder successor and inorder
30 . 50 . 69 80
predecessor of currentNode. Mark them as
successor and predecessor, respectively.
7. If currentNode has a right child:
a. Make the left child field of its successor
currentNode point to its predecessor and set the left
thread of successor to zero.
Deletion b. Go to step 9.
complete 8. If currentNode has a left child:
60 a. Make the right child of its predecessor
point to its successor and set the right
successor thread of predecessor to zero.
child 9. Release the memory for currentNode.
Inorder_suc
Inorder_suc
Inorder_suc
Inorder_suc
Inorder_suc
Consider
After inserting
an example
values in
in the
which
specified
you wantorder,
to insert
the binary
some
search tree
numeric values
appears
in a binary
as follows:
search tree in the following order:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
This
Now
In aprocess
Assuch result,
if you can binary
the
a case,
want be
to very
the searchtimesearch
search
binary consuming
for tree
a value
tree ifbecomes
attains
14 the
inanumber
linear
the of values
structure.
given
equivalent
binary
stored
search
to
In a
this in
linked a binary
tree,
case, the search
list.
you tree
will have
height tois traverse
of the large. all its preceding
binary search tree is 15. nodes
before you reach node 14. In this case, you need to make
14 comparisons.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In this
The
Now height
if case,
you want
ofthe
thetotal
tobinary
search
number
search
for of
a value
tree
comparisons
has
14,now
you been
to
just
beneed
reduced
madeto to
to 4. for
traverse
search nodes
node814 and
is three.
12, before you reach node 14.
This approach reduces the time to search for a particular
value in a binary search tree.
4 12
2 6 10 14
1 3 5 7 9 11 13 15
4 12
2 6 10 14
1 3 5 7 9 11 13 15
1
-1 0
40
40 40
1 0
0 -1 0 0
30 50
30 50 30 50
0
0
10
60
-2 1
40 40
0 -2 2 0
30 50 30 50
1 1 0 0
60 35 45 55
0
0
34
55
Answer:
One
Xl Xr
Aftertree
The insertion of a node,unbalanced.
now becomes the binary tree appears as:
-1
-2
P -10
X
Pl
Xl Xr
Therestore
To tree isthe
nowbalance,
balanced.
you need to perform a single left
rotation around the pivot node.
-2 -1
After rotation
P -1 X
0
X
P
Pl
Xl Xr
Xr Pl Xl Xr
Xr
-1
P 0
X
0
Y
Pl
Xr
Yl Yr
-1
-2
P -1
0
X
01
Y
Pl
Xr
Yl
Yl Yr
To the
In restore the balance,
first rotation, you need
the subtree to root
with perform
X is arotated
doubleright in
rotation.
such a way so that the right child of Y now points to X.
-2
P -1
X
1
Y
Pl
Xr
Yl Yr
-2 -2
After single right rotation
P -1 P
-1
X Y
1 -1
Y X
Pl Pl Yl
Xr
Yl Yr Yr
Xr
-2 -2
After single right rotation
P -1 P
-1
X Y
1 -1
Y X
Pl Pl Yl
Xr
Yl Yr Yr
Xr
The
In thetree
second
is now
rotation,
balanced.
the subtree with root P is rotated left
so that Y is moved upwards to form the root.
The left child of Y now points to P.
0
-2
After single left rotation
Y
P 0
-1
-1 P X
Y
-1
X
Pl Yl Yl Yr
Xr
Yr Pl
Xr
X
Pr
Xl Xr
1
2
P
0
1
X
Pr
Xl
Xl Xr
2
P
1
X
Pr
Xl Xr
2 0
P X
After single right rotation
1 0
P
X
Pr
Xl
Xr Xr Pr
Xl
0
X
0
Y
Pr
Xl
Yl Yr
21
P
-1
0
X
-10
Y
Pr
Xl
Yl Yr
2
P
-1
X
-1
Y
Pr
Xl
Yl Yr
2
P
-1
X
-1
Y
Pr
Xl
Yl Yr
2 2
P -1 P
After single left rotation
-1 Y
-1
X X
-1
Y
Pr Pr
Xl Yr
Yl Yr Yl
Xl
2 2
P -1 P
After single left rotation
-1 Y
-1
X X
-1
Y
Pr Pr
Xl Yr
Yl Yr Yl
Xl
The
In thetree
next
is step,
now balanced.
a single right rotation is performed in which
the subtree with root P is rotated right.
2 0
-1 P Y
After single right rotation
Y 1 0
-1
X P
X
Pr
Yl
Yr Xl Yr Pr
Yl
Xl
Let us
50 40consider
30 60 an example
55 80 to10
insert35
values
32 in a binary
search tree and restore its balance whenever required.
0
50
01
50
0
40
1
2
50
10
40
0
30
0
40
0 0
30 50
-1
0
40
0 -1
0
30 50
0
60
-1
-2
40
0 -1
-2
30 50
10
60
0
55
-2
40
0 -2
30 50
1
60
0
55
-2
40
0 -2
30 50
-1
1
55
60
0 0
55 60
-2
-1
40
0 -2
0
30 55
0 -10
50 60
0
60
-2
-1
40
0 -1
0
30 55
0 -10
50 60
0
80
-2
0
55
40
0 -1
40
30 60
55
0 00 0
-1
30 5050 80
60
0
80
01
55
10 -1
40 60
0
1 0 0
30 50 80
0
10
1
55
1 -1
40 60
1
0 0 0
30 50 80
0 0
10 35
1
2
55
1
2 -1
40 60
-10 0 0
30 50 80
0 1
0
10 35
0
32
2
55
2 -1
40 60
-1 0 0
30 50 80
0 1
10 35
0
32
50 40 30 60 55 80 10 35 32
2
55
2 -1
40 60
-12 0 0
35 50 80
0 1
30 35
0 0
10 32
0
55
0 -1
35 60
0 -1 0
30 40 80
0 0 0
10 32 50
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
0
32
0
32
Delete 53
1
55
1
2 -1
40 70
-1 -1
0 0 -1
30 50 60 80
0 1 0 0
10 35 53 90
0
32
0
32
0
32
1
55
2 -1
40 70
2 0 0 -1
35 50 60 80
0 0
30 90
0 0
10 32
0 0 0
10 32 50
0 0 0
10 32 50
0 0
10 32
0 0
10 32
0 0
10 32
0
32
Consider a situation:
You have to visit a set of cities and return back to the original
city in the end.
For this, you need to:
Find the shortest or least expensive path that starts from the
original city, visits each of the desired cities, and then returns
back to the original city.
How will you solve this problem?
Answer:
3. Complete graph
Answer:
The given graph is a directed graph.