Assignment 2
HOWARD NEEDHAM
October 15, 2014
Presented To : Amin Gheibi
Question 1 a: Prove that, with the array representation for storing an n-element heap, the leaves are the nodes indexed by |n/2|floor + 1, |n/2|floor + 2, … , n
Defn:
A heap is a specialized tree base data structure that satisfies the following properties
1:
If A is a parent node of B the key (A) is ordered with respect to the key (B) with the same ordering applying across the heap.
2:
The keys of the parent nodes are either greater than or equal to (Max Heap) or less than or equal to (Min Heap) of their children. Either the highest key (Max Heap) or lowest key (Min
Heap) are the root node – depending on the type of heap (Min or Max Heap)
3:
A heap tree is loaded from left to right, starting at the node and working down in row form. The tree is completely filled on all levels except possibly the lowest, where it is loaded from left to right, up to a point.
Proof: We know from (3) that the indexes of all non-leaf (parent) nodes are stored before leaf (child) nodes. If the number of non-leaf (child) nodes is more than |n/2|floor then the right child of the last non-leaf (parent) node would lie outside of the boundary of the heap; its array index would be greater than n = 2*|n/2|floor + 1, which is greater than ‘n’, which is a contradiction.
From this we can claim that the index of the non-leaf (parent) nodes is at most |n/2|floor. This implies that all parent nodes are indexed from 1 to |n/2|floor. So by our previous statement, the parent node of ‘n’ is |n/2|floor, and since parent nodes cannot be leaf (child) nodes, then
|n/2|floor cannot be a leaf node. Hence, as in the above statement there are exactly |n/2|floor non-leaf (parent) nodes indexed from left to right, 1 to |n/2|floor. Ergo, this implies that all nonleaf (childe) nodes are indexed from |n/2|floor + 1 to ‘n’.
Question 1 b: Is the array with values {23, 17, 14, 6, 13,