我试图为二分查找树编写一个min方法,但由于某种原因,输出始终为None! 我知道我总是要离开才能找到最低限度,这就是我的代码所做的事情,但我不明白为什么我总是将无作为输出!
class Tree_node():
def __init__(self, key, val):
self.key = key
self.val = val
self.left = None
self.right = None
class BSearch_tree():
def __init__(self):
self.root=None
def insert(self, key, val):
if self.root==None:
self.root=Tree_node(key,val)
elif key == self.root.key:
self.val=val
elif key < self.root.key:
left = BSearch_tree()
left.root = self.root.left
self.root.left=BSearch_tree.insert(left,key,val)
elif key > self.root.key:
right = BSearch_tree()
right.root=self.root.right
self.root.right=BSearch_tree.insert(right,key,val)
def min(self):
if self.root==None:
return None
current= self
while current.root.left!=None:
current=current.root.left
return current