-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryHeap.cpp
126 lines (92 loc) · 3.18 KB
/
BinaryHeap.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
//============================================================================
// Name : BinaryHeap.cpp
// Author : Hasan Sozer
// Version : 1.2
// Copyright : (c) 2013 Ozyegin University
// Description : Includes the (incomplete) implementation file of the
// BinaryHeap class that will be used as part of the program
// to be submitted as Homework 3, which is assigned in the
// context of the course CS201, Data Structures and Algorithms.
//
// THE IMPLEMENTATION MUST BE COMPLETED TO MAKE ALL THE TESTS
// IN THE MAIN METHOD PASS.
//
// After completing the heap implementation, "BinaryHeap.h" and
// "BinaryHeap.cpp" files will be used for extending homework 1,
// and the extended program will be submitted as Homework 3.
//============================================================================
#include "BinaryHeap.h"
BinaryHeap::BinaryHeap(int capacity) {
this->capacity = capacity;
// The element at index 0 is not used!
// The root element will be placed at index 1
heap = new int[capacity+1];
size = 0;
}
BinaryHeap::~BinaryHeap() {
delete [] heap;
}
void BinaryHeap::insert(int element) {
if(size < capacity) {
int hole = ++size;
heap[hole] = element;
while(hole > 1 && heap[hole] < heap[hole / 2]){
swap(hole / 2, hole);
hole /= 2;
}
}
// TO BE COMPLETED
// The capacity of the heap is assumed to be fixed.
// Insert the element if size < capacity
// Do nothing otherwise.
// After the new element is inserted, perform a percolate up operation here.
// You can add a percolateUp method to the class,
// or just implement the operations within this insert method.
}
void BinaryHeap::deleteMin() {
if(size>=1)
{
heap[1]=heap[size];
size--;
percolateDown(1);
}
}
// TO BE COMPLETED
// If the size is less than 1, do nothing and stop
// Otherwise, replace the root of the heap with the last element on the last level
// Then, call the percolateDown function by providing the index of the root node, i.e., 1
int BinaryHeap::getMin() {
if(size<1)
{
return -1;
}
else
return heap[1];
// TO BE COMPLETED
// If the size is less than 1, return -1
// Otherwise, return the value of the root node
}
void BinaryHeap::percolateDown(int hole) {
// TO BE COMPLETED
// Compare the node with its children; if they are in the correct order, stop
// Otherwise, swap the element with the smallest child
// Repeat the operation for the swapped child node
int min_index = hole;
int left = hole * 2;
int right = hole * 2 + 1;
if (left <= size && heap[left] < heap[min_index]) {
min_index = left;
}
if (right <= size && heap[right] < heap[min_index]) {
min_index = right;
}
if (min_index != hole) {
swap(hole, min_index);
percolateDown(min_index);
}
}
void BinaryHeap::swap(int i, int j) {
int t = heap[i];
heap[i] = heap[j];
heap[j] = t;
}