Assignment 8, Due at 11:59pm, Tue Nov 25. (Absolute!)


What to do

You are to write a C++ program that does the following.
  1. It keeps reading user's inputs, line by line. Each input line the user types is supposed to be in one of the following forms:
    new heapname = [list of integers separated by space]
    top heapname
    pop heapname
    push heapname [an integer]
    print heapname
    • exit tells your program to quit
    • heapname is an identifier (you can just think of it as a valid C++ variable name). For example, a, _a34, counter are all valid variable names.
    • [list of integers separated by space] is ... a list of integers separated by spaces, such as 3 8 -10 2 0
    • [an integer] is any integer, such as 531 or -3
    • new tells your program to create a new max-heap of integers, implemented using the UBHeap class (whose interface is given below). The heap contains all members of the given list of integers. If [list of integers separated by space] is empty, then an empty heap is created.
    • top heapname prints the top element of the heap named heapname. If the heap is empty, then Heap is Empty error is printed.
    • pop heapname removes the maximum element from the heap without printing anything. If the heap is empty, then Heap is Empty error is printed.
    • push heapname [an integer] inserts the integer into the heap.
    • To get a sense of what the program is supposed to do, please download my implementation named ubheap. The binary can run under timberlake as usual. You can get it by typing
      chmod 700 ubheap
      Here is a sample run of ubheap:
      [HQN@mymachine] $ ubheap
      UBHeap: build and test a Heap data structure
      Version 0.7, comes with NO WARRANTY
      > new a = 9 -1 10 2 -3
      > print a
      10 2 9 -1 -3 
      > top a
      > pop a
      > push a 20
      > print a
      20 9 -3 -1 2 
      > top a
      > pop a
      > pop a
      > pop a
      > pop a
      > print a
      > pop a
      > top a
      ** ERROR **
      Heap is Empty
      > pop a
      ** ERROR **
      Heap is Empty
      > new b = 3 1 5 3 2
      > push b 3
      > print b
      5 3 3 1 2 3 
      > top b
      > exit
    • The heaps must be of type UBHeap, whose interface is defined below. In particular you must use the given header file as is. It would be helpful for you to read the lecture notes on priority queue. Most of this assignment is really about copy-and-paste the correct things from a previous assignment, and read the lecture notes.

The UBHeap class

// ============================================================================ 
// UBHeap.h
// ~~~~~~~~
// - a simple Max Heap class
// - THIS FILE MUST BE USED AS IS, Except for the "YOUR NAME" field above
// ============================================================================ 

#ifndef UBHeap_H_
#define UBHeap_H_

#include <vector>

class UBHeap
     * construct a heap from a given array
     * since this has a default parameter, there's a lurking default
     * constructor in here too
    explicit UBHeap(const std::vector<int>& a = std::vector<int>()); 

    UBHeap& operator=(const UBHeap& theOther); // assignment operator

    std::string toString();      // print all members of the heap
    void push(int);              // insert a new int to the heap
    void pop();                  // remove the max element from the heap
    int top();                   // return but not remove the max element
    bool empty();                // return whether the current heap is empty

    std::vector<int> heap_; // storing the heap using an array
    void heapify();         // heapify the array stored in heap_
    void sink(size_t i);    // sink the node whose index is i
    void floatUp(size_t i); // float the node whose index is i


Specific requirements for your code

  1. You must use UBHeap.h (above) as is. (Put your name there if you want to.)
  2. You must supply a UBHeap.cpp file that implements the interface given in UBHeap.h.
  3. The rest of the header and source files are up to you.
  4. You must supply a Makefile. When we type make, the Makefile produces an executable named ubheap. When we type make clean the Makefile will help remove all temporary object files and the executable, leaving only the source files.

How to submit

Put all source files under a directory named A8. Before you submit, make sure you do a make clean. What you will submit is the tarred ball A8.tar
cd ..
tar -cvf A8.tar A8/*
submit_cse250 A8.tar
Note that the above line only works if you logged in to your CSE account and the directory is there. All previous things can be done at home, as long as you remember to upload the final file to your CSE account and run the submit script from there.