From 1c88c1dbd9c9192b3feca50d43fe92a3c1184e62 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 25 Dec 2022 16:13:05 +0200 Subject: [SDP/S08] Solved exercises from seminar --- .../08-Seminar-30.11.2022/exercises.cpp | 164 +++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 Year 2/Structures and data algorithms/08-Seminar-30.11.2022/exercises.cpp (limited to 'Year 2/Structures and data algorithms/08-Seminar-30.11.2022/exercises.cpp') diff --git a/Year 2/Structures and data algorithms/08-Seminar-30.11.2022/exercises.cpp b/Year 2/Structures and data algorithms/08-Seminar-30.11.2022/exercises.cpp new file mode 100644 index 0000000..390ae1b --- /dev/null +++ b/Year 2/Structures and data algorithms/08-Seminar-30.11.2022/exercises.cpp @@ -0,0 +1,164 @@ +#include "tree-utils.h" + +// Exercise 1 + +template +void myInsertInBST(Tree* tree, const T& elem) { + if (elem > tree->data) { + if (tree->right == nullptr) + tree->right = new Tree(elem); + else + myInsertInBST(tree->right, elem); + } + else { + if (tree->left == nullptr) + tree->left = new Tree(elem); + else + myInsertInBST(tree->left, elem); + } +} + +void ex1() { + Tree* t = new Tree(8, new Tree(3, new Tree(1), new Tree(6, nullptr, new Tree(7))), new Tree(10, nullptr, new Tree(14, new Tree(13)))); + myInsertInBST(t, 4); + printTree(t); + + freeTree(t); +} + +// Exercise 2 + +template +bool myContains(Tree* tree, const T& elem) { + if (tree == nullptr) return false; + if (tree->data == elem) return true; + return myContains(tree->left, elem) || myContains(tree->right, elem); +} + +template +bool myContainsBST(Tree* tree, const T& elem) { + if (tree == nullptr) return false; + if (tree->data == elem) + return true; + else if (elem < tree->data) + return myContainsBST(tree->left, elem); + else + return myContainsBST(tree->right, elem); +} + +void ex2() { + Tree* t = new Tree(8, new Tree(3, new Tree(1), new Tree(6, new Tree(4), new Tree(7))), new Tree(10, nullptr, new Tree(14, new Tree(13)))); + std::cout << myContains(t, 9) << " " << myContains(t, 4) << std::endl; + std::cout << myContainsBST(t, 9) << " " << myContainsBST(t, 4) << std::endl; + + freeTree(t); +} + +// Exercise 3 + +template +void maxRec(Tree* tree, T& max) { + if (tree == nullptr) return; + if (tree->data > max) max = tree->data; + maxRec(tree->left, max); + maxRec(tree->right, max); +} + +template +T max(Tree* tree) { + if (tree == nullptr) return T(); + T max = tree->data; + maxRec(tree, max); + return max; +} + +template +T maxBST(Tree* tree) { + if (tree == nullptr) return T(); + while (tree->right != nullptr) tree = tree->right; + return tree->data; +} + +template +void minRec(Tree* tree, T& max) { + if (tree == nullptr) return; + if (tree->data < max) max = tree->data; + minRec(tree->left, max); + minRec(tree->right, max); +} + +template +T min(Tree* tree) { + if (tree == nullptr) return T(); + T max = tree->data; + minRec(tree, max); + return max; +} + +template +T minBST(Tree* tree) { + if (tree == nullptr) return T(); + while (tree->left != nullptr) tree = tree->left; + return tree->data; +} + +void ex3() { + Tree* t = new Tree(8, new Tree(3, new Tree(1), new Tree(6, new Tree(4), new Tree(7))), new Tree(10, nullptr, new Tree(14, new Tree(13)))); + std::cout << max(t) << " " << maxBST(t) << std::endl; + std::cout << min(t) << " " << minBST(t) << std::endl; + + freeTree(t); +} + +// Exercise 4 + +template +void heightRec(Tree* tree, int& result, int current = 0) { + if (tree == nullptr) return; + if (current > result) result = current; + heightRec(tree->left, result, current + 1); + heightRec(tree->right, result, current + 1); +} + +template +int height(Tree* tree) { + int result = 0; + heightRec(tree, result); + return result; +} + +void ex4() { + Tree* t = new Tree(8, new Tree(3, new Tree(1), new Tree(6, new Tree(4), new Tree(7))), new Tree(10, nullptr, new Tree(14, new Tree(13)))); + std::cout << height(t) << std::endl; + + freeTree(t); +} + +// Exercise 5 + +template +void inorderTraversal(Tree* tree, void (*todo)(T)) { + if (tree == nullptr) return; + inorderTraversal(tree->left, todo); + todo(tree->data); + inorderTraversal(tree->right, todo); +} + +template +void print(T elem) { + std::cout << elem << " "; +} + +void ex5() { + Tree* t = new Tree(8, new Tree(3, new Tree(1), new Tree(6, new Tree(4), new Tree(7))), new Tree(10, nullptr, new Tree(14, new Tree(13)))); + inorderTraversal(t, print); // on a BST, it prints all elements, in ascending order + std::cout << std::endl; + + freeTree(t); +} + +// main + +int main() { + ex5(); +} -- cgit v1.3.1-26-gb76a