我在Python中实现了函数来创建二进制搜索树。它当前显示节点 inorder ,现在我试图让它显示在预订中。
我根据笔记创建了 inorder 功能,但我无法弄清楚如何让预订正常工作。现在它显示最左边的节点,但我不能让它回溯到正确的节点。我将使用示例值发布我的代码。任何让我走上正确轨道的提示都将不胜感激。谢谢!
我把所有代码和预订功能代码都放了。
####
#
# Binary Search Tree
#
########
class Node:
#constructor
def __init__(self,data):
self.left = None
self.right = None
self.data = data
def insert(self,data):
if(data < self.data):
if(self.left is None):
self.left = Node(data)
else:
self.left.insert(data)
elif(data > self.data):
if(self.right is None):
self.right = Node(data)
else:
self.right.insert(data)
#lookup node containing data
#data to lookup and parent nodes parent
#returns node and its parent if found, otherwise none
def lookup(self,data,parent=None):
if(data < self.data):
if(self.left is None):
return None, None
return self.left.lookup(data,self)
elif(data > self.data):
if(self.right is None):
return None, None
return self.right.lookup(data,self)
else:
return self,parent
#return the number of children (either 0,1,or 2)
def childrenCount(self):
children = 0
if(self.left):
children += 1
if(self.right):
children += 1
return children
#delete the node containing data
#data node content to be deleted
def delete(self,data):
node, parent = self.lookup(data)
if(node is not None):
childrenCount = node.childrenCount()
if(childrenCount == 0):
if(parent):
if(parent.left is node):
parent.left = None
else:
parent.right = None
del node
elif(childrenCount == 1):
if(node.left):
n = node.left
else:
n = node.right
if(parent):
if(parent.left is node):
parent.left = n
else:
parent.right = n
del node
else:
parent = node
nextNode = node.right
while(nextNode.left):
parent = nextNode
nextNode = nextNode.left
node.data = nextNode.data
if(parent.left == nextNode):
parent.left = nextNode.right
else:
parent.right = nextNode.right
#display the tree via inorder
def displayInorder(self):
global dspList2
if(self.left):
self.left.displayInorder()
dspList2.append(self.data)
if(self.right):
self.right.displayInorder()
def preOrder(self):
global dspList
# dspList.append(self.data)
if(self.left):
print("Left.. appending",self.data)
dspList.append(self.data)
self.left.preOrder()
if (self.right is None):
print("No right.. append",self.data)
dspList.append(self.data)
dspList = []
dspList2 = []
def main():
global dspList2
global dspList
root = Node(8)
root.insert(3)
root.insert(10)
root.insert(1)
root.insert(6)
root.insert(4)
root.insert(7)
root.insert(14)
root.insert(13)
node,parent = root.lookup(6)
root.preOrder()
root.displayInorder()
print("Inorder:",dspList2)
print("Preorder:",dspList)
main()
def preOrder(self):
global dspList
# dspList.append(self.data)
if(self.left):
print("Left.. appending",self.data)
dspList.append(self.data)
self.left.preOrder()
if (self.right is None):
print("No right.. append",self.data)
dspList.append(self.data)
答案 0 :(得分:0)
查看遍历on Wikipedia的解释。您的解决方案非常简单,并且可以将解释清晰地转换为代码。预订仅按顺序与通过树递归的顺序不同。除此之外,它同样简单明了。对代码进行必要的更改以使预订工作同样应该是从按顺序到预订的简单更改。下面的代码应该有效:
def inOrder(self):
global dspList2
if(self.left):
self.left.inOrder()
dspList2.append(self.data)
if(self.right):
self.right.inOrder()
def preOrder(self):
global dspList
dspList.append(self.data)
if(self.left):
self.left.preOrder()
if (self.right):
self.right.preOrder()
注意,不是使用全局变量,而是委托给带累加器的辅助方法会更安全,更好:[/ p>
def inOrder(self):
acc = []
self.inOrderAcc(self, acc)
return acc
def inOrderAcc(self, acc):
if (self.left):
self.left.inOrderAcc(acc)
acc.append(self.data)
if (self.right):
self.right.inOrderAcc(acc)