Makefile
(modify it!) and other components you've been working with.
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.
new heapname = [list of integers separated by space] top heapname pop heapname push heapname [an integer] print heapname exitwhere
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.
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 ubheapHere 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
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.
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
UBHeap.h
(above) as is. (Put your name there if you want to.)
UBHeap.cpp
file that implements the interface given in UBHeap.h
.
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.
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.tarNote 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.
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.
UBHeap.cpp
implementation of UBHeap.h
above.
new
and print
work. (The way to test if new
works is to see the print
output.)
top
works.
pop
works.
push
works.
pop, top
on an empty heap.