-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryHeap.h
62 lines (50 loc) · 1.58 KB
/
BinaryHeap.h
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
/*
* BinaryHeap.h
*
* Description: This is a min binary heap class that stores elements in a binary tree.
*
* Class Invariant: Smallest element is always at the root.
*
* Author: Asheesh Yadav + Eeshan V
* Last Modified: April 5, 2024
*/
#ifndef BINARY_HEAP_H
#define BINARY_HEAP_H
#include "Event.h"
#include "EmptyDataCollectionException.h"
#include <iostream>
// Declaration of the BinaryHeap class template
template <class ElementType>
class BinaryHeap {
private:
ElementType* elements; // Array to store elements
unsigned int elementCount; // Number of elements in the heap
unsigned int capacity; // Capacity of the heap
// Re-heapify down operation
// Time Complexity: O(log n)
void reHeapDown(unsigned int indexOfRoot);
// Re-heapify up operation
// Time Complexity: O(log n)
void reHeapUp(unsigned int indexOfChild);
public:
// Constructor: Initializes an empty binary heap
// Time Complexity: O(1)
BinaryHeap();
// Destructor: Deallocates memory
// Time Complexity: O(1)
~BinaryHeap();
// Returns the number of elements in the heap
// Time Complexity: O(1)
unsigned int getElementCount() const;
// Inserts a new element into the heap
// Time Complexity: O(log n)
bool insert(ElementType & newElement);
// Removes the root element from the heap
// Time Complexity: O(log n)
void remove();
// Retrieves the root element of the heap
// Time Complexity: O(1)
ElementType & retrieve() const;
};
#include "BinaryHeap.cpp" // Include the implementation file
#endif //BINARY_HEAP_H