Assignment-9 Priority Queue Spanning Balancing Trees Hash Tables 1
Assignment-9 Priority Queue Spanning Balancing Trees Hash Tables 1
Exercise-1
Insert and DeleteMin are two fundamental operations of priority queues. What is the worst case
time cost of these two operations in the case where the priority queue is implemented using:
a linked list
Insert- O(n)
DeleteMin- O(n)
a binary heap
Insert- O(log n)
DeleteMin- O(log n)
Exercise-2:
Describes briefly the method that prevents a Binary Search Tree from degenerating into a linear
structure i.e. unbalanced tree. Explain why there is no need of such method for binary Heaps.
The method that prevents a Binary Search Tree from degenerating into a linear structure is
the AVL method.
Exercise-3:
Suppose we want to find the shortest distance from s to some particular vertex (rather than to all
vertices reachable from s). What would we do? Explain with an example?
What we would do is run Dijkstra’s algorithm which is the most effective short path algorithm.
We would then add the vertex we’d want to find the distance to.
Exercise-4:
Is the sequence ⟨24,16,21,6,8,19,20,5,7,4⟩ a max-heap? Explain?
The sequence is not a max-heap because according to the sequence 7 would be under the node 6
which goes against max heaps rules of the parent node always being larger than the child node.
Exercise-5:
Describe a linear-time algorithm MaxHeapVerify(A) that checks whether a given array A is a
max-heap. Analyze the running time.
def MaxHeapVerify(A):
for i in range(len(A)-1, 0, -1):
if A[(i-1)//2] < A[i]:
return False
return True
The big O notation of this algorithm is O(N). This is because the for loop is n-1 and
everything else is 0(1) which makes the total big O notation O(N).
Exercise-6:
Draw a BST with the following array: [2,4,6,8,10,12]
Now show the steps to balance this BST. Use appropriate rotation to balance the Tree.
For one the tree can’t have a side that’s two levels lower than another or else it isn’t
balanced. For this to be successful I’d to use 8 as the first parent node. Then after that, I’d put the
next smallest number on the left and the next biggest number on the right.