无法在Python中将元素插入二进制搜索树

时间:2017-07-26 03:22:25

标签: python python-2.7

class Node:
    def __init__(self,key):
      self.left=None
      self.right=None
      self.val=key
def insert(root,key):
     if root is None:
        root=Node(key)
     else:
        if root.val < key:
            insert(root.right, key)
     else:
            insert(root.left, key)

     def inorder(root):
            if root:
                inorder(root.right)
                print root.val
                inorder(root.left)
     root=None
     d=input()
     while(d!=-1):
        insert(root,d)
        d=input()
     inorder(root)

我无法按顺序打印上面的树元素.....代码出了什么问题....任何人都可以解释一下吗?

0 个答案:

没有答案