From c516610609be1e62628badbe4fc25f5775275be2 Mon Sep 17 00:00:00 2001 From: Desislava Shunina <115353472+desiish@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:40:15 +0200 Subject: add gifs and example function --- Practicum/pract07/ExampleFunctions.cpp | 85 ++++++++++++++++++++++++++++ Practicum/pract07/Traversals.cpp | 98 +++++++++++++++++++++++++++++++++ Practicum/pract07/binaryTree.png | Bin 0 -> 22798 bytes Practicum/pract07/inorder.gif | Bin 0 -> 186492 bytes Practicum/pract07/postorder.gif | Bin 0 -> 176498 bytes Practicum/pract07/preorder.gif | Bin 0 -> 157163 bytes Practicum/pract07/tree-dfs-vs-bfs.gif | Bin 0 -> 90580 bytes 7 files changed, 183 insertions(+) create mode 100644 Practicum/pract07/ExampleFunctions.cpp create mode 100644 Practicum/pract07/Traversals.cpp create mode 100644 Practicum/pract07/binaryTree.png create mode 100644 Practicum/pract07/inorder.gif create mode 100644 Practicum/pract07/postorder.gif create mode 100644 Practicum/pract07/preorder.gif create mode 100644 Practicum/pract07/tree-dfs-vs-bfs.gif (limited to 'Practicum/pract07') diff --git a/Practicum/pract07/ExampleFunctions.cpp b/Practicum/pract07/ExampleFunctions.cpp new file mode 100644 index 0000000..9e81f98 --- /dev/null +++ b/Practicum/pract07/ExampleFunctions.cpp @@ -0,0 +1,85 @@ +#include +#include +#include + +template +struct TreeNode { + T data; + TreeNode* left; + TreeNode* right; + + TreeNode(const T& val) : data(val), left(nullptr), right(nullptr) {} +}; + +// Проверка дали даден възел е листо +// Листо е възел, който няма деца +template +bool isLeaf(TreeNode* node) { + return node && !node->left && !node->right; +} + +// Проверка дали даден елемент се съдържа в дърво +template +bool contains(TreeNode* root, T value) { + if (root == nullptr) return false; + + if (root->data == value) return true; + + return contains(root->left, value) || contains(root->right, value); +} + +// Намиране на височина на дърво +// Височината на дърво е дължината на най-дългия път от корена до някое листо +template +int getHeight(TreeNode* root) { + if (!root) return -1; + + int leftHeight = getHeight(root->left); + int rightHeight = getHeight(root->right); + + return 1 + std::max(leftHeight, rightHeight); +} + +// Преброяване на възлите в дърво +template +int countNodes(TreeNode* root) { + if (!root) return 0; + + if (!root->left && !root->right) return 1; + + return 1 + countNodes(root->left) + countNodes(root->right); +} + +int main() { + // Създаване на двоично дърво от цели числа + TreeNode* root = new TreeNode(1); + root->left = new TreeNode(2); + root->right = new TreeNode(3); + root->left->left = new TreeNode(4); + root->left->right = new TreeNode(5); + root->right->left = new TreeNode(6); + root->right->right = new TreeNode(7); + + // Как изглежда дървото: + // 1 + // / \ + // 2 3 + // / \ / \ + // 4 5 6 7 + + // Проверка дали даден възел е листо + std::cout << "Is 4 a leaf: " << std::boolalpha << isLeaf(root->left->left) << std::endl; // true + std::cout << "Is 3 a leaf: " << std::boolalpha << isLeaf(root->right) << std::endl; // false + + // Проверка дали даден елемент се съдържа в дървото + std::cout << "Does the tree contain 5: " << std::boolalpha << contains(root, 5) << std::endl; // true + std::cout << "Does the tree contain 8: " << std::boolalpha << contains(root, 8) << std::endl; // false + + // Намиране на височината на дървото + std::cout << "Height of the tree: " << getHeight(root) << std::endl; // 2 + + // Преброяване на възлите в дървото + std::cout << "Total nodes in the tree: " << countNodes(root) << std::endl; // 7 + + return 0; +} diff --git a/Practicum/pract07/Traversals.cpp b/Practicum/pract07/Traversals.cpp new file mode 100644 index 0000000..61f8bb1 --- /dev/null +++ b/Practicum/pract07/Traversals.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + +template +struct TreeNode { + T data; + TreeNode* left; + TreeNode* right; + + TreeNode(const T& val) : data(val), left(nullptr), right(nullptr) {} +}; + +// Preorder обхождане (корен, ляво, дясно) +template +void preorderTraversal(TreeNode* root) { + if (!root) return; + std::cout << root->data << " "; + preorderTraversal(root->left); + preorderTraversal(root->right); +} + +// Inorder обхождане (ляво, корен, дясно) +template +void inorderTraversal(TreeNode* root) { + if (!root) return; + inorderTraversal(root->left); + std::cout << root->data << " "; + inorderTraversal(root->right); +} + +// Postorder обхождане (ляво, дясно, корен) +template +void postorderTraversal(TreeNode* root) { + if (!root) return; + postorderTraversal(root->left); + postorderTraversal(root->right); + std::cout << root->data << " "; +} + +// Обхождане в широчина +template +void bfs(TreeNode* root) { + if (!root) return; + + std::queue*> q; + q.push(root); + + while (!q.empty()) { + TreeNode* current = q.front(); + q.pop(); + + std::cout << current->data << " "; + + if (current->left) q.push(current->left); + if (current->right) q.push(current->right); + } +} + +int main() { + // Създаване на двоично дърво от цели числа + TreeNode* root = new TreeNode(1); + root->left = new TreeNode(2); + root->right = new TreeNode(3); + root->left->left = new TreeNode(4); + root->left->right = new TreeNode(5); + root->right->left = new TreeNode(6); + root->right->right = new TreeNode(7); + + // Как изглежда дървото: + // 1 + // / \ + // 2 3 + // / \ / \ + // 4 5 6 7 + + // Preorder обхождане + std::cout << "Preorder traversal: "; + preorderTraversal(root); // 1 2 4 5 3 6 7 + std::cout << std::endl; + + // Inorder обхождане + std::cout << "Inorder traversal: "; + inorderTraversal(root); // 4 2 5 1 6 3 7 + std::cout << std::endl; + + // Postorder обхождане + std::cout << "Postorder traversal: "; + postorderTraversal(root); // 4 5 2 6 7 3 1 + std::cout << std::endl; + + // Обхождане в широчина + std::cout << "BFS traversal: "; + bfs(root); // 1 2 3 4 5 6 7 + std::cout << std::endl; + + return 0; +} diff --git a/Practicum/pract07/binaryTree.png b/Practicum/pract07/binaryTree.png new file mode 100644 index 0000000..01a9e2a Binary files /dev/null and b/Practicum/pract07/binaryTree.png differ diff --git a/Practicum/pract07/inorder.gif b/Practicum/pract07/inorder.gif new file mode 100644 index 0000000..d807a10 Binary files /dev/null and b/Practicum/pract07/inorder.gif differ diff --git a/Practicum/pract07/postorder.gif b/Practicum/pract07/postorder.gif new file mode 100644 index 0000000..023f302 Binary files /dev/null and b/Practicum/pract07/postorder.gif differ diff --git a/Practicum/pract07/preorder.gif b/Practicum/pract07/preorder.gif new file mode 100644 index 0000000..17ae1b0 Binary files /dev/null and b/Practicum/pract07/preorder.gif differ diff --git a/Practicum/pract07/tree-dfs-vs-bfs.gif b/Practicum/pract07/tree-dfs-vs-bfs.gif new file mode 100644 index 0000000..4fdbc4a Binary files /dev/null and b/Practicum/pract07/tree-dfs-vs-bfs.gif differ -- cgit v1.3.1-26-gb76a