Mister

Topics: Sorting algorithm, Heapsort, Heap Pages: 20 (5355 words) Published: October 16, 2014
COMP 3804
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, 10, 1, 5, 7, 12} a max heap? Justify your answer.
27
77
27
2

17
77
27
16
77
27
57
72
7

13
77
27
77
72
7

10
77
27

17
72
7

The tree structure in figure 1 is the
heap structure as identified by the
given matrix; loaded from left to right in
row format. As we can see in the right
hand tree structure the parent node
key (2) is less than its child nodes key
(10). From our definition (1) this
violates the requirements for a heap
structure – the same ordering must
apply across the heap.

12
77
27
1|Page

Question 2a: Illustrate the operation of Max-Heapify (A,3) on the array A = { 27, 17, 2, 26, 13, 10, 1, 5, 7, 12, 4, 8, 9, 0 }
27
77
27
17
77
27

26
77
27

2

13
77
27

10

17
72
7

57
77
12
47
87
97
07
72
72
77
72
72
72
72
7
7
27
7
7
7
7
Converting the array A into binary tree we find that nodes 2 and 10 violate the rules for a heap structure. All other nodes satisfy the conditions. To turn this into a Max Heap the elements { 2, 10, 8, 9 } must be rearranged to { 10, 9, 8, 2 } or { 10, 9, 2, 8 }.

NOTES:

If we have a node at index i, the left child will be at 2i, the right child will be at 2i +1, and the parent is at [i/2]floor.

INPUT:

We know that left(i) and right(i) are the roots of two max-heaps and A[ i] might not be the root of a max-heap.

OUTPUT:

Array A; all elements are the root of a max-heap.

HELPER FUNTIONS:
Left (int i)
Right (int i)
Parent (int i)

{ return 2*i }
{ return 2*i +1 }
{ return [1/2]floor }

PRE and POST CONDITIONS:
PRE:

Check if it observes the Heap requirements
[A[i] is a parent node with a higher key value than its children]

2|Page

A != null && i >= 1 && i < A.length && for all int j; j > i && j = A[ left( j ) ] && A[ j ] >= A[ right( j ) ]); POST: Checks the binary tree from the old i location is a heap structure for all int j; j >= old( i ) && j = A[ left( j ) ] && A[ j ] >= A[ right( j ) ]); The pre-condition says all...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Mister Pip Essay
  • Mister Pip’ by Lloyd Jones Essay
  • Mister Pip Essay
  • Essay about Moral Confusion in Mister Pip
  • Mister pip Essay
  • Mister pip essay
  • Mister Pip Essay
  • Essay on mister pip

Become a StudyMode Member

Sign Up - It's Free