1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
#include <string>
#include "Heap.h"
using std::cout;
using std::endl;
typedef int TYPE;
int main(void) {
Heap<TYPE> *heap = new Heap<TYPE>(); // numeric type heap
TYPE item = 0;
Heap<std::string> *s_heap = new Heap<std::string>(); // string heap
std::string s_item = "y";
std::vector<std::string> s_blah(11,s_item);
cout << "*** Test insert elements ***";
heap->Insert(15);
heap->Insert(1);
heap->Insert(3);
heap->Insert(4);
heap->print_heap(); //should print 15 4 3 1 and it does
cout << endl;
cout << "\n*** Test delete elements ***\n"; //delete also works fine 15, 4, 3, 1 and then returns 0
cout << item << " " << heap->Delete(item) << endl;
cout << item << " " << heap->Delete(item) << endl;
cout << item << " " << heap->Delete(item) << endl;
cout << item << " " << heap->Delete(item) << endl;
cout << "the next delete should return false (i.e. 0)\n";
cout << item << " " << heap->Delete(item) << endl;
cout << "\n*** Test Heap(vector,bool) ***\n";
std::vector<TYPE> blah(11, 99);
blah[1] = 15;
blah[2] = 4;
blah[3] = 12;
blah[4] = 3;
blah[5] = 22;
blah[6] = 37;
blah[7] = 9;
blah[8] = 1;
blah[9] = 7;
blah[10] = 3;
delete heap;
heap = new Heap<TYPE>(blah, false); // false make it a min heap
heap->print_heap();
//here it prints wrong> 99 15 4 12 3 22 37 9 1 7 3
//and it should print> 1 3 4 9 3 22 37 15 12 7 99
cout << endl;
// Sting heap test
cout << "\n*** Test insert elements ***";
s_heap->Insert("15");
s_heap->Insert("11");
s_heap->Insert("13");
s_heap->Insert("43");
cout << endl;
s_heap->print_heap();
//works fine, prints out 43, 15, 13, 11
cout << "\n*** Test delete elements ***\n";
cout << s_item << " " << s_heap->Delete(s_item) << endl;
cout << s_item << " " << s_heap->Delete(s_item) << endl;
cout << s_item << " " << s_heap->Delete(s_item) << endl;
cout << s_item << " " << s_heap->Delete(s_item) << endl;
cout << "the next delete should return false (i.e. 0)\n";
cout << s_item << " " << s_heap->Delete(s_item) << endl;
//deletes fine
cout << "\n*** Test Heap(vector,bool) ***\n";
s_blah[1] = "a";//"15";
s_blah[2] = "c";//"4";
s_blah[3] = "e";//"12";
s_blah[4] = "d";//"3";
s_blah[5] = "f";//"22";
s_blah[6] = "h";//"37";
s_blah[7] = "s";//"9";
s_blah[8] = "b";//"1";
s_blah[9] = "v";//"7";
s_blah[10] = "z";//"3";
delete s_heap;
s_heap = new Heap<std::string>(s_blah, false);
s_heap->print_heap();
cout << endl;
//again, messes up here> it prints:
//z y c e a f h s b v d
//it should print>
a b c e d f h s y v z
cout << "*** Emptying heap ***\n";
unsigned int size = s_heap->size();
for(unsigned int i=0; i<size; i++) {
s_heap->Delete(s_item);
cout << s_item << " ";
}
cout << "\n*********************\n";
cout << endl;
} // end to main
|