forked from gulinghao1847/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeapWithCapacity.cpp
More file actions
150 lines (137 loc) · 3.07 KB
/
MinHeapWithCapacity.cpp
File metadata and controls
150 lines (137 loc) · 3.07 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
148
149
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class MinHeap{
private:
int size;
int capacity;
vector<int> array;
void bubbleUp(int index);
int leftChild(int index);
int rightChild(int index);
int parent(int index);
void bubbleDown(int index);
public:
MinHeap(int);
~MinHeap();
void push(int val);
int top();
void pop();
};
MinHeap::MinHeap(int capacity){
size = 0;
this -> capacity = capacity;
}
MinHeap::~MinHeap(){}
int MinHeap::top(){
if(size == 0) return -1;
return array[0];
}
void MinHeap::push(int val){
if (size < capacity) {
array.push_back(val);
size++;
bubbleUp(size - 1);
}else{
if(val < array[0]){
//do nothing
}else{
array[0] = val;
bubbleDown(0);
}
}
}
int MinHeap::leftChild(int index){
index++;
int result = index * 2;
if(result > size){
return -1;
}
return result - 1;
}
int MinHeap::rightChild(int index){
index++;
int result = index * 2 + 1;
if(result > size){
return -1;
}
return result - 1;
}
int MinHeap::parent(int index){
if(index == 0 || index >= size) return -1;
index++;
return index / 2 - 1;
}
void MinHeap::bubbleUp(int index){
if(index <= 0) return;
int smaller = array[index];
//cout << "smaller : " << index << endl;
while (index > 0) {
int pare = parent(index);
//cout << "pare: " << pare << endl;
smaller = array[index];
if(smaller < array[pare]){
smaller = array[pare];
array[pare] = array[index];
array[index] = smaller;
index = pare;
}else{
break;
}
}
}
void MinHeap::bubbleDown(int index){
while (index < size) {
int localIndex = -1;
int value = INT_MAX;
int left = 0;
int right = 0;
//cout << index << endl;
if((left = leftChild(index)) != -1){
localIndex = left;
value = array[left];
}
//cout << left << endl;
//cout << localIndex << endl;
//cout << value << endl;
if((right = rightChild(index)) != -1 && array[right] < value){
localIndex = right;
value = array[right];
}
if(localIndex != -1 && array[index] > value){
array[localIndex] = array[index];
array[index] = value;
index = localIndex;
}else{
break;
}
}
}
void MinHeap::pop(){
if(size == 0) return;
array[0] = array[size - 1];
size--;
bubbleDown(0);
}
int main(){
MinHeap test(2);
test.push(7);
cout << test.top() << endl;
test.push(8);
cout << test.top() << endl;
test.push(9);
cout << test.top() << endl;
test.push(10);
cout << test.top() << endl;
test.pop();
cout << test.top() << endl;
test.pop();
cout << test.top() << endl;
/*
test.pop();
cout << test.top() << endl;
test.pop();
cout << test.top() << endl;
*/
}