HW2 solutions

HW2 solutions - Homework #2 (Solutions) 1. Let X be a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Homework #2 (Solutions) 1. Let X be a binary search tree with height indicators located at each node. Let X 1 and X 2 be the subtrees rooted at the children of the root of X . Assuming X 1 and X 2 are height balanced AVL trees of height h 1 and h 2 respectively, give an O ( | h 1- h 2 | + 1) algorithm to balance the whole tree without increasing its height. Solution Without loss of generality assume that h 1 h 2 . If h 1- h 2 2, use the standard balancing procedure. Thus, consider the case when h 1- h 2 3. Let x ,x 1 ,.....,x k be the path that starts at the root and descends from a parent to its right child until h ( x ) h 2 + 1. Thus h k is either h 2 + 1 or h 2 ; it cannot be smaller because h ( x k- 1 ) > h 2 + 1 and the subtree X 1 is balanced. We insert the root (with subtree X 2 attached) between the nodes x k- 1 and x k . In the new tree, node x is balanced, but its parent, x k- 1 , may not be balanced because the height of its right child has increased by one. Therefore, we need to rebalance the nodes x k- 1 ,x k- 2 ,.....,x 1 using the standard procedure. By induction, the height of each of these nodes increases by one or does not change. Therefore, the total tree height either remains the same or becomes smaller by one, since the original root x has been removed. The time taken is O ( | h 1- h 2 | ), which is O ( | h 1- h 2 | + 1). 2. Suppose we are given all the elements we wish to insert into an initially empty MaxHeap up front. If we insert one at a time, it takes O ( n log n ) time to build the MaxHeap. Suppose we instead build a pre-heap of depth d by forming a complete binary tree with each node in an arbitrary position. We must heapify this pre-heap so that each node has value greater than its children. (a) Suppose we take the subtrees of size 3 rooted at depth d- 1. Show how to heapify all subtrees in O ( n ) total time. Solution Suppose we take the subtrees of size 3 rooted at depth d- 1. Show how to heapify all subtrees in O ( n ) total time. We need to check the root of the subtree and its child (both children) to determine which of the node is the greatest. We pick the largest child and compare it to the root. If root is larger, then it quits, otherwise it swaps the root with the largest child. We do this for all subtrees, of which there are approximately n 4 . (b) Assuming that all subtrees rooted at depth j are valid MaxHeaps, show how to heapify all subtrees rooted at depth j- 1 in O ( ( d- j ) n 2 d- j ) total time. Solution Assuming that all subtrees rooted at depth j are valid MaxHeaps, show how to heapify all subtrees rooted at depth j- 1 in O ( ( d- j ) n 2 d- j ) total time. We can view this as tracing a path from the root down to the nodes at depth j . Similar to part a, we first check the root and its children to determine which of the node is the greatest. If it is the root, then we are done. If not, we swap the largest child with the root and continue recursively down the tree until we reach...
View Full Document

Page1 / 5

HW2 solutions - Homework #2 (Solutions) 1. Let X be a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online