From 61d7d0495ee6270bbe85640569b4bb980e66d1bc Mon Sep 17 00:00:00 2001 From: Adarsh Kishore <76656932+adarsh-kishore786@users.noreply.github.com> Date: Tue, 14 Sep 2021 19:14:42 +0530 Subject: [PATCH] chore(CPlusPlus): add binary tree implementation (#462) --- algorithms/CPlusPlus/README.md | 1 + .../Trees/binary-tree-implementation.cpp | 175 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 algorithms/CPlusPlus/Trees/binary-tree-implementation.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index bae45a72..bb4a3c17 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -107,6 +107,7 @@ 9. [Min Heap](Trees/min-heap.cpp) 10. [Finding the height of a given tree](Trees/Height-Of-Tree.cpp) 11. [Finding the elements of a tree visible from top view](Trees/Top-View-Of-A-Tree.cpp) +12. [Binary Tree Implementation](Trees/binary-tree-implementation.cpp) # Maths diff --git a/algorithms/CPlusPlus/Trees/binary-tree-implementation.cpp b/algorithms/CPlusPlus/Trees/binary-tree-implementation.cpp new file mode 100644 index 00000000..207986c1 --- /dev/null +++ b/algorithms/CPlusPlus/Trees/binary-tree-implementation.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + +using dType = int; + +template +struct Node +{ + T val; + Node* rightNode; + Node* leftNode; +}; + +template +class BinaryTree +{ +private: + Node* root = nullptr; + + static int getHeight(Node* root) + { + if (!root) + return 0; + + return 1 + std::max(getHeight(root->leftNode), getHeight(root->rightNode)); + } + + static const std::string print(Node* node) + { + if (!node) + return "Empty"; + return "" + std::to_string(node->val) + " (" + print(node->leftNode) + ") (" + print(node->rightNode) + ")"; + } + +public: + BinaryTree() {} + + void insert(T val) + { + Node* newNode { new Node[sizeof(Node)] }; + newNode->val = val; + newNode->rightNode = nullptr; + newNode->leftNode = nullptr; + + if (!root) + { + root = newNode; + return; + } + + Node* currNode = root; + while (currNode) + { + T oldVal = currNode->val; + if (val > oldVal) + { + if (currNode->rightNode) + { + currNode = currNode->rightNode; + continue; + } + currNode->rightNode = newNode; + break; + } + else if (val < oldVal) + { + if (currNode->leftNode) + { + currNode = currNode->leftNode; + continue; + } + currNode->leftNode = newNode; + break; + } + else + throw std::runtime_error("Duplicate value."); + } + } + + int height() + { + return getHeight(root); + } + + friend std::ostream& operator<<(std::ostream& os, const BinaryTree& bt) + { + os << print(bt.root) << "\n"; + return os; + } + + bool search(T val) + { + Node* currNode = root; + while (currNode) + { + if (currNode->val == val) + return true; + else if (currNode->val < val) + currNode = currNode->rightNode; + else + currNode = currNode->leftNode; + } + return false; + } +}; + +template +void insert_element(BinaryTree& root) +{ + std::cout << "Enter value to insert: "; + T value {}; + std::cin >> value; + + root.insert(value); + std::cout << "Successfully inserted " << value << "!\n\n"; +} + +template +void search_element(BinaryTree& root) +{ + std::cout << "Enter value to insert: "; + T value {}; + std::cin >> value; + + if (root.search(value)) + std::cout << "Value exists in tree.\n\n"; + else + std::cout << "Value does not exist in tree.\n\n"; +} + +int main() +{ + BinaryTree root{}; + + do + { + try + { + std::cout << "What would you like to do?\n"; + std::cout << "1. Insert an element\n"; + std::cout << "2. Get height of tree\n"; + std::cout << "3. Print the tree\n"; + std::cout << "4. Search for an element in the tree\n"; + std::cout << "5. Quit\n"; + std::cout << "Enter your choice.\n"; + + int ch {}; + std::cin >> ch; + + switch (ch) + { + case 1: insert_element(root); + break; + case 2: std::cout << "Height is " << root.height() << ".\n\n"; + break; + case 3: std::cout << "Tree is:\n" << root << "\n"; + break; + case 4: search_element(root); + break; + case 5: return 0; + default: std::cout << "Invalid option. Try again.\n\n"; + } + } + catch (std::runtime_error e) + { + std::cerr << e.what() << "\n\n"; + } + } + while(true); + + + return 0; +}