[英]Binary search tree not inserting/printing actual largest value unless it is implemented as root
我現在正在學習二叉搜索樹,自己做了一個。 我的樹的問題是traverse()
方法不打印最大值(下面粘貼的代碼中的'15'),除非它是要插入的第一個值。
我也嘗試了getMaxValue()
函數,它也沒有返回預期值,而是給出第二大值。 這導致我認為問題必須在某個地方的insert()
函數中,但它已經是一個小時,我找不到修復或我做錯了什么。
class Node(object):
def __init__(self,data):
self.data = data
self.leftChild = None
self.rightChild = None
class BinarySearchTree(object):
def __init__(self):
self.root = None
#Parent function of insert
def insert(self,data):
if not self.root:
self.root = Node(data)
else:
self.insertNode(data,self.root)
# O(log n) if the tree is balanced!!!!
# thats why AVL and RBT are needed!
#Child function of insert
def insertNode(self,data,node):
if data < node.data:
# check if there is already a left child, if it is not null then we call insertNode recursively and insert a node as its child
if node.leftChild:
self.insertNode(data,node.leftChild)
# if there is no left child then we instantiate it, and make a left child, same stuff happening for right child
else:
node.leftChild = Node(data)
else:
if node.rightChild:
self.insertNode(data, node.rightChild)
else:
self.rightChild = Node(data)
#Parent function of getminvalue
def getMinValue(self):
if self.root:
return self.getMin(self.root)
#Child function of getminvalue
def getMin(self,node):
#if there is a left child
if node.leftChild:
#go to left child recursively
return self.getMin(node.leftChild)
return node.data
#Parent function of getMaxValue
def getMaxValue(self):
if self.root:
return self.getMax(self.root)
#Child function of getmaxValue
def getMax(self, node):
if node.rightChild:
return self.getMax(node.rightChild)
return node.data
#Parent function of traverse
def traverse(self):
if self.root:
self.traverseInOrder(self.root)
#Child function of traverseinOrder
def traverseInOrder(self,node):
if node.leftChild:
self.traverseInOrder(node.leftChild)
print("%s " % node.data)
if node.rightChild:
self.traverseInOrder(node.rightChild)
#Inputs
bst = BinarySearchTree()
bst.insert(10)
bst.insert(15)
bst.insert(5)
bst.insert(4)
bst.insert(3)
print(bst.traverse())
預期的結果是
3
4
5
10
15
但我得到了
3
4
5
10
None
固定:
將很快編輯詳細信息
class Node(object):
def __init__(self,data):
self.data = data
self.leftChild = None
self.rightChild = None
class BinarySearchTree(object):
def __init__(self):
self.root = None
#Parent function of insert
def insert(self,data):
if not self.root:
self.root = Node(data)
else:
self.insertNode(data,self.root)
# O(log n) if the tree is balanced!!!!
# thats why AVL and RBT are needed!
#Child function of insert
def insertNode(self,data,node):
if data < node.data:
# check if there is already a left child, if it is not null then we call insertNode recursively and insert a node as its child
if node.leftChild:
self.insertNode(data,node.leftChild)
# if there is no left child then we instantiate it, and make a left child, same stuff happening for right child
else:
node.leftChild = Node(data)
else:
if node.rightChild:
self.insertNode(data, node.rightChild)
else:
node.rightChild = Node(data)
#Parent function of getminvalue
def getMinValue(self):
if self.root:
return self.getMin(self.root)
#Child function of getminvalue
def getMin(self,node):
#if there is a left child
if node.leftChild:
#go to left child recursively
return self.getMin(node.leftChild)
return node.data
#Parent function of getMaxValue
def getMaxValue(self):
if self.root:
return self.getMax(self.root)
#Child function of getmaxValue
def getMax(self, node):
if node.rightChild:
return self.getMax(node.rightChild)
return node.data
#Parent function of traverse
def traverse(self):
if self.root:
self.traverseInOrder(self.root)
#Child function of traverseinOrder
def traverseInOrder(self,node):
if node.leftChild:
self.traverseInOrder(node.leftChild)
print("%s " % node.data)
if node.rightChild:
self.traverseInOrder(node.rightChild)
#Inputs
bst = BinarySearchTree()
bst.insert(10)
bst.insert(15)
bst.insert(5)
bst.insert(4)
bst.insert(3)
bst.traverse()
我使用調試器(我建議你這樣做,它非常有用)逐步完成代碼,並看到了這個:
正如您所看到的,具有數據10
node
沒有正確的子node
,而self
具有帶數據10
的root
和帶有數據15
rightChild
。
這讓我看到插入一個新的右子的行,你錯誤地寫了: self.rightChild = Node(data)
而不是node.rightChild = Node(data)
(就像你正確對待左子)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.