BST遍历的递归函数错误

时间:2015-12-01 19:16:04

标签: python python-3.x

这是我的代码:

class Node:
  def __init__(self,data):
    self.data=data
    self.left=None
    self.right=None
    self.parent=None

class binarytree:
  def __init__(self):
    self.root=None
    self.size=0

   def insert(self,data):
     if self.root==None:
        self.root=Node(data)
     else:
        current=self.root
        while 1:
            if data < current.data:
                if current.left:
                    current=current.left
                else:
                    new=Node(data)
                    current.left=new
                    break;
            elif data > current.data:
                if current.right:
                    current=current.right
                else:
                    new=Node(data)
                    current.right=new
                    break;
            else:
                break

   def print_tree(self):
     current=self.root
     if current.left!=None:
        print_tree(current.left)
     print(current.data)
     if current.right!=None:
        print_tree(current.right)

b=binarytree()  

当我运行以下代码并插入元素后,当我调用函数print_tree时,我得到错误:

  File "C:\Windows\System32\btree.py", line 40, in print_tree
    print_tree(current.left)
NameError: name 'print_tree' is not defined

有人可以为此提供帮助。提前致谢

0 个答案:

没有答案