-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxheap.h
108 lines (89 loc) · 1.89 KB
/
maxheap.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
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
/*
* maxheap.h
*
* Specifications for MaxHeap class
*
* @author Juan Arias
*
*/
#ifndef MAXHEAP_H
#define MAXHEAP_H
#include "heap.h"
/*
* A MaxHeap is an implementation of the Heap interface that prioritizes
* the maximum value.
*/
template <class T>
class MaxHeap: public Heap<T> {
public:
/*
* Constructs empty heap
*/
MaxHeap();
/*
* Constructs heap from given array
* @param arr The array to construct heap from
* @param size The size of arr
*/
MaxHeap(const T arr[], int size);
/*
* Copy constructor overload
* @param other The other heap to copy
*/
MaxHeap(const Heap<T>& other);
/*
* Destroys heap and deallocates all dynamic memory
*/
virtual ~MaxHeap();
/*
* Assignment operator overload
* @param other The other heap to copy
* @return this heap by reference
*/
Heap<T>& operator=(const Heap<T>& other) override;
/*
* Remove the peek item in the heap
*/
void remove() override;
/*
* Add item to the heap
* @param item The item to add to the heap
*/
void add(const T& item) override;
/*
* Remove the peek item in the heap
*/
bool contains(const T& item) override;
/*
* Static method
* Heap sorts the given array
* @param arr The array to sort
* @param size The size of arr
*/
static void maxHeapSort(T arr[], int size);
private:
/*
* Helper function for array constructor
*/
void create();
/*
* Bubbles node up heap until in correct position
* @param curr The current node in the heap
*/
void bubbleUp(Node curr);
/*
* Trickles nodes down heap until in correct position
* @param curr The current node in the heap
*/
void rebuild(Node curr);
/*
* Static method
* Trickles nodes down given heap array until in correct position
* @param arr The heap array to rebuild
* @param size The size of arr
* @param curr The current node in the heap
*/
static void rebuild(T arr[], int size, Node curr);
};
#include "maxheap.cpp"
#endif // MAXHEAP_H