You are on page 1of 3

/*

* Title: hw5_1.cpp
* Abstract: This program conducts heap operations.
* Author: Harsandeep Singh
* ID: 9430
* Date: 04/13/2021
*/
#include <iostream>
#include <vector>
using namespace std;
class hw5_1 {
public:
vector<int> nums;
void heapify() {
// last non-leaf node
int lastParentIndex = (nums.size() - 2) / 2;
int i = lastParentIndex;
while (i >= 0) {
i = min(i, lastParentIndex);
int leftChildIndex = (2 * i) + 1;
int rightChildIndex = leftChildIndex + 1;
int biggestChildIndex;
// check to see the the last non-leaf node has only one child
if (rightChildIndex >= nums.size()) {
biggestChildIndex = leftChildIndex;
} else {
if (nums[leftChildIndex] > nums[rightChildIndex]) {
biggestChildIndex = leftChildIndex;
} else {
biggestChildIndex = rightChildIndex;
}
}
if (nums[biggestChildIndex] > nums[i]) {
int temp = nums[i];
nums[i] = nums[biggestChildIndex];
nums[biggestChildIndex] = temp;
i = biggestChildIndex;
} else {
i--;
}
}
}
void checkHeap() {
for (int i = 1; i < nums.size(); i++) {
int parentIndex = (i - 1) / 2;
if (nums[i] > nums[parentIndex]) {
cout << "This is NOT a heap." << endl;
heapify();
return;
}
}
cout << "This is a heap." << endl;
}
void displayMax() {
cout << nums[0] << endl;
}
void insert(int n) {
nums.push_back(n);
heapify();
}
void deleteMax() {
nums[0] = nums[nums.size() - 1];
nums.pop_back();
heapify();
}
void deleteNum(int n) {
for (int &num : nums) {
if (num == n) {
num = nums[nums.size() - 1];
nums.pop_back();
heapify();
return;
}
}
}
void update(int index, int n) {
nums[index] = n;
heapify();
}
void display() {
for (int i : nums) {
cout << i << " ";
}
cout << endl;
}
};
int main() {
int length;
cin >> length;
hw5_1 hw5_1;
for (int i = 0; i < length; ++i) {
int temp;
cin >> temp;
hw5_1.nums.push_back(temp);
}
int numberOfCommands;
cin >> numberOfCommands;
cin.ignore();
vector<string> commands(numberOfCommands);
for (int i = 0; i < numberOfCommands; ++i) {
string s;
getline(cin, s);
commands[i] = s;
}
hw5_1.checkHeap();
for (const string &s : commands) {
if (s == "displayMax") {
hw5_1.displayMax();
} else if (s.find("insert") != string::npos) {
int n = stoi(s.substr(6, s.size()));
hw5_1.insert(n);
} else if (s == "deleteMax") {
hw5_1.deleteMax();
} else if (s.find("delete") != string::npos) {
int n = stoi(s.substr(6, s.size()));
hw5_1.deleteNum(n);
} else if (s.find("update") != string::npos) {
int index = stoi(s.substr(6, 8));
int n = stoi(s.substr(8, s.size()));
// use index - 1 b/c he's using 1 based indexing
hw5_1.update(index - 1, n);
} else if (s == "display") {
hw5_1.display();
}
}
return 0;
}

You might also like