Add RD of MaxHeap implementation
This commit is contained in:
parent
a864342974
commit
4d917b2db8
|
@ -0,0 +1,18 @@
|
|||
###############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: An example of a max heap implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## CMakeLists.txt
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
# Define the project name
|
||||
project(MaxHeap)
|
||||
# Define source files
|
||||
set(SRC driver.cpp maxheap.cpp)
|
||||
# Build an executable
|
||||
add_executable(HeapDriver ${SRC})
|
|
@ -0,0 +1,71 @@
|
|||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: A driver program to test a max heap implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## driver.cpp
|
||||
*/
|
||||
|
||||
#include "maxheap.h"
|
||||
#include <iostream>
|
||||
|
||||
enum OPS {
|
||||
EXIT, INSERT, DELETE, PRINT, EMPTY, MIN, MAX
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Driver: \n";
|
||||
MaxHeap testList;
|
||||
bool exit = false;
|
||||
int choice = -1;
|
||||
int val;
|
||||
|
||||
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";
|
||||
std::cin >> choice;
|
||||
std::cin.clear();
|
||||
switch (choice) {
|
||||
case EXIT:
|
||||
exit = true;
|
||||
break;
|
||||
|
||||
case INSERT:
|
||||
std::cout << "Enter a value to insert to our tree: ";
|
||||
std::cin >> val;
|
||||
std::cin.clear();
|
||||
testList.insert(val);
|
||||
break;
|
||||
|
||||
case DELETE:
|
||||
testList.del();
|
||||
break;
|
||||
|
||||
case PRINT:
|
||||
testList.print();
|
||||
break;
|
||||
|
||||
case EMPTY:
|
||||
testList.makeEmpty();
|
||||
break;
|
||||
|
||||
case MIN:
|
||||
std::cout << "Min value within our tree: " << testList.findMin() << endl;
|
||||
break;
|
||||
|
||||
case MAX:
|
||||
std::cout << "Max value within our tree: " << testList.findMax() << endl;
|
||||
break;
|
||||
|
||||
default:
|
||||
std::cout << "Invalid entry...\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,269 @@
|
|||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: An example of a max heap implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## maxheap.cpp
|
||||
*/
|
||||
|
||||
#include "maxheap.h"
|
||||
|
||||
/********************************************************************************
|
||||
* Constructors, Destructors, Operators
|
||||
*********************************************************************************/
|
||||
|
||||
/** default constructor
|
||||
* Constructs a heap with the given default values
|
||||
*/
|
||||
MaxHeap::MaxHeap() : size(0), index(0), heap(NULL) {}
|
||||
|
||||
/** copy constructor
|
||||
* Creates a new heap which is identical to an existing MaxHeap object
|
||||
*
|
||||
* @param rhs
|
||||
*/
|
||||
MaxHeap::MaxHeap(const MaxHeap& rhs) : size(rhs.size), index(rhs.index), heap(rhs.heap)
|
||||
{
|
||||
}
|
||||
|
||||
/** constructor
|
||||
* Constructs a new heap with a predefined size
|
||||
* Does not input any values
|
||||
*
|
||||
* @param _size The maximum number of indices within the heap
|
||||
*/
|
||||
MaxHeap::MaxHeap(int _size) : size(_size), index(0)
|
||||
{
|
||||
grow(heap, size);
|
||||
heap[index++] = INT32_MIN;
|
||||
}
|
||||
|
||||
/** destructor
|
||||
* Empties the heap, freeing used memory
|
||||
*/
|
||||
MaxHeap::~MaxHeap()
|
||||
{
|
||||
makeEmpty();
|
||||
}
|
||||
|
||||
/** operator=
|
||||
* Sets one heap equal to another, making the two refer to the same data
|
||||
*
|
||||
* @param rhs An existing MaxHeap to set equal to
|
||||
* @return The MaxHeap object which we want to create a reference to
|
||||
*/
|
||||
const MaxHeap& MaxHeap::operator=(const MaxHeap& rhs)
|
||||
{
|
||||
if (this == &rhs) return *this;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
* Public Member Functions
|
||||
*********************************************************************************/
|
||||
|
||||
/** insert
|
||||
* Calls private member to insert variable into our heap
|
||||
*
|
||||
* @param val The value to be inserted into the heap
|
||||
*/
|
||||
void MaxHeap::insert(int val)
|
||||
{
|
||||
insert(heap, size, val);
|
||||
}
|
||||
|
||||
/** del
|
||||
* Removes the ROOT value from the heap
|
||||
*/
|
||||
void MaxHeap::del()
|
||||
{
|
||||
del(heap);
|
||||
}
|
||||
|
||||
/** print
|
||||
* Outputs all values held within the heap
|
||||
*/
|
||||
void MaxHeap::print()
|
||||
{
|
||||
print(heap, index);
|
||||
}
|
||||
|
||||
/** makeEmpty
|
||||
* Deletes the heap object if it exists
|
||||
*/
|
||||
void MaxHeap::makeEmpty()
|
||||
{
|
||||
if (!isEmpty()) {
|
||||
delete[] heap;
|
||||
heap = NULL;
|
||||
size = index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** findMin
|
||||
* Finds and returns the minimum value stored within our heap
|
||||
*
|
||||
* @return The smallest value stored in our heap
|
||||
*/
|
||||
int MaxHeap::findMin()
|
||||
{
|
||||
int min = INT32_MAX;
|
||||
for (int i = ROOT; i < index; i++)
|
||||
if (min > heap[i]) min = heap[i];
|
||||
return min;
|
||||
}
|
||||
|
||||
/** findMax
|
||||
* Finds and returns the max value stored within our heap
|
||||
*
|
||||
* @return The largest value stored in our heap
|
||||
*/
|
||||
int MaxHeap::findMax()
|
||||
{
|
||||
return heap[ROOT];
|
||||
}
|
||||
|
||||
/** isEmpty
|
||||
* Checks if our heap is empty or not
|
||||
*
|
||||
* @return true if the heap is empty, false if it has contents
|
||||
*/
|
||||
bool MaxHeap::isEmpty()
|
||||
{
|
||||
return heap == NULL;
|
||||
}
|
||||
|
||||
/** isFull
|
||||
* Checks if our heap is full or not
|
||||
*
|
||||
* @return true if the heap is full, false if it is not
|
||||
*/
|
||||
bool MaxHeap::isFull()
|
||||
{
|
||||
// Offset for the 0 index
|
||||
return index >= size-1;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
* Private Member Functions
|
||||
*********************************************************************************/
|
||||
|
||||
/** insert
|
||||
* @brief Inserts a given value into a heap array at the last free position
|
||||
*
|
||||
* @param heap Address of the heap array to modify
|
||||
* @param _size Last free position within the heap array
|
||||
* @param val Value to insert into the heap
|
||||
*/
|
||||
void MaxHeap::insert(int*& heap, int _size, int val)
|
||||
{
|
||||
if (isFull()) grow(heap, _size * 2);
|
||||
heap[index] = val;
|
||||
//Check the root node < all parent nodes
|
||||
siftUp(heap, index++);
|
||||
// Increment index
|
||||
}
|
||||
|
||||
/** del
|
||||
* @brief Delete at the root index of the heap (1)
|
||||
*
|
||||
* @param heap Address of the heap to modify
|
||||
*/
|
||||
void MaxHeap::del(int* heap)
|
||||
{
|
||||
std::swap(heap[ROOT], heap[index-1]);
|
||||
heap[index-1] = int();
|
||||
//Check the root node > all child nodes
|
||||
siftDown(heap, ROOT);
|
||||
}
|
||||
|
||||
/** print
|
||||
* @brief Print the contents of the heap array
|
||||
* Start from the 0 index, regardless of the ROOT
|
||||
*
|
||||
* @param heap Address of the heap array
|
||||
* @param _index Last free position in the array
|
||||
*/
|
||||
void MaxHeap::print(int* heap, int _index)
|
||||
{
|
||||
if (isEmpty()) return;
|
||||
for (int i = 0; i < _index; i++)
|
||||
std::cout << "[" << heap[i] << "] | ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
/** grow
|
||||
* @brief Expands the maximum length of the heap
|
||||
*
|
||||
* @param heap Modifiable reference to the dynamic heap array to expand
|
||||
* @param _size The new maximum size for the given heap
|
||||
*/
|
||||
void MaxHeap::grow(int*& heap, int _size)
|
||||
{
|
||||
if (isEmpty()) {
|
||||
// Offset size for the 0 index
|
||||
size = 2;
|
||||
heap = new int[size];
|
||||
// Store smallest int possible at 0 index to show its value isn't to be used
|
||||
heap[index++] = INT32_MIN;
|
||||
return;
|
||||
}
|
||||
int *newHeap = new int[_size];
|
||||
if (!isEmpty()) for (int i = 0; i < _size; i++)
|
||||
// Deep copy of previous heap
|
||||
newHeap[i] = heap[i];
|
||||
// Delete the previous heap before we reassign
|
||||
delete[] heap;
|
||||
size = _size;
|
||||
heap = newHeap;
|
||||
}
|
||||
|
||||
/** siftUp
|
||||
* @brief Sorts the last item inserted into the heap against items above it
|
||||
* Swap nodes until given item is < parent node
|
||||
*
|
||||
* @param heap Address of heap array to sort through
|
||||
* @param _index Last used position within the heap array
|
||||
*/
|
||||
void MaxHeap::siftUp(int* heap, int _index)
|
||||
{
|
||||
// Swap until parent value > new value
|
||||
for(; heap[_index] > heap[_index / 2]; _index/=2)
|
||||
{
|
||||
if (heap[_index / 2] == INT32_MIN) return;
|
||||
std::swap(heap[_index], heap[_index / 2]);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** siftDown
|
||||
* @brief Sorts the item at the given currentMax against lower items in the heap
|
||||
* Swap nodes until the given item is > all child nodes
|
||||
*
|
||||
* @param heap Address of heap array to sort through
|
||||
* @param currentMax Last known largest position within the heap
|
||||
*/
|
||||
void MaxHeap::siftDown(int* heap, int currentMax)
|
||||
{
|
||||
int left = currentMax * 2; // Left subtree of the heap
|
||||
int right = currentMax * 2 + 1; // Right subtree of the heap
|
||||
// Set the last known largest position in the heap
|
||||
int largest = currentMax;
|
||||
|
||||
// Check if the left tree value is > the last known largest
|
||||
if (left < index && heap[left] > heap[largest])
|
||||
largest = left;
|
||||
// Check if the right tree value is > the last known largest
|
||||
if (right < index && heap[right] > heap[largest])
|
||||
largest = right;
|
||||
|
||||
// If there was any change in the last known largest node, siftDown again
|
||||
if (currentMax != largest) {
|
||||
std::swap(heap[currentMax], heap[largest]);
|
||||
siftDown(heap, largest);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: An example of a max heap implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## maxheap.h
|
||||
*/
|
||||
|
||||
#ifndef MAXHEAP_H
|
||||
#define MAXHEAP_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define ROOT 1
|
||||
|
||||
class MaxHeap {
|
||||
|
||||
public:
|
||||
MaxHeap();
|
||||
MaxHeap(const MaxHeap& rhs);
|
||||
MaxHeap(int _size);
|
||||
~MaxHeap();
|
||||
const MaxHeap& operator=(const MaxHeap& rhs);
|
||||
void insert(int val);
|
||||
void del();
|
||||
void print();
|
||||
void makeEmpty();
|
||||
int findMax();
|
||||
int findMin();
|
||||
bool isEmpty();
|
||||
bool isFull();
|
||||
|
||||
private:
|
||||
void insert(int*& heap, int _size, int val);
|
||||
void del(int* heap);
|
||||
void print(int* heap, int _index);
|
||||
void grow(int*& heap, int _size);
|
||||
void siftUp(int* heap, int _index);
|
||||
void siftDown(int* heap, int currentMax);
|
||||
int size, index;
|
||||
int *heap;
|
||||
};
|
||||
|
||||
#endif //MAXHEAP_H
|
Loading…
Reference in New Issue