Syntax error Python Program for Depth First Binary Tree Search using Recursion

Python Program for Depth First Binary Tree Search using Recursion



When it is required to perform depth first search on a tree using recursion, a class is defined, and methods are defined on it that help perform breadth first search.

Below is a demonstration for 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 insert_at_left(self, new_node):
      self.left = new_node

   def insert_at_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(key)
         if temp is not None:
            return temp
      if self.right is not None:
         temp = self.right.search(key)
         return temp
      return None

   def depth_first_search(self):
      print('entering {}...'.format(self.key))
      if self.left is not None:
         self.left.depth_first_search()
      print('at {}...'.format(self.key))
      if self.right is not None:
         self.right.depth_first_search()
      print('leaving {}...'.format(self.key))

btree_instance = None

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

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

   op = my_input[0].strip().lower()
   if op == 'insert':
      data = int(my_input[1])
      new_node = BinaryTree_struct(data)
      sub_op = my_input[2].strip().lower()
      if sub_op == 'at':
         btree_instance = new_node
      else:
         position = my_input[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 sub_op == 'left':
            ref_node.insert_at_left(new_node)
         elif sub_op == 'right':
            ref_node.insert_at_right(new_node)
   elif op == 'dfs':
      print('depth-first search traversal:')
      if btree_instance is not None:
         btree_instance.depth_first_search()
      print()

   elif op == 'quit':
      break

Output

Menu (no duplicate keys)
insert <data> at root
insert <data> left of <data>
insert <data> right of <data>
dfs
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? dfs
depth-first search traversal:
entering 5...
entering 6...
at 6...
leaving 6...
at 5...
entering 8...
at 8...
leaving 8...
leaving 5...
What would you like to do? quit
Use quit() or Ctrl-D (i.e. EOF) to exit

Explanation

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

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

  • Another method named ‘set_root’ is defined to specify the root of the tree.

  • Another method named ‘insert_at_left’ is defined that helps add nodes to the left of the tree.

  • Another method named ‘insert_at_right’ is defined that helps add nodes to the right of the tree.

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

  • A method named ‘depth_first_search’ is defined, that helps perform depth first search on the binary tree.

  • An instance of the class is created and assigned to ‘None’.

  • A menu is given.

  • 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.

Updated on: 2021-04-16T12:21:22+05:30

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements