-
Notifications
You must be signed in to change notification settings - Fork 14
/
binary_heap.cpp
152 lines (124 loc) · 2.68 KB
/
binary_heap.cpp
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 <climits>
using namespace std;
void swap(int *x, int *y);
class min_heap
{
public:
min_heap(int capacity);
void minHeapify(int i);
int parent(int i) {return (i-1)/2;}
int left(int i) {return (2*i + 1);}
int right(int i) {return (2*i + 2);}
int extractMin();
void decreaseKey(int i, int new_val);
int getMin() {return harr[0];}
void deleteKey(int i);
void insertKey(int k);
void print_heap();
private:
int *harr; //pointer to array of elements in heap
int capacity; //maximum heap size
int heap_size; //current number of elements
};
min_heap::min_heap(int cap)
{
heap_size = 0;
capacity = cap;
harr = new int[cap];
}
void min_heap::insertKey(int k)
{
if (heap_size == capacity)
{
cout << "\nmax capacity: could not insert key\n";
return;
}
heap_size++;
int i = heap_size - 1;
harr[i] = k;
while (i != 0 && harr[parent(i)] > harr[i])
{
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
void min_heap::decreaseKey(int i, int new_val)
{
harr[i] = new_val;
while (i!=0 && harr[parent(i)] > harr[i])
{
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
int min_heap::extractMin()
{
if (heap_size <= 0)
return INT_MAX;
if (heap_size == 1)
{
heap_size--;
return harr[0];
}
int root = harr[0];
harr[0] = harr[heap_size-1];
heap_size--;
minHeapify(0);
return root;
}
void min_heap::deleteKey(int i)
{
decreaseKey(i, INT_MIN);
extractMin();
}
void min_heap::minHeapify(int i)
{
//recursive method to heapify a subtree rooted at i
//assumes subtrees are already heapified
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size && harr[l] < harr[i])
smallest = l;
if (r < heap_size && harr[r] < harr[smallest])
smallest = r;
if (smallest != i)
{
swap(&harr[i], &harr[smallest]);
minHeapify(smallest);
}
}
void min_heap::print_heap()
{
for (int i = 0; i < heap_size; ++i)
{
cout << harr[i] << " ";
}
cout << endl;
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
min_heap h(11);
h.insertKey(3);
h.insertKey(2);
h.insertKey(1);
h.insertKey(15);
h.insertKey(5);
h.insertKey(4);
h.insertKey(45);
cout << "binary heap: " << endl;
h.print_heap();
cout << "extract min: " << h.extractMin() << endl;
cout << "get min: " << h.getMin() << endl;
cout << "decrease key: 2->1 and get min: ";
h.decreaseKey(2, 1);
cout << h.getMin() << endl;
return 0;
}