#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()); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;

1.
2. Kr 23.44
Kr 57.12
3.
4. Kr 68.99
5. Kr 111.22
Kr 87.43
6. Kr 44.55
7. Kr 77.77
8. Kr 18.36
9. Kr 543.21
10. Kr 20.21
11. Kr 345.67
12. Kr 36.18
13. Kr 48.48
14. Kr 101.00
15. Kr 11.00
16. Kr 21.00
17. Kr 51.00
18. Kr 1.00
19. Kr 251.00
20. Kr 151.00
Transcribed Image Text:1. 2. Kr 23.44 Kr 57.12 3. 4. Kr 68.99 5. Kr 111.22 Kr 87.43 6. Kr 44.55 7. Kr 77.77 8. Kr 18.36 9. Kr 543.21 10. Kr 20.21 11. Kr 345.67 12. Kr 36.18 13. Kr 48.48 14. Kr 101.00 15. Kr 11.00 16. Kr 21.00 17. Kr 51.00 18. Kr 1.00 19. Kr 251.00 20. Kr 151.00
• Derive a MinHeap class from the BST class of your Lab 4 code.
• Define/Override only the Search/Insert/Delete methods in the new class.
. Write a new main for this lab which:
• Will only be a test program for the MinHeap.
. Declares and creates a MinHeap object as necessary.
• Declares and creates the Krone objects as necessary.
• Inserts the 20 Krone objects specified in Lab 4 in the same order.
• Performs all 4 traversals after the inserting the 10th and the last objects - in total there will be 8 outputs which should be clearly demarcated.
• No user input is necessary, no data validation is necessary.
. For submission - upload all class files from Lab 4 as necessary as well as your new MinHeap class and the main. Remember to also include adequate
number of screenshots of the program execution.
• The Discussion forum will be monitored and responded to all week during the Finals. Office Hours will only be held on MTW. For clarifications, post your
questions on the Discussion forum first.
Transcribed Image Text:• Derive a MinHeap class from the BST class of your Lab 4 code. • Define/Override only the Search/Insert/Delete methods in the new class. . Write a new main for this lab which: • Will only be a test program for the MinHeap. . Declares and creates a MinHeap object as necessary. • Declares and creates the Krone objects as necessary. • Inserts the 20 Krone objects specified in Lab 4 in the same order. • Performs all 4 traversals after the inserting the 10th and the last objects - in total there will be 8 outputs which should be clearly demarcated. • No user input is necessary, no data validation is necessary. . For submission - upload all class files from Lab 4 as necessary as well as your new MinHeap class and the main. Remember to also include adequate number of screenshots of the program execution. • The Discussion forum will be monitored and responded to all week during the Finals. Office Hours will only be held on MTW. For clarifications, post your questions on the Discussion forum first.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education