164 lines
4.3 KiB
C++
164 lines
4.3 KiB
C++
#include "ExtendedBinaryTree.h"
|
|
#include <iostream>
|
|
|
|
ExtendedBinaryTree::ExtendedBinaryTree(Ware *rootNodeKey, int priority) {
|
|
ExtendedBinaryTreeNode *root =
|
|
new ExtendedBinaryTreeNode(rootNodeKey, priority);
|
|
this->rootNode = root;
|
|
}
|
|
|
|
ExtendedBinaryTreeNode *ExtendedBinaryTree::insert(Ware *key, int priority) {
|
|
return this->rootNode->insert(key, priority);
|
|
}
|
|
|
|
ExtendedBinaryTreeNode *ExtendedBinaryTree::search(int value) {
|
|
ExtendedBinaryTreeNode *node = this->rootNode;
|
|
while (value != node->key->getVerkaufspreis() && node != nullptr) {
|
|
if (value > node->key->getVerkaufspreis()) {
|
|
node = node->right;
|
|
} else {
|
|
node = node->left;
|
|
}
|
|
}
|
|
return node;
|
|
}
|
|
|
|
ExtendedBinaryTreeNode *ExtendedBinaryTree::deleteItem(Ware *key) {
|
|
return this->rootNode->deleteItem(key);
|
|
}
|
|
|
|
ExtendedBinaryTreeNode *
|
|
ExtendedBinaryTree::findMin(ExtendedBinaryTreeNode *node) {
|
|
while (node->left != nullptr) {
|
|
node = node->left;
|
|
}
|
|
return node;
|
|
}
|
|
|
|
ExtendedBinaryTreeNode *
|
|
ExtendedBinaryTree::findMax(ExtendedBinaryTreeNode *node) {
|
|
while (node->right != nullptr) {
|
|
node = node->right;
|
|
}
|
|
return node;
|
|
}
|
|
|
|
// function to print a tree in pre-order: (sub)root, left (sub)tree, right
|
|
// (sub)tree
|
|
std::string ExtendedBinaryTree::printPreorder(ExtendedBinaryTreeNode *node) {
|
|
std::stringstream output;
|
|
output << *(node->key) << " " << node->priority << std::endl;
|
|
|
|
if (node->left != nullptr) {
|
|
output << this->printPreorder(node->left);
|
|
}
|
|
if (node->right != nullptr) {
|
|
output << this->printPreorder(node->right);
|
|
}
|
|
return output.str();
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printPreorder() {
|
|
return this->printPreorder(this->rootNode);
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printPostorder() {
|
|
return this->printPostorder(this->rootNode);
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printPostorder(ExtendedBinaryTreeNode *node) {
|
|
std::stringstream output;
|
|
|
|
if (node->left != nullptr) {
|
|
output << this->printPreorder(node->left);
|
|
}
|
|
if (node->right != nullptr) {
|
|
output << this->printPreorder(node->right);
|
|
}
|
|
output << *(node->key) << std::endl;
|
|
return output.str();
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printInorder() {
|
|
return this->printInorder(this->rootNode);
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printInorder(ExtendedBinaryTreeNode *node) {
|
|
std::stringstream output;
|
|
|
|
if (node->left != nullptr) {
|
|
output << this->printPreorder(node->left);
|
|
}
|
|
output << *(node->key) << std::endl;
|
|
|
|
if (node->right != nullptr) {
|
|
output << this->printPreorder(node->right);
|
|
}
|
|
return output.str();
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printPriority() {
|
|
return this->printPriority(this->rootNode, INT_MAX);
|
|
}
|
|
|
|
std::string ExtendedBinaryTree::printPriority(ExtendedBinaryTreeNode *node,
|
|
int max) {
|
|
std::stringstream output;
|
|
|
|
// Print the node
|
|
if (node->priority < max) {
|
|
output << *(node->key) << " Priority: " << node->priority << std::endl;
|
|
}
|
|
|
|
// find out which of the children has lower priority
|
|
ExtendedBinaryTreeNode *bigger = nullptr;
|
|
ExtendedBinaryTreeNode *smaller = nullptr;
|
|
|
|
if (node->left != nullptr) {
|
|
if (node->right != nullptr) {
|
|
smaller = node->left->priority < node->right->priority ? node->left
|
|
: node->right;
|
|
bigger = node->left->priority > node->right->priority ? node->left
|
|
: node->right;
|
|
} else {
|
|
smaller = node->left;
|
|
}
|
|
} else if (node->right != nullptr) {
|
|
smaller = node->right;
|
|
}
|
|
|
|
if (smaller != nullptr) {
|
|
int prio;
|
|
if (bigger != nullptr) {
|
|
if (bigger->priority < max) {
|
|
|
|
prio = bigger->priority;
|
|
} else {
|
|
prio = max;
|
|
}
|
|
} else {
|
|
prio = INT_MAX;
|
|
}
|
|
output << printPriority(smaller, prio);
|
|
}
|
|
if (bigger != nullptr) {
|
|
output << printPriority(bigger, INT_MAX);
|
|
}
|
|
return output.str();
|
|
}
|
|
std::string ExtendedBinaryTree::printPriorityBruteForce() {
|
|
// return ExtendedBinaryTree::printPriorityBruteForce(this->rootNode,
|
|
// INT_MAX);
|
|
int maxprinted = 0;
|
|
ExtendedBinaryTreeNode *maxprintedNode = nullptr;
|
|
while (true) {
|
|
}
|
|
}
|
|
|
|
std::string
|
|
ExtendedBinaryTree::printPriorityBruteForce(ExtendedBinaryTreeNode *key,
|
|
int max) {
|
|
int currentMin = max;
|
|
while (true) {
|
|
}
|
|
} |