From 7e1d2fc945be0fec6d9e3b97150b504cefc88e97 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Wed, 1 Jun 2022 19:27:14 +0200 Subject: [PATCH] First Draft of 2.1 --- Uebung 2/2.1/ExtendedBinaryTree.cpp | 62 +++++++++++++++++++++++++++ Uebung 2/2.1/ExtendedBinaryTree.h | 25 +++++++++++ Uebung 2/2.1/ExtendedBinaryTreeNode.h | 14 ++++++ Uebung 2/2.1/Makefile | 51 ++++++++++++++++++++++ Uebung 2/2.1/Ware.h | 46 ++++++++++++++++++++ Uebung 2/2.1/main.cpp | 3 ++ 6 files changed, 201 insertions(+) create mode 100644 Uebung 2/2.1/ExtendedBinaryTree.cpp create mode 100644 Uebung 2/2.1/ExtendedBinaryTree.h create mode 100644 Uebung 2/2.1/ExtendedBinaryTreeNode.h create mode 100644 Uebung 2/2.1/Makefile create mode 100644 Uebung 2/2.1/Ware.h create mode 100644 Uebung 2/2.1/main.cpp diff --git a/Uebung 2/2.1/ExtendedBinaryTree.cpp b/Uebung 2/2.1/ExtendedBinaryTree.cpp new file mode 100644 index 0000000..1de58e6 --- /dev/null +++ b/Uebung 2/2.1/ExtendedBinaryTree.cpp @@ -0,0 +1,62 @@ +#include "ExtendedBinaryTree.h" +#include + +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); +} \ No newline at end of file diff --git a/Uebung 2/2.1/ExtendedBinaryTree.h b/Uebung 2/2.1/ExtendedBinaryTree.h new file mode 100644 index 0000000..e7f95d8 --- /dev/null +++ b/Uebung 2/2.1/ExtendedBinaryTree.h @@ -0,0 +1,25 @@ +#include "ExtendedBinaryTreeNode.h" +#include +#include + +#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 printPreorder(BinaryTreeNode* node); +// /* -- Your TODO -- */ +// std::string printPostorder(BinaryTreeNode* node); +// std::string printInorder(BinaryTreeNode* node); \ No newline at end of file diff --git a/Uebung 2/2.1/ExtendedBinaryTreeNode.h b/Uebung 2/2.1/ExtendedBinaryTreeNode.h new file mode 100644 index 0000000..85397a6 --- /dev/null +++ b/Uebung 2/2.1/ExtendedBinaryTreeNode.h @@ -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); +}; \ No newline at end of file diff --git a/Uebung 2/2.1/Makefile b/Uebung 2/2.1/Makefile new file mode 100644 index 0000000..918ffe9 --- /dev/null +++ b/Uebung 2/2.1/Makefile @@ -0,0 +1,51 @@ + +# Name of the binary for Development +BINARY = main +# Name of the binary for Release +FINAL = prototyp +# Object files +OBJS = 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 diff --git a/Uebung 2/2.1/Ware.h b/Uebung 2/2.1/Ware.h new file mode 100644 index 0000000..b072d56 --- /dev/null +++ b/Uebung 2/2.1/Ware.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include + +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); \ No newline at end of file diff --git a/Uebung 2/2.1/main.cpp b/Uebung 2/2.1/main.cpp new file mode 100644 index 0000000..eb6ba1e --- /dev/null +++ b/Uebung 2/2.1/main.cpp @@ -0,0 +1,3 @@ +#include "ExtendedBinaryTreeNode.h" + +int main(){}; \ No newline at end of file