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