-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.h
120 lines (107 loc) · 2.17 KB
/
Heap.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
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once
#ifndef _HEAP_H
#define _HEAP_H
#include <string>
#include <iostream>
#include <vector>
using namespace std;
template<class Comparable>
class Heap
{
private:
vector<Comparable> arr;
int size;
int left(const int& index) {
if ((index * 2) > size)
return -1;
else
return (index * 2);
}
int right(const int& index) {
if ((index * 2 + 1) > size)
return -1;
else
return (index * 2 + 1);
}
int parent(const int& i) {
if (i >= 1)
return i / 2;
else
return -1;
}
void swap(const int& i, const int& j) {
Comparable temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void percDown(const int& index) {
if (index <= (size/2)-1) {
if (arr[index] > arr[left(index)] && arr[index] > arr[right(index)]) {
if (arr[left(index)] < arr[right(index)]) {
swap(index, left(index));
percDown(left(index));
}
else {
swap(index, right(index));
percDown(right(index));
}
}
else if (arr[index] > arr[left(index)]) {
swap(index, left(index));
percDown(left(index));
}
else if (arr[index] > arr[right(index)]) {
swap(index, right(index));
percDown(right(index));
}
}
}
void percUp(const int& index) {
if (parent(index) != -1 && arr[index] < arr[parent(index)]) {
swap(index, parent(index));
percUp(parent(index));
}
}
void percUp() {
percUp(size);
}
public:
Heap() :arr(vector<Comparable>(1, 0)), size(0) {
arr.reserve(128);
}
Comparable deleteMin() {
if (size > 0) {
Comparable result = arr[1];
arr[1] = arr[size];
size--;
percDown(1);
return result;
}
else {
cout << "deleteMin unsuccesful" << endl;
}
}
void insert(const Comparable& insert) {
size++;
if (arr.size() - 1 <= size) {
arr.push_back(insert);
percUp();
}
else {
arr[size] = insert;
percUp();
}
}
Comparable kthMin(int k) {
vector<Comparable> temp;
for (int i = 0; i < k; i++) {
temp.push_back(deleteMin());
}
Comparable result = temp[temp.size()-1];
for (int i = 0; i < k; i++) {
insert(temp[i]);
}
return result;
}
};
#endif