Assignment 8, Due at 11:59pm, Tue Nov 25. (Absolute!)
Objectives
- Put together a small but complete program. I will not supply a code base this time. You will build everything "from scratch". However, please feel free to use any piece of code I have posted so far. In particular, I strongly suggest you make use of the main program's skeleton we have been using and the
Makefile (modify it!) and other components you've been working with.
- Implement the vector-based
MaxHeap data structure in C++. In particular, by completing this assignment you will have implemented a fully featured data structure along with a driver to test the data structure.
What to do
You are to write a C++ program that does the following.
- 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
where
-
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
wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/ubheap
chmod 700 ubheap
Here is a sample run of ubheap:
[HQN@mymachine] $ ubheap
UBHeap: build and test a Heap data structure
hqn
Version 0.7, comes with NO WARRANTY
> new a = 9 -1 10 2 -3
> print a
10 2 9 -1 -3
> top a
10
> pop a
> push a 20
> print a
20 9 -3 -1 2
> top a
20
> pop a
> pop a
> pop a
> pop a
> print a
-3
> 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
5
> 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
// ~~~~~~~~
// YOUR NAME
// - 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
{
public:
/**
* 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
private:
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
};
#endif
Specific requirements for your code
- You must use
UBHeap.h (above) as is. (Put your name there if you want to.)
- You must supply a
UBHeap.cpp file that implements the interface given in UBHeap.h.
- The rest of the header and source files are up to you.
- 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.
Grading
- 0 point if typing
make in the submitted directory does not produce an executable named ubheap. In particular, if there is any compilation error (using /usr/bin/g++ in timberlake) then no point shall be given. Please even try to remove all warnings. It's a very important professional practice to remove all warnings from the compilation stage.
- 0 point if there is no
UBHeap.cpp implementation of UBHeap.h above.
- 20 points if
new and print work. (The way to test if new works is to see the print output.)
- 20 points if
top works.
- 20 points if
pop works.
- 20 points if
push works.
- 10 points if user errors are handled gracefully, in particular when we try to do
pop, top on an empty heap.
- 10 points if your code is well-indented, with a maximum
of 80 characters per column. Each indentation is between 3 to 8 spaces.
Absolutely no tab character occurs in the code.