Move Folders
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
#include "ExtendedBinaryTree.h"
|
||||
#include <iostream>
|
||||
|
||||
ExtendedBinaryTree::ExtendedBinaryTree(Ware *rootNodeKey) {
|
||||
ExtendedBinaryTreeNode *root = new ExtendedBinaryTreeNode(rootNodeKey);
|
||||
this->rootNode = root;
|
||||
}
|
||||
|
||||
ExtendedBinaryTreeNode *ExtendedBinaryTree::insert(Ware *key) {
|
||||
return this->rootNode->insert(key);
|
||||
}
|
||||
|
||||
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) << 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();
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "ExtendedBinaryTreeNode.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#pragma once
|
||||
|
||||
class ExtendedBinaryTree {
|
||||
public:
|
||||
ExtendedBinaryTreeNode *rootNode;
|
||||
|
||||
ExtendedBinaryTree(Ware *rootNodeKey);
|
||||
|
||||
ExtendedBinaryTreeNode *search(int value);
|
||||
ExtendedBinaryTreeNode *insert(Ware *key);
|
||||
ExtendedBinaryTreeNode *deleteItem(Ware *key);
|
||||
ExtendedBinaryTreeNode *findMin(ExtendedBinaryTreeNode *node);
|
||||
ExtendedBinaryTreeNode *findMax(ExtendedBinaryTreeNode *node);
|
||||
std::string printPreorder(ExtendedBinaryTreeNode *node);
|
||||
std::string printPreorder();
|
||||
std::string printPostorder(ExtendedBinaryTreeNode *node);
|
||||
std::string printPostorder();
|
||||
std::string printInorder(ExtendedBinaryTreeNode *node);
|
||||
std::string printInorder();
|
||||
};
|
||||
|
||||
// std::string printPreorder(BinaryTreeNode* node);
|
||||
// /* -- Your TODO -- */
|
||||
// std::string printPostorder(BinaryTreeNode* node);
|
||||
// std::string printInorder(BinaryTreeNode* node);
|
||||
@@ -0,0 +1,62 @@
|
||||
#include "ExtendedBinaryTreeNode.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
ExtendedBinaryTreeNode::ExtendedBinaryTreeNode(Ware *key) {
|
||||
this->key = key;
|
||||
this->left = nullptr;
|
||||
this->right = nullptr;
|
||||
}
|
||||
|
||||
ExtendedBinaryTreeNode *ExtendedBinaryTreeNode::insert(Ware *key) {
|
||||
if (key->getVerkaufspreis() > this->key->getVerkaufspreis()) {
|
||||
if (this->right == nullptr) {
|
||||
ExtendedBinaryTreeNode *temp = new ExtendedBinaryTreeNode(key);
|
||||
this->right = temp;
|
||||
return this->right;
|
||||
}
|
||||
this->right->insert(key);
|
||||
} else {
|
||||
if (this->left == nullptr) {
|
||||
ExtendedBinaryTreeNode *temp = new ExtendedBinaryTreeNode(key);
|
||||
this->left = temp;
|
||||
return this->left;
|
||||
}
|
||||
this->left->insert(key);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
ExtendedBinaryTreeNode *ExtendedBinaryTreeNode::deleteItem(Ware *key) {
|
||||
ExtendedBinaryTreeNode *node = this;
|
||||
|
||||
if (node == nullptr) {
|
||||
return this;
|
||||
} else if (key->getVerkaufspreis() < node->key->getVerkaufspreis()) {
|
||||
node->left = node->left->deleteItem(key);
|
||||
} else if (key->getVerkaufspreis() > node->key->getVerkaufspreis()) {
|
||||
node->right = node->right->deleteItem(key);
|
||||
} else {
|
||||
if (node->left == nullptr && node->right == nullptr) {
|
||||
delete node;
|
||||
node = nullptr;
|
||||
} else if (node->left == nullptr) { // only children in right subtree
|
||||
ExtendedBinaryTreeNode *temp = node;
|
||||
node = node->right;
|
||||
delete temp;
|
||||
} else if (this->right == nullptr) { // only children in left subtree
|
||||
ExtendedBinaryTreeNode *temp = node;
|
||||
node = node->left;
|
||||
delete temp;
|
||||
} else { // we have to keep the BST structure, here, we look for the minimum
|
||||
// in the right subtree (see lecture)
|
||||
ExtendedBinaryTreeNode *temp = node->right;
|
||||
while (temp->left != nullptr) {
|
||||
temp = temp->left;
|
||||
}
|
||||
node->key = temp->key;
|
||||
node->right = node->right->deleteItem(temp->key);
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Ware.h"
|
||||
|
||||
class ExtendedBinaryTreeNode {
|
||||
public:
|
||||
Ware *key;
|
||||
ExtendedBinaryTreeNode *left;
|
||||
ExtendedBinaryTreeNode *right;
|
||||
|
||||
ExtendedBinaryTreeNode(Ware *key);
|
||||
|
||||
ExtendedBinaryTreeNode *insert(Ware *key);
|
||||
ExtendedBinaryTreeNode *deleteItem(Ware *key);
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
# Name of the binary for Development
|
||||
BINARY = main
|
||||
# Name of the binary for Release
|
||||
FINAL = prototyp
|
||||
# Object files
|
||||
OBJS = Ware.o ExtendedBinaryTreeNode.o ExtendedBinaryTree.o main.o
|
||||
# Compiler flags
|
||||
CFLAGS = -Werror -Wall -std=c++17 -fsanitize=address,undefined -g
|
||||
# Linker flags
|
||||
LFLAGS = -fsanitize=address,undefined
|
||||
#Which Compiler to use
|
||||
COMPILER = c++
|
||||
|
||||
|
||||
# all target: builds all important targets
|
||||
all: binary
|
||||
|
||||
final : ${OBJS}
|
||||
${COMPILER} ${LFLAGS} -o ${FINAL} ${OBJS}
|
||||
|
||||
binary : ${OBJS}
|
||||
${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS}
|
||||
|
||||
# Links the binary
|
||||
${BINARY} : ${OBJS}
|
||||
${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS}
|
||||
|
||||
|
||||
# Compiles a source-file (any file with file extension .c) into an object-file
|
||||
#
|
||||
# "%" is a wildcard which matches every file-name (similar to * in regular expressions)
|
||||
# Such a rule is called a pattern rule (because it matches a pattern, see https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html),
|
||||
# which are a form of so called implicit rules (see https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html)
|
||||
# "$@" and "$<" are so called automatic variables (see https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html)
|
||||
%.o : %.cpp
|
||||
${COMPILER} -c ${CFLAGS} -o $@ $<
|
||||
|
||||
|
||||
# Rules can not only be used for compiling a program but also for executing a program
|
||||
run: ${BINARY}
|
||||
./${BINARY}
|
||||
|
||||
|
||||
# Delete all build artifacts
|
||||
clean :
|
||||
rm -rf ${BINARY} ${OBJS}
|
||||
|
||||
|
||||
# all and clean are a "phony" targets, meaning they are no files
|
||||
.PHONY : all clean
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "Ware.h"
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const Ware &ware) {
|
||||
out << "Name: " << ware.getBezeichnung() << ", SN: " << ware.getSeriennummer()
|
||||
<< ", Gewicht: " << ware.getGewicht()
|
||||
<< ", EK: " << ware.getEinkaufspreis()
|
||||
<< ", VK: " << ware.getVerkaufspreis();
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Ware {
|
||||
private:
|
||||
std::string bezeichnung;
|
||||
int seriennummer;
|
||||
double gewicht;
|
||||
double einkaufspreis;
|
||||
double verkaufspreis;
|
||||
|
||||
public:
|
||||
std::string getBezeichnung() const { return this->bezeichnung; }
|
||||
|
||||
void setBezeichnung(std::string bezeichnung) {
|
||||
this->bezeichnung = bezeichnung;
|
||||
}
|
||||
|
||||
int getSeriennummer() const { return this->seriennummer; }
|
||||
|
||||
void setSeriennummer(int seriennummer) { this->seriennummer = seriennummer; }
|
||||
|
||||
double getGewicht() const { return this->gewicht; }
|
||||
|
||||
void setGewicht(double gewicht) { this->gewicht = gewicht; }
|
||||
|
||||
double getEinkaufspreis() const { return this->einkaufspreis; }
|
||||
|
||||
void setEinkaufspreis(double einkaufspreis) {
|
||||
this->einkaufspreis = einkaufspreis;
|
||||
}
|
||||
|
||||
double getVerkaufspreis() const { return this->verkaufspreis; }
|
||||
|
||||
void setVerkaufspreis(double verkaufspreis) {
|
||||
this->verkaufspreis = verkaufspreis;
|
||||
}
|
||||
|
||||
Ware(std::string bezeichnung, int seriennummer, double gewicht,
|
||||
double einkaufspreis, double verkaufspreis)
|
||||
: bezeichnung(bezeichnung), seriennummer(seriennummer), gewicht(gewicht),
|
||||
einkaufspreis(einkaufspreis), verkaufspreis(verkaufspreis) {}
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const Ware &ware);
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "ExtendedBinaryTree.h"
|
||||
#include "ExtendedBinaryTreeNode.h"
|
||||
#include "Ware.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
Ware ware1("S", 1, 5.12, 10.37, 13.0);
|
||||
Ware ware2("L", 2, 6.12, 1.37, 29.0);
|
||||
Ware ware3("A", 3, 4.12, 9.37, 10.0);
|
||||
Ware ware4("Z", 12, 1.12, 18.37, 19.0);
|
||||
Ware ware5("LA", 27, 0.12, 13.37, 17.0);
|
||||
Ware ware6("LC", 13, 13.12, 15.37, 16.0);
|
||||
Ware ware7("ABC", 4, 7.12, 27.37, 35.0);
|
||||
Ware ware8("C", 123, 9.12, 2.37, 4.0);
|
||||
|
||||
ExtendedBinaryTree tree(&ware1);
|
||||
tree.insert(&ware2);
|
||||
tree.insert(&ware3);
|
||||
tree.insert(&ware4);
|
||||
tree.insert(&ware5);
|
||||
tree.insert(&ware6);
|
||||
tree.insert(&ware7);
|
||||
tree.insert(&ware8);
|
||||
|
||||
// std::cout << ware1 << std::endl;
|
||||
std::cout << tree.printPreorder() << std::endl;
|
||||
std::cout << tree.printPostorder() << std::endl;
|
||||
std::cout << tree.printInorder();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user