【问题标题】:Binary Seacrh tree Traversing python二叉搜索树遍历python
【发布时间】:2021-12-02 04:26:02
【问题描述】:
def traverse(self):
    if self.root !=None:
        print('*****Traversing*****')
        print('self.root is', self.root.data)
        print('self.root.Left is', self.root.leftchild.data)
        print('self.root.Right  is', self.root.rightchild.data)
        self.traverse_in_order(self.root)
        
def traverse_in_order(self,node):
    
    if node.leftchild!=None:  #there is leftchild
        print('node.leftchild is',node.leftchild.data )
        self.traverse_in_order(node.leftchild)       #go till end of leftnode
    print('')
    print('print node ', node.data)
    print('')
    if node.rightchild!=None:
        print('Node.RIght',node.rightchild.data)
        self.traverse_in_order(node.rightchild)
bst=BST()
bst.insert(32)
bst.insert(10)
bst.insert(1)
bst.insert(19)
bst.insert(46)

bst.traverse()

Output----

*****Traversing*****
   self.root is 32
   self.root.Left is 10
   self.root.Right  is 46
   node.leftchild is 10
   node.leftchild is 1
   -------------print node  1
   -------------print node  10
   Node.RIght 19
   print node  19
   print node  32
   Node.RIght 46
   print node  46

任何人都可以帮助使用 traverse_in_order 功能。我的疑问是从根开始,我们一直到最后一个左节点,然后打印它的数据,即(1 被打印理解。)如何再次打印 10。与权限子树相同。我希望你明白我在问什么。 谢谢

【问题讨论】:

    标签: python binary-search-tree traversal


    【解决方案1】:

    “10”不再打印。唯一重要的打印是那些说“打印节点”的打印,它们都是唯一且有序的。其他只是调试打印,以帮助您遵循逻辑。他们不算数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2011-09-08
      • 1970-01-01
      • 2018-02-25
      相关资源
      最近更新 更多