#include #include #include using namespace std; // Krone class class Krone { private: int wholeValue; int fractionalValue; public: Krone() {} Krone(int whole, int fraction) : wholeValue(whole), fractionalValue(fraction) {} int getWhole() const { return wholeValue; } int getFraction() const { return fractionalValue; } bool operator<(const Krone& other) const { if (wholeValue < other.wholeValue) { return true; } else if (wholeValue == other.wholeValue) { return fractionalValue < other.fractionalValue; } else { return false; } } friend ostream& operator<<(ostream& out, const Krone& krone) { out << "Kr " << krone.wholeValue << "." << krone.fractionalValue; return out; } }; // BST Node class class BSTNode { private: Krone data; BSTNode* left; BSTNode* right; public: BSTNode() {} BSTNode(const Krone& krone) : data(krone), left(nullptr), right(nullptr) {} Krone getData() const { return data; } BSTNode* getLeft() const { return left; } BSTNode* getRight() const { return right; } void setData(const Krone& krone) { data = krone; } void setLeft(BSTNode* node) { left = node; } void setRight(BSTNode* node) { right = node; } }; // BST class class BST { protected: BSTNode* root; public: BST() : root(nullptr) {} BSTNode* getRoot() const { return root; } bool isEmpty() const { return root == nullptr; } int countNodes(BSTNode* node) const { if (node == nullptr) { return 0; } else { return 1 + countNodes(node->getLeft()) + countNodes(node->getRight()); } }
can you fix
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
// Krone class
class Krone {
private:
int wholeValue;
int fractionalValue;
public:
Krone() {}
Krone(int whole, int fraction) : wholeValue(whole), fractionalValue(fraction) {}
int getWhole() const { return wholeValue; }
int getFraction() const { return fractionalValue; }
bool operator<(const Krone& other) const {
if (wholeValue < other.wholeValue) {
return true;
}
else if (wholeValue == other.wholeValue) {
return fractionalValue < other.fractionalValue;
}
else {
return false;
}
}
friend ostream& operator<<(ostream& out, const Krone& krone) {
out << "Kr " << krone.wholeValue << "." << krone.fractionalValue;
return out;
}
};
// BST Node class
class BSTNode {
private:
Krone data;
BSTNode* left;
BSTNode* right;
public:
BSTNode() {}
BSTNode(const Krone& krone) : data(krone), left(nullptr), right(nullptr) {}
Krone getData() const { return data; }
BSTNode* getLeft() const { return left; }
BSTNode* getRight() const { return right; }
void setData(const Krone& krone) { data = krone; }
void setLeft(BSTNode* node) { left = node; }
void setRight(BSTNode* node) { right = node; }
};
// BST class
class BST {
protected:
BSTNode* root;
public:
BST() : root(nullptr) {}
BSTNode* getRoot() const { return root; }
bool isEmpty() const { return root == nullptr; }
int countNodes(BSTNode* node) const {
if (node == nullptr) {
return 0;
}
else {
return 1 + countNodes(node->getLeft()) + countNodes(node->getRight());
}
}
void empty(BSTNode* node) {
if (node != nullptr) {
empty(node->getLeft());
empty(node->getRight());
delete node;
}
}
void insertNode(const Krone& krone) {
BSTNode* node = new BSTNode(krone);
if (isEmpty()) {
root = node;
}
else {
BSTNode* currNode = root;
while (true) {
if (krone < currNode->getData()) {
if (currNode->getLeft() == nullptr) {
currNode->setLeft(node);
break;
}
else {
currNode = currNode->getLeft();
}
}
else {
if (currNode->getRight() == nullptr) {
currNode->setRight(node);
break;
}
else {
currNode = currNode->getRight();
}
}
}
}
}
BSTNode* searchNode(const Krone& krone) const {
BSTNode* currNode = root;
while (currNode != nullptr) {
if (krone < currNode->getData()) {
currNode = currNode->getLeft();
}
else if (currNode->getData() < krone) {
currNode = currNode->getRight();
}
else {
// krone == currNode->getData()
return currNode;
}
}
// krone not found in tree
return nullptr;
}
class MinHeap : public BST {
public:
MinHeap() : BST() {}
void insertNode(const Krone& krone) {
BSTNode* node = new BSTNode(krone);
if (isEmpty()) {
root = node;
}
else {
BSTNode* parent = nullptr;
BSTNode* currNode = root;
queue<BSTNode*> bfsQueue;
bfsQueue.push(currNode);
while (!bfsQueue.empty()) {
parent = currNode;
currNode = bfsQueue.front();
bfsQueue.pop();
if (currNode->getLeft() == nullptr) {
currNode->setLeft(node);
break;
}
else if (currNode->getRight() == nullptr) {
currNode->setRight(node);
break;
}
bfsQueue.push(currNode->getLeft());
bfsQueue.push(currNode->getRight());
}
if (node->getData() < parent->getData()) {
swap(node->setData(parent->getData()), parent->setData(node->getData()));
heapify(parent);
}
}
}
void deleteNode(BSTNode* nodeToDelete) {
if (nodeToDelete == nullptr) {
return;
}
BSTNode* lastNode = root;
queue<BSTNode*> bfsQueue;
bfsQueue.push(lastNode);
while (!bfsQueue.empty()) {
lastNode = bfsQueue.front();
bfsQueue.pop();
if (lastNode->getLeft() != nullptr) {
bfsQueue.push(lastNode->getLeft());
}
if (lastNode->getRight() != nullptr) {
bfsQueue.push(lastNode->getRight());
}
}
nodeToDelete->setData(lastNode->getData());
if (lastNode == root) {
root = nullptr;
}
else {
BSTNode* parent = root;
while (parent != nullptr) {
if (parent->getLeft() == lastNode || parent->getRight() == lastNode) {
break;
}
else if (lastNode->getData() < parent->getData()) {
parent = parent->getLeft();
}
else {
parent = parent->getRight();
}
}
if (parent->getLeft() == lastNode) {
parent->setLeft(nullptr);
}
else {
parent->setRight(nullptr);
}
heapify(parent);
}
delete lastNode;
}
private:
void heapify(BSTNode* node) {
if (node == nullptr) {
return;
}
BSTNode* smallest = node;
if (node->getLeft() != nullptr && node->getLeft()->getData() < smallest->getData()) {
smallest = node->getLeft();
}
if (node->getRight() != nullptr && node->getRight()->getData() < smallest->getData()) {
smallest = node->getRight();
}
if (smallest != node) {
swap(node->getData(), smallest->getData());
heapify(smallest);
}
}
};
int main() {
// Create MinHeap object
MinHeap heap;
Step by step
Solved in 2 steps