我最近开始在Python中学习和实现一些数据结构,并尝试二进制搜索树并完成了代码。
除了删除根节点外,所有操作都正常运行。
我将代码分为3个模块
这是我的代码:
节点。py公司
class Node:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insert(self, data):
if data < self.data:
if not self.leftChild:
self.leftChild = Node(data)
else:
self.leftChild.insert(data)
else:
if not self.rightChild:
self.rightChild = Node(data)
else:
self.rightChild.insert(data)
def remove(self, data, parentNode):
if data < self.data:
if self.leftChild is not None:
self.leftChild.remove(data, self)
elif data > self.data:
if self.rightChild is not None:
self.rightChild.remove(data, self)
else:
if self.leftChild is not None and self.rightChild is not None:
self.data = self.rightChild.getMin()
self.rightChild.remove(self.data, self)
elif parentNode.leftChild == self:
if self.leftChild is not None:
tempNode = self.leftChild
else:
tempNode = self.rightChild
parentNode.leftChild = tempNode
elif parentNode.rightChild == self:
if self.leftChild is not None:
tempNode = self.leftChild
else:
tempNode = self.rightChild
parentNode.rightChild = tempNode
def getMin(self):
if self.leftChild is None:
return self.data
else:
self.leftChild.getMin()
def getMax(self):
if self.rightChild is None:
return self.data
else:
self.rightChild.getMax()
def traverseInOrder(self):
if self.leftChild is not None:
self.leftChild.traverseInOrder()
print(self.data)
if self.rightChild is not None:
self.rightChild.traverseInOrder()
二进制搜索树。py公司
from BinarySearchTrees.Node import Node
class BST:
def __init__(self):
self.rootNode = None;
def insert(self, data):
if self.rootNode is None:
self.rootNode = Node(data)
else:
self.rootNode.insert(data)
def removal(self, dataToRemove):
if self.rootNode:
if self.rootNode.data == dataToRemove:
tempNode = Node(None)
tempNode.leftChild = self.rootNode
print("The value of dataToRemove : " + str(dataToRemove))
self.rootNode.remove(dataToRemove, tempNode)
else:
self.rootNode.remove(dataToRemove, None)
def getMin(self):
if self.rootNode:
return self.rootNode.getMin()
def getMax(self):
if self.rootNode:
return self.rootNode.getMax()
def traverseInOrder(self):
if self.rootNode:
self.rootNode.traverseInOrder()
输出py公司
from BinarySearchTrees.BinarySearchTree import BST
bst = BST()
bst.insert(5)
bst.insert(8)
bst.insert(3)
bst.insert(7)
bst.insert(9)
bst.insert(4)
bst.insert(2)
bst.traverseInOrder()
bst.removal(5)
bst.traverseInOrder()
删除命令的错误是:
Traceback (most recent call last):
File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Output.py", line 16, in <module>
bst.removal(5)
File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\BinarySearchTree.py", line 24, in removal
self.rootNode.remove(dataToRemove, tempNode)
File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 34, in remove
self.rightChild.remove(self.data, self)
File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 23, in remove
if data < self.data:
TypeError: '<' not supported between instances of 'NoneType' and 'int'
这就像在节点的remove函数中传递的数据参数返回了None值,尽管在BinarySearchTree remove函数中给出了一个值。
如果有人能在我的代码中找到错误,请告诉我解决方法。这将是一个很大的帮助。。