Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit51d55feba53fb7af4901147922cc9a212e62ecbc (patch)
Author
 Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com>
2022-12-05 15:25:22 +0200
Committer
 GitHub <noreply@github.com>
2022-12-05 15:25:22 +0200
Filesa87a15c9ef56c492d4bd3f43dbe810b74c892936
Data-structures-and-algorithms-51d55feba53fb7af4901147922cc9a212e62ecbc.tar
Data-structures-and-algorithms-51d55feba53fb7af4901147922cc9a212e62ecbc.tar.gz
Data-structures-and-algorithms-51d55feba53fb7af4901147922cc9a212e62ecbc.zip
Parentfdc3c73cd900aa30b25235fbf535447a700f5a8e (diff)
Parent1ca2c9e9702f97e878ed51ecea8f1403e7e67403 (diff)
Merge pull request #4 from MartinSkachkov/solutions
Task 3 & 5 from Seminar 08
2 files changed, 87 insertions, 0 deletions
-rw-r--r--Seminar08/inorder-traversal.cpp23
-rw-r--r--Seminar08/min-max-elem.cpp64
diff --git a/Seminar08/inorder-traversal.cpp b/Seminar08/inorder-traversal.cpp
new file mode 100644
index 0000000..c9a9bd6
--- /dev/null
+++ b/Seminar08/inorder-traversal.cpp
@@ -0,0 +1,23 @@
+#include "tree-utils.h"
+
+template <typename T>
+void inorderTraversal(const Tree<T> *tree) {
+ if (tree == nullptr) {
+ return;
+ }
+
+ inorderTraversal(tree->left);
+ std::cout << tree->data << ' ';
+ inorderTraversal(tree->right);
+}
+
+int main() {
+ Tree<int> *root = new Tree<int>(8, new Tree<int>(3, new Tree<int>(1), new Tree<int>(6, new Tree<int>(4), new Tree<int>(7))), new Tree<int>(10, nullptr, new Tree<int>(14, new Tree<int>(13))));
+
+ inorderTraversal(root); //изпечатва елементите на дървото сортирано
+ std::cout << '\n';
+ printTree(root);
+
+ freeTree(root);
+ return 0;
+} \ No newline at end of file
diff --git a/Seminar08/min-max-elem.cpp b/Seminar08/min-max-elem.cpp
new file mode 100644
index 0000000..cbd8560
--- /dev/null
+++ b/Seminar08/min-max-elem.cpp
@@ -0,0 +1,64 @@
+#include "tree-utils.h"
+#include <cmath>
+
+// for regular binary trees
+template <typename T>
+T max_elem(Tree<T> *tree) {
+ T res = tree->data;
+
+ if (tree->left != nullptr) {
+ res = std::max(res, max_elem(tree->left));
+ }
+ if (tree->right != nullptr) {
+ res = std::max(res, max_elem(tree->right));
+ }
+
+ return res;
+}
+
+template <typename T>
+T min_elem(Tree<T> *tree) {
+ T res = tree->data;
+
+ if (tree->left != nullptr) {
+ res = std::min(res, min_elem(tree->left));
+ }
+ if (tree->right != nullptr) {
+ res = std::min(res, min_elem(tree->right));
+ }
+
+ return res;
+}
+
+// for binary search trees
+template <typename T>
+T max_elem_bst(Tree<T> *tree) {
+ while (tree->right != nullptr)
+ tree = tree->right;
+
+ return tree->data;
+}
+
+template <typename T>
+T min_elem_bst(Tree<T> *tree) {
+ while (tree->left != nullptr)
+ tree = tree->left;
+
+ return tree->data;
+}
+
+int main() {
+ Tree<int> *regularTree = new Tree<int>(2, new Tree<int>(3, new Tree<int>(4), new Tree<int>(5)), new Tree<int>(13, new Tree<int>(7), new Tree<int>(8)));
+ printTree(regularTree);
+ std::cout << "Maximum element in regular binary tree: " << max_elem(regularTree) << '\n';
+ std::cout << "Minimum element in regular binary tree: " << min_elem(regularTree) << '\n';
+
+ Tree<int> *binarySearchTree = new Tree<int>(8, new Tree<int>(3, new Tree<int>(1), new Tree<int>(6, new Tree<int>(4), new Tree<int>(7))), new Tree<int>(10, nullptr, new Tree<int>(14, new Tree<int>(13))));
+ printTree(binarySearchTree);
+ std::cout << "Maximum element in bs tree: " << max_elem_bst(binarySearchTree) << '\n';
+ std::cout << "Minimum element in bs tree: " << min_elem_bst(binarySearchTree) << '\n';
+
+ freeTree(regularTree);
+ freeTree(binarySearchTree);
+ return 0;
+} \ No newline at end of file