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

PythonServer Side ProgrammingProgramming

When it is required to find the ‘n’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.

Below is a demonstration of the same −

## Example

Live Demo

class BinaryTree_struct:
def __init__(self, key=None):
self.key = key
self.left = None
self.right = None

def set_root(self, key):
self.key = key

def inorder_nth(self, n):
return self.inorder_nth_helper_fun(n, [])

def inorder_nth_helper_fun(self, n, in_ord):
if self.left is not None:
temp = self.left.inorder_nth_helper_fun(n, in_ord)
if temp is not None:
return temp
in_ord.append(self)
if n == len(in_ord):
return self
if self.right is not None:
temp = self.right.inorder_nth_helper_fun(n, in_ord)
if temp is not None:
return temp

def insert_t0_left(self, new_node):
self.left = new_node

def insert_to_right(self, new_node):
self.right = new_node

def search_elem(self, key):
if self.key == key:
return self
if self.left is not None:
temp = self.left.search_elem(key)
if temp is not None:
return temp
if self.right is not None:
temp = self.right.search_elem(key)
return temp
return None

btree_instance = None

print('Menu (this assumes no duplicate keys)')
print('insert <data> at root')
print('insert <data> left of <data>')
print('insert <data> right of <data>')
print('inorder ')
print('quit')

while True:
do = input('What would you like to do? ').split()

operation = do[0].strip().lower()
if operation == 'insert':
data = int(do[1])
new_node = BinaryTree_struct(data)
suboperation = do[2].strip().lower()
if suboperation == 'at':
btree_instance = new_node
else:
position = do[4].strip().lower()
key = int(position)
ref_node = None
if btree_instance is not None:
ref_node = btree_instance.search_elem(key)
if ref_node is None:
print('No such key.')
continue
if suboperation == 'left':
ref_node.insert_t0_left(new_node)
elif suboperation == 'right':
ref_node.insert_to_right(new_node)

elif operation == 'inorder':
if btree_instance is not None:
index = int(do[1].strip().lower())
node = btree_instance.inorder_nth(index)
if node is not None:
print('nth term of inorder traversal: {}'.format(node.key))
else:
print('The index exceeds maximum possible index.')
else:
print('The tree is empty...')

elif operation == 'quit':
break

## Output

Menu (this assumes no duplicate keys)
insert <data> at root
insert <data> left of <data>
insert <data> right of <data>
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

## Explanation

• The ‘BinaryTree_struct’ class with required attributes is created.

• It has an ‘init’ function that is used to set the left and right nodes to ‘None’.

• It has a ‘set_root’ method that helps set the root of the binary tree.

• Another method named ‘inorder_nth’ that performs inorder traversal using recursion.

• Hence it has a helper function defined alongside.

• Another method named ‘insert_to_right’ is defined that helps add element to the right side of the root node.

• A method named ‘insert_to_left’ is defined, that helps add element to the left side of the root node.

• A method named ‘search_elem’ is defined, that helps search for a specific element.

• An object of the ‘BinaryTree_struct’ class is created.

• The user input is taken for the operation that needs to be performed.

• Depending on the user’ choice, the operation is performed.

• Relevant output is displayed on the console.

Published on 15-Apr-2021 13:32:24