Finish MaxHeap ctor, dtor and op=

This commit is contained in:
Shaun Reed 2020-07-11 20:35:49 -04:00
parent 4d917b2db8
commit be91573abc
3 changed files with 18 additions and 11 deletions

View File

@ -23,8 +23,7 @@ int main()
int choice = -1; int choice = -1;
int val; int val;
while (!exit) while (!exit) {
{
std::cout << "##### Max Heap Menu #####\n\t0. Exit" std::cout << "##### Max Heap Menu #####\n\t0. Exit"
"\n\t1. Insert\n\t2. Delete\n\t3. Print" "\n\t1. Insert\n\t2. Delete\n\t3. Print"
<< "\n\t4. Empty\n\t5. Min\n\t6. Max\n"; << "\n\t4. Empty\n\t5. Min\n\t6. Max\n";
@ -55,11 +54,11 @@ int main()
break; break;
case MIN: case MIN:
std::cout << "Min value within our tree: " << testList.findMin() << endl; std::cout << "Min value within our heap: " << testList.findMin() << std::endl;
break; break;
case MAX: case MAX:
std::cout << "Max value within our tree: " << testList.findMax() << endl; std::cout << "Max value within our heap: " << testList.findMax() << std::endl;
break; break;
default: default:
@ -67,5 +66,5 @@ int main()
break; break;
} }
} }
}
}

View File

@ -24,8 +24,12 @@ MaxHeap::MaxHeap() : size(0), index(0), heap(NULL) {}
* *
* @param rhs * @param rhs
*/ */
MaxHeap::MaxHeap(const MaxHeap& rhs) : size(rhs.size), index(rhs.index), heap(rhs.heap) MaxHeap::MaxHeap(const MaxHeap& rhs) : size(rhs.size), index(rhs.index)
{ {
heap = new int[size];
for (int i = 0; i < index; i++) {
heap[i] = rhs.heap[i];
}
} }
/** constructor /** constructor
@ -49,14 +53,18 @@ MaxHeap::~MaxHeap()
} }
/** operator= /** operator=
* Sets one heap equal to another, making the two refer to the same data * Sets one existing MaxHeap equal to another existing MaxHeap
* *
* @param rhs An existing MaxHeap to set equal to * @param rhs An existing MaxHeap
* @return The MaxHeap object which we want to create a reference to * @return The copied MaxHeap object
*/ */
const MaxHeap& MaxHeap::operator=(const MaxHeap& rhs) MaxHeap MaxHeap::operator=(MaxHeap rhs)
{ {
if (this == &rhs) return *this; if (this == &rhs) return *this;
std::swap(heap, rhs.heap);
size = rhs.size;
index = rhs.index;
return *this;
} }

View File

@ -22,7 +22,7 @@ class MaxHeap {
MaxHeap(const MaxHeap& rhs); MaxHeap(const MaxHeap& rhs);
MaxHeap(int _size); MaxHeap(int _size);
~MaxHeap(); ~MaxHeap();
const MaxHeap& operator=(const MaxHeap& rhs); MaxHeap operator=(MaxHeap rhs);
void insert(int val); void insert(int val);
void del(); void del();
void print(); void print();