Assignment 3 - Solutions.
1:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
But it is possible that the preorder traversal
of T visits the nodes in the reverse order of the postorder traversal.Consider
following example.
A tree in which all nodes have one child or zero child.In
this case the preorder traversing is just the reverse of postorder traversing.
= (h-2) .2h + 2
= h.2h -
2 .2h + 1 + 1
= h.2h + 1- 2
.2h + 1
=
De + 1 - 2 .2h + 1
De + 1 = D i + 2 .2h -
1 = D i + 2h + 1 - 1
We know , total number of nodes in T is n = 2h + 1 - 1
So De + 1 = Di + n ----(1)
Given De + 1 = a .Di + b. n ---- (2)
From (1) and (2)
a = 1 and b = 1.
if isInternal(T, v)
then next <- LeftChild(v)
else if isLeftChild(T,v)
then next <- RightChild(Parent(v))
else
while isRightChild(parent(v))
do v <- parent(v)
v <- parent(v)
next <- RightChild(Parent(v))
return next
In worst case the while loop executes height of
the tree times.So worst case running time is O(lgn).
Algorithm InorderNext(T, v)
if
isExternal(T, v)
then if is LeftChild(v)
then next <- parent(v)
else
while isRightChild(parent(v))
do v <- parent(v)
next <- parent(parent(v))
else
v <- rightChild(v)
if isExternal(T,v)
then next <- v
else
while isInternal(T,v)
do v <- leftChild(v)
next <- v
return next
The worst case running time is O(lgn)
Algorithm PostorderNext(T , v)
if isExternal(T,v)
then
if isRightChild(v)
then next <- parent(v)
else
v <- rightChild(parent(v))
while(isInternal(v))
do v <- leftChild(v)
next <- v
else
if isRightChild(v)
then next <- parent(v)
else
v <- rightChild(parent(v))
while(isInternal(v))
do v <- leftChild(v)
next <- v
return next
The worst case running time is O(lgn)
Analysis
depth() takes
O(n) time . It is executed twice in this algorithm. So O(2n)
. parent() takes O(1) time. In the worst case , when v and are leaf nodes
in two differnt path and root is their LCA , then parent () is called 2n
times. So total time for parent() is Theta(2n).
So total running time of this algorithm is O(2n
+ 2n ) = O(n).
O(£ni =1
i) = n(n + 1) / 2 = O(n2)
The removeMin() takes O(1) time.There are n removals
So total time for removals O(n)
So total running time is O(n2
+ n) = O(n2)
input sequence ( 2,5, 16,4, 10,23, 39 , 18, 26 ,
25)
In Heap sort , a priority queue is implemented using
the heap.First all th elements are inserted into the priority queue.Then
removeMin() is called n times where n is the number of elements in the
sequence.Initially we make the heap , root of the heap is 2 .we swap
that with the last node in the heap ie 25 and decreases the size of the
heap by 1.Again we bubble down 25 to proper position.This procedure is
repeated till the heap size is one. So the output sequnce is ( 2,4,5,10,16,18,23,25,26,39)
You can show this using the diagram.
Algorithm Find_Smallest(k):
Step 1: make the heap using the Bottom-Up
Heap procedure. ----O(n)
Step 2: while k > 0
remove root from the heap --------O(1)
replace that with the last node in the heap -----O(1)
bubble down the replaced node . -------O(lgn)
Step 3:
return k
The running time is O(n + klgn) .