Python Program to Find Nth Node in the Inorder Traversal of a Tree

PythonServer Side ProgrammingProgramming

<p>When it is required to find the &lsquo;n&rsquo;th node using inorder traversal of a binary tree, a binary tree class is created with methods to set the root element, add elements to left or right, perform in order traversal and so on. An instance of the class is created, and it can be used to access the methods.</p><p>Below is a demonstration of the same &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/LcekGpkw" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">class BinaryTree_struct: &nbsp; &nbsp;def __init__(self, key=None): &nbsp; &nbsp; &nbsp; self.key = key &nbsp; &nbsp; &nbsp; self.left = None &nbsp; &nbsp; &nbsp; self.right = None &nbsp; &nbsp;def set_root(self, key): &nbsp; &nbsp; &nbsp; self.key = key &nbsp; &nbsp;def inorder_nth(self, n): &nbsp; &nbsp; &nbsp; return self.inorder_nth_helper_fun(n, []) &nbsp; &nbsp;def inorder_nth_helper_fun(self, n, in_ord): &nbsp; &nbsp; &nbsp; if self.left is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = self.left.inorder_nth_helper_fun(n, in_ord) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if temp is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return temp &nbsp; &nbsp; &nbsp; in_ord.append(self) &nbsp; &nbsp; &nbsp; if n == len(in_ord): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self &nbsp; &nbsp; &nbsp; if self.right is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = self.right.inorder_nth_helper_fun(n, in_ord) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if temp is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return temp &nbsp; &nbsp;def insert_t0_left(self, new_node): &nbsp; &nbsp; &nbsp; self.left = new_node &nbsp; &nbsp;def insert_to_right(self, new_node): &nbsp; &nbsp; &nbsp; self.right = new_node &nbsp; &nbsp;def search_elem(self, key): &nbsp; &nbsp; &nbsp; if self.key == key: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self &nbsp; &nbsp; &nbsp; if self.left is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = self.left.search_elem(key) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if temp is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return temp &nbsp; &nbsp; &nbsp; if self.right is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = self.right.search_elem(key) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return temp &nbsp; &nbsp; &nbsp; return None btree_instance = None print(&#39;Menu (this assumes no duplicate keys)&#39;) print(&#39;insert &lt;data&gt; at root&#39;) print(&#39;insert &lt;data&gt; left of &lt;data&gt;&#39;) print(&#39;insert &lt;data&gt; right of &lt;data&gt;&#39;) print(&#39;inorder &#39;) print(&#39;quit&#39;) while True: &nbsp; &nbsp;do = input(&#39;What would you like to do? &#39;).split() &nbsp; &nbsp;operation = do[0].strip().lower() &nbsp; &nbsp;if operation == &#39;insert&#39;: &nbsp; &nbsp; &nbsp; data = int(do[1]) &nbsp; &nbsp; &nbsp; new_node = BinaryTree_struct(data) &nbsp; &nbsp; &nbsp; suboperation = do[2].strip().lower() &nbsp; &nbsp; &nbsp; if suboperation == &#39;at&#39;: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btree_instance = new_node &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;position = do[4].strip().lower() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key = int(position) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ref_node = None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if btree_instance is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ref_node = btree_instance.search_elem(key) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ref_node is None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;No such key.&#39;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if suboperation == &#39;left&#39;: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ref_node.insert_t0_left(new_node) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif suboperation == &#39;right&#39;: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ref_node.insert_to_right(new_node) &nbsp; &nbsp;elif operation == &#39;inorder&#39;: &nbsp; &nbsp; &nbsp; if btree_instance is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index = int(do[1].strip().lower()) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node = btree_instance.inorder_nth(index) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if node is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;nth term of inorder traversal: {}&#39;.format(node.key)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;The index exceeds maximum possible index.&#39;) &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(&#39;The tree is empty...&#39;) &nbsp; &nbsp;elif operation == &#39;quit&#39;: &nbsp; &nbsp; &nbsp; break</pre><h2>Output</h2><pre class="result notranslate">Menu (this assumes no duplicate keys) insert &lt;data&gt; at root insert &lt;data&gt; left of &lt;data&gt; insert &lt;data&gt; right of &lt;data&gt; inorder quit What would you like to do? insert 5 at root What would you like to do? insert 6 left of 5 What would you like to do? insert 8 right of 5 What would you like to do? inorder 5 The index exceeds maximum possible index. What would you like to do? 6 6</pre><h2>Explanation</h2><ul class="list"><li><p>The &lsquo;BinaryTree_struct&rsquo; class with required attributes is created.</p></li><li><p>It has an &lsquo;init&rsquo; function that is used to set the left and right nodes to &lsquo;None&rsquo;.</p></li><li><p>It has a &lsquo;set_root&rsquo; method that helps set the root of the binary tree.</p></li><li><p>Another method named &lsquo;inorder_nth&rsquo; that performs inorder traversal using recursion.</p></li><li><p>Hence it has a helper function defined alongside.</p></li><li><p>Another method named &lsquo;insert_to_right&rsquo; is defined that helps add element to the right side of the root node.</p></li><li><p>A method named &lsquo;insert_to_left&rsquo; is defined, that helps add element to the left side of the root node.</p></li><li><p>A method named &lsquo;search_elem&rsquo; is defined, that helps search for a specific element.</p></li><li><p>An object of the &lsquo;BinaryTree_struct&rsquo; class is created.</p></li><li><p>The user input is taken for the operation that needs to be performed.</p></li><li><p>Depending on the user&rsquo; choice, the operation is performed.</p></li><li><p>Relevant output is displayed on the console.</p></li></ul>
raja
Updated on 15-Apr-2021 13:35:21

Advertisements