From 1ec38c432eb43c7d52e35df911bfab0bf33525dd Mon Sep 17 00:00:00 2001 From: Paramita Tejasvi <69339443+ptejasv@users.noreply.github.com> Date: Fri, 21 May 2021 20:15:21 +0800 Subject: [PATCH] chore(CPlusPlus): add in order binary tree Morris traversal (#317) User can enter the number of nodes and the value for each note as desired with -1 for a NULL node. The program will build the tree and print out the traversal output. --- algorithms/CPlusPlus/README.md | 1 + .../Trees/in-order-morris-traversal.cpp | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 algorithms/CPlusPlus/Trees/in-order-morris-traversal.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 47c0e3ba..446a0729 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -63,6 +63,7 @@ 3. [Level Order Traversal](Trees/level-order-traversal.cpp) 4. [Depth first Traversal](Trees/pre-in-post-traversal.cpp) 5. [Binary Search Tree](Trees/binary-search-tree.cpp) +6. [In order morris traversal](Trees/in-order-morris-traversal.cpp) # Maths 1. [Kaprekar Number](Maths/Kaprekar-number.cpp) diff --git a/algorithms/CPlusPlus/Trees/in-order-morris-traversal.cpp b/algorithms/CPlusPlus/Trees/in-order-morris-traversal.cpp new file mode 100644 index 00000000..6934c65f --- /dev/null +++ b/algorithms/CPlusPlus/Trees/in-order-morris-traversal.cpp @@ -0,0 +1,98 @@ +/** + * Conduct a morris in-order traversal of a binary tree, + * printing out the value at each node according to + * the traversal using user defined inputs. + * Node.val can take any number except -1 + **/ +# include +# include +using namespace std; + +// Basic tree definition +struct Node { + int val; + Node* left; + Node* right; + Node(int x) : val(x), left(nullptr), right(nullptr) {}; + +}; + +/** + * Print out the values of the treenodes using an in-order traversal + * (left subtree - root - right subtree) without using additional memory + * node values are printed with a space between the values of each 2 nodes + **/ + +void print_morris(Node* root) { + if (!root) cout << "\n"; + + Node* previous; + + while(root) { + if (!root->left) { + cout << root->val << " "; + root = root->right; + } else { + previous = root->left; + + while (previous->right && previous->right != root) + previous = previous->right; + + if (!previous->right) { + previous->right = root; + root = root->left; + } else { + previous->right = NULL; + cout << root->val << " "; + root = root->right; + } + } + } +} + +Node* build_tree(vector& nodes, Node* root, int i, int n) { + if (i < n) { + if (nodes[i] == -1) return NULL; + Node* temp = new Node(nodes[i]); + root = temp; + + root->left = build_tree(nodes, root->left, 2 * i + 1, n); + root->right = build_tree(nodes, root->right, 2 * i + 2, n); + } + return root; +} + +int main() { + vector nodes; + int numNodes; + + cout << "Enter the number of nodes: "; + cin >> numNodes; + if (!numNodes) { cout << "\n"; return 0; } + + for (int i = 0; i < numNodes; i++) { + cout << "Enter the value for node followed by ENTER (-1 for no node) " << i + 1 << ": "; + int nodeVal; + cin >> nodeVal; + nodes.push_back(nodeVal); + } + + Node* my_root = build_tree(nodes, my_root, 0, nodes.size()); + + /** + * Test for the morris traversal: + * tree referenced by my_root: + * 8 + * / \ + * 5 10 + * / \ + * 9 6 + * + * for input:7, 8 5 10 -1 -1 9 6 + * expected output: 5 8 9 10 6 + * time complexity: O(n), space complexity O(1) + **/ + + print_morris(my_root); + return 0; +}