-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.h
More file actions
148 lines (122 loc) · 3.24 KB
/
heap.h
File metadata and controls
148 lines (122 loc) · 3.24 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef HEAP_H
#define HEAP_H
#include <functional>
#include <stdexcept>
#include <vector>
#include <iostream>
template <typename T, typename Comparator >
class Heap
{
public:
/// Constructs an m-ary heap for any m >= 2
Heap(int m, Comparator c);
/// Destructor as needed
~Heap();
/// Adds an item
void push(const T& item);
/// returns the element at the top of the heap
/// max (if max-heap) or min (if min-heap)
T const & top() const;
/// Removes the top element
void pop();
/// returns true if the heap is empty
bool empty() const;
//allows us to output heap contents
void printHeap() const;
private:
/// Add whatever helper functions and data members you need below
std::vector <T> myArray;
void bubbleUp(int pos);
void trickleDown(int pos);
int m;
Comparator comp;
};
//Constructor
template<typename T, typename Comparator>
Heap<T, Comparator>::Heap(int m, Comparator c) {
this->m=m;
comp=c;
}
//Destructor
template<typename T, typename Comparator>
Heap<T,Comparator>::~Heap(){}
// Add implementation of member functions here
// We will start top() for you to handle the case of
// calling top on an empty heap
template <typename T, typename Comparator>
T const & Heap<T,Comparator>::top() const
{
// Here we use exceptions to handle the case of trying
// to access the top element of an empty heap
if(empty()){
throw std::logic_error("can't top an empty heap");
}
// If we get here we know the heap has at least 1 item
// Add code to return the top element
return myArray[0];
}
// We will start pop() for you to handle the case of
// calling top on an empty heap
template <typename T, typename Comparator>
void Heap<T,Comparator>::pop()
{
if(empty()){
throw std::logic_error("can't pop an empty heap");
}
myArray[0]=myArray.back();
myArray.pop_back();
trickleDown(0);
}
template<typename T, typename Comparator>
void Heap<T,Comparator>::trickleDown(int pos){
//index of left child
int child=m*pos+1;
//index that will switch with pos
int finalChild=child;
if(child>=myArray.size()){//done trickling down
return;
}
for(int i=1; i<m; i++){
//make comparison
if(child+i<myArray.size()&&comp(myArray[child+i], myArray[child])){
finalChild=child+i;
}
}
//switch contents
if(comp(myArray[finalChild], myArray[pos])){
T temp=myArray[finalChild];
myArray[finalChild]=myArray[pos];
myArray[pos]=temp;
trickleDown(finalChild);
}
}
template<typename T, typename Comparator>
bool Heap<T, Comparator>::empty() const{
return myArray.empty();
}
//adding new item
template<typename T, typename Comparator>
void Heap<T, Comparator>::push(const T& item){
myArray.push_back(item);
bubbleUp(myArray.size()-1);
}
template<typename T, typename Comparator>
void Heap<T, Comparator>::bubbleUp(int pos){
//if pos=0, them we are at the top
if(pos>0&&comp(myArray[pos], myArray[(pos-1)/m])){
//need to continue bubbling up
T parent=myArray[(pos-1)/m];
T temp=myArray[pos];
myArray[pos]=parent;
myArray[(pos-1)/m]=temp;
bubbleUp((pos-1)/m);
}
}
//allows for output of heap contents
template<typename T, typename Comparator>
void Heap<T, Comparator>::printHeap() const{
for(unsigned int i=0; i<myArray.size(); i++){
std::cout<<myArray[i]<<std::endl;
}
}
#endif