COM1201 Spring 1997 Professor Fell QUIZ 8 Solution

1 The following array of current size 10 represents a heap structure:
1234567891011
97766142545931231744 
The tree diagram of this heap is shown below:


a. (5 points) Insert a new value 86 into the heap. Draw the new heap when done. [Increase the heap size from 10 to 11 place 86 initially into slot 12 of the array, then perform upheap to restore the heap conditions.] Show the resulting array.

1234567891011
9786614276593123174454

b. (10 points) Start again with the original heap of 10 elements. Show the result after each swap of two passes of HEAPSORT. Note: After one pass 97 is in position 10 and the values in positions 1 .. 9 again form a heap.
You may have extra space.
12345678910
97766142545931231744
4476614254593123179797 swapped into place
76446142545931231797
76546142445931231797
76546142445931231797pass one finished
1754614244593123769776 swapped into place
61541742445931237697
61545942441731237697pass two finished
          
Bold entries are no longer part of the heap.

2. (5 points) Write a recursive function

int CountRange(Node* root, int low, int high);
that given a Binary Search Tree and low and high values, counts the number of records (data values) that fall between the two values. For full credit, the function CountRange should visit as few nodes as possible.
int CountRange(Node* root, int low, int high){
	if (low > high) return 0; // or precondition: low <= high
	if (!root) return 0;
	if (root->data > high) return CountRange(t->left,  low, high);
	if (root->data <  low) return CountRange(t->right, low, high);
	// if you got this far, low <= root->data <= high
	return 1 + CountRange(t->left, low, high) + CountRange(t->right, low, high);
}


Last Updated: May 26, 1997 10:06 pm by

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1101/QUIZ/Q8/quiz8Solution.html