在树的中序遍历中查找第 N 个节点的 Python 程序

当需要使用二叉树的中序遍历找到第n个节点时,创建一个二叉树类,其中包含设置根元素、向左或向右添加元素、执行顺序遍历等方法。类的一个实例被创建,它可以用来访问方法。

以下是相同的演示 -

示例

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):
      ifself.leftis 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
      ifself.rightis 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):
      ifself.key== key:
         return self
      ifself.leftis not None:
         temp = self.left.search_elem(key)
         if temp is not None:
            return temp
      ifself.rightis 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
输出结果
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

解释

  • 创建了具有所需属性的“BinaryTree_struct”类。

  • 它有一个“init”函数,用于将左右节点设置为“无”。

  • 它有一个“set_root”方法,可以帮助设置二叉树的根。

  • 另一种名为“inorder_nth”的方法使用递归执行中序遍历。

  • 因此,它旁边定义了一个辅助函数。

  • 定义了另一个名为“insert_to_right”的方法,它有助于将元素添加到根节点的右侧。

  • 定义了一个名为“insert_to_left”的方法,它有助于将元素添加到根节点的左侧。

  • 定义了一个名为“search_elem”的方法,它有助于搜索特定元素。

  • 创建了“BinaryTree_struct”类的对象。

  • 用户输入用于需要执行的操作。

  • 根据用户的选择,执行操作。

  • 相关输出显示在控制台上。

猜你喜欢