#include "./tree-utils.h" #include // Exercise 1 template bool isBST(Tree* tree) { if (tree == nullptr) return true; return (tree->left == nullptr || tree->left->data < tree->data) && (tree->right == nullptr || tree->data < tree->right->data) && isBST(tree->left) && isBST(tree->right); } void ex1() { 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 << isBST(t) << std::endl; freeTree(t); } // Exercise 2 template void recBuildBST(std::vector& vec, int start, int end, Tree*& parent) { int mid = start + (end - start) / 2; Tree* current = new Tree(vec[mid]); if (parent == nullptr) parent = current; if (current->data < parent->data) parent->left = current; else if (parent->data < current->data) parent->right = current; if (mid - start > 0) recBuildBST(vec, start, mid - 1, current); if (end - mid > 0) recBuildBST(vec, mid + 1, end, current); } template Tree* vecToBST(std::vector& vec) { Tree* result = nullptr; recBuildBST(vec, 0, vec.size() - 1, result); return result; } void ex2() { std::vector v = { 1, 3, 4, 6, 7, 8, 10, 13, 14 }; Tree* t = vecToBST(v); printTree(t); freeTree(t); } // Exercise 3 template void insertNodeInBST(Tree*& tree, Tree* node) { if (tree == nullptr) { tree = node; return; } insertNodeInBST((tree->data < node->data) ? tree->right : tree->left, node); } template void removeInBST(Tree*& tree, const T& elem) { Tree** current = &tree; while (*current != nullptr && (*current)->data != elem) { current = ((*current)->data < elem) ? &((*current)->right) : &((*current)->left); } if (current == nullptr) return; Tree* oldCurrent = *current; *current = (*current)->right; insertNodeInBST(*current, oldCurrent->left); delete oldCurrent; } 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)))); removeInBST(t, 8); printTree(t); freeTree(t); } // Exercise 4 template bool equal(Tree* a, Tree* b) { if ((a == nullptr) != (b == nullptr)) return false; else if (a == nullptr) return true; return a->data == b->data && equal(a->left, b->left) && equal(a->right, b->right); } void ex4() { Tree* t1 = 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)))); Tree* t2 = 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 << equal(t1, t2) << std::endl; freeTree(t1); freeTree(t2); } // Exercise 5 template void rightRotation(Tree*& tree) { Tree* root = tree; Tree* pivot = tree->left; tree = pivot; root->left = pivot->right; tree->right = root; } template void leftRotation(Tree*& tree) { Tree* root = tree; Tree* pivot = tree->right; tree = pivot; root->right = pivot->left; tree->left = root; } void ex5() { Tree* t = new Tree('Q', new Tree('P', new Tree('A'), new Tree('B')), new Tree('C')); printTree(t); rightRotation(t); printTree(t); leftRotation(t); leftRotation(t); printTree(t); rightRotation(t); printTree(t); freeTree(t); } // Exercise 6 template T pred(Tree* tree, const T& x) { if (tree->value > x) { if (tree->left == nullptr) return tree->value; pred(tree->left, x); } else { if (tree->right == nullptr) return tree->value; pred(tree->right, x); } } void ex6() { } // main int main() { ex5(); }