diff --git a/cpp/datastructs/maxheap/driver.cpp b/cpp/datastructs/maxheap/driver.cpp index 350d6dd..b4e5fe1 100644 --- a/cpp/datastructs/maxheap/driver.cpp +++ b/cpp/datastructs/maxheap/driver.cpp @@ -23,8 +23,7 @@ int main() int choice = -1; int val; - while (!exit) - { + while (!exit) { std::cout << "##### Max Heap Menu #####\n\t0. Exit" "\n\t1. Insert\n\t2. Delete\n\t3. Print" << "\n\t4. Empty\n\t5. Min\n\t6. Max\n"; @@ -55,11 +54,11 @@ int main() break; case MIN: - std::cout << "Min value within our tree: " << testList.findMin() << endl; + std::cout << "Min value within our heap: " << testList.findMin() << std::endl; break; case MAX: - std::cout << "Max value within our tree: " << testList.findMax() << endl; + std::cout << "Max value within our heap: " << testList.findMax() << std::endl; break; default: @@ -67,5 +66,5 @@ int main() break; } } -} +} diff --git a/cpp/datastructs/maxheap/maxheap.cpp b/cpp/datastructs/maxheap/maxheap.cpp index 0886400..bc8399e 100644 --- a/cpp/datastructs/maxheap/maxheap.cpp +++ b/cpp/datastructs/maxheap/maxheap.cpp @@ -24,8 +24,12 @@ MaxHeap::MaxHeap() : size(0), index(0), heap(NULL) {} * * @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 @@ -49,14 +53,18 @@ MaxHeap::~MaxHeap() } /** 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 - * @return The MaxHeap object which we want to create a reference to + * @param rhs An existing MaxHeap + * @return The copied MaxHeap object */ -const MaxHeap& MaxHeap::operator=(const MaxHeap& rhs) +MaxHeap MaxHeap::operator=(MaxHeap rhs) { if (this == &rhs) return *this; + std::swap(heap, rhs.heap); + size = rhs.size; + index = rhs.index; + return *this; } diff --git a/cpp/datastructs/maxheap/maxheap.h b/cpp/datastructs/maxheap/maxheap.h index 888096e..b0d4124 100644 --- a/cpp/datastructs/maxheap/maxheap.h +++ b/cpp/datastructs/maxheap/maxheap.h @@ -22,7 +22,7 @@ class MaxHeap { MaxHeap(const MaxHeap& rhs); MaxHeap(int _size); ~MaxHeap(); - const MaxHeap& operator=(const MaxHeap& rhs); + MaxHeap operator=(MaxHeap rhs); void insert(int val); void del(); void print();