UCR CS 14: Introduction to Data Structures and Algorithms

Summer Quarter 2003 / Class webpage




Lab 08

Remember:

In this lab, we will work with ADT Heap (chapter 11). A heap is a complete binary tree:
   1.That is empty
or
   2a. Whose root contains a search key greater than or equal to the search key in each of its children, and
   2b. Whose root has heaps as its subtrees.

In this lab you will work using an array-based implementation of the ADT heap. You will need the following files:

Figure 1. UML diagram for the class Heap

In our definition of a heap, the root contains the item with the largest search key. Such a heap is also known as a maxheap. A minheap, on the other hand, places the item with the smallest search key in its root. Today you will implement a minheap and trace the swaps necessary for inserting new values in the heap, for example:

Enter a positive value (negative value to stop): 6
Insert value: 6
Heap values:
6

Enter a positive value (negative value to stop): 8
Insert value: 8
Heap values:
6  8

Enter a positive value (negative value to stop): 7
Insert value: 7
Heap values:
6  8  7

Enter a positive value (negative value to stop): 5
Insert value: 5
    (1) Swap 5 and 8
    (2) Swap 5 and 6
Heap values:
5  6  7  8

Enter a positive value (negative value to stop): 4
Insert value: 4
    (3) Swap 4 and 6
    (4) Swap 4 and 5
Heap values:
4  5  7  8  6

Enter a positive value (negative value to stop): 2
Insert value: 2
    (5) Swap 2 and 7
    (6) Swap 2 and 4
Heap values:
2  5  4  8  6  7

Enter a positive value (negative value to stop): -1


Figure 2 illustrates these insertions.

Figure 2. Sequence of insertions in a heap

Even though you do not have enough time to finish all bonus exercises during the lab, you should try to do them by yourself. Keep in mind that only those exercises that you turn in during the lab will be considered in your grade.


Turn-in

8 points possible (plus 2 from participation)


Bonus, 2 points each