-1
我想在Python中实现BST。我在插入函数中出现错误:“节点对象没有插入属性。”是否有我不能在我定义的Node对象上调用插入函数的原因?BST在Python中的实现错误
class Node:
def __init__(self, data, left=None, right=None):
self.data=data
self.left=left
self.right=right
def setdata(self,data):
self.data=data
def hasLeft(self):
return self.left
def hasRight(self):
return self.right
class BST:
def __init__(self):
self.root=None
def insert(self, data):
if self.root:
self._insert(data, self.root)
else:
self.root=Node(data)
def _insert(self, data, curr):
if data<curr.data:
if curr.hasLeft:
self._insert(data, curr.left)
else:
curr.left=Node(data)
if data>self.data:
if self.hasRight:
self._insert(data, curr.right)
else:
curr.right=Node(data)
root = Node(2)
root.insert(1)
root.insert(3)
print root
print root.left
误差属于哪一行? – Kasramvd 2014-11-05 17:50:21