gpt4 book ai didi

python - 实现二进制搜索树以处理 Python 中的重复键

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:43 26 4
gpt4 key购买 nike

下面是我发现可以帮助我更好地学习 python 的网站上给出的示例代码:Interactive Python

作者解释说:

One important problem with our implementation of insert is that duplicate keys are not handled properly. As our tree is implemented a duplicate key will create a new node with the same key value in the right subtree of the node having the original key. The result of this is that the node with the new key will never be found during a search. A better way to handle the insertion of a duplicate key is for the value associated with the new key to replace the old value. We leave fixing this bug as an exercise for you."

我的问题是,如何解决此问题以正确处理重复键?如果树中已经存在键,则新的有效负载应替换旧值。目标是不添加具有相同 key 的另一个节点,但我什至不知道从哪里开始这样做。我不确定为什么这会如此令人困惑。

class BinarySearchTree:

def __init__(self):
self.root = None
self.size = 0

def length(self):
return self.size

def __len__(self):
return self.size

def __iter__(self):
return self.root.__iter__()

class TreeNode:
def __init__(self,key,val,left=None,right=None,
parent=None):
self.key = key
self.payload = val
self.leftChild = left
self.rightChild = right
self.parent = parent

def hasLeftChild(self):
return self.leftChild

def hasRightChild(self):
return self.rightChild

def isLeftChild(self):
return self.parent and \
self.parent.leftChild == self

def isRightChild(self):
return self.parent and \
self.parent.rightChild == self

def isRoot(self):
return not self.parent

def isLeaf(self):
return not (self.rightChild or self.leftChild)

def hasAnyChildren(self):
return self.rightChild or self.leftChild

def hasBothChildren(self):
return self.rightChild and self.leftChild

def replaceNodeData(self,key,value,lc,rc):
self.key = key
self.payload = value
self.leftChild = lc
self.rightChild = rc
if self.hasLeftChild():
self.leftChild.parent = self
if self.hasRightChild():
self.rightChild.parent = self

def put(self,key,val):
if self.root:
self._put(key,val,self.root)
else:
self.root = TreeNode(key,val)
self.size = self.size + 1

def _put(self,key,val,currentNode):
if key < currentNode.key:
if currentNode.hasLeftChild():
self._put(key,val,currentNode.leftChild)
else:
currentNode.leftChild = TreeNode(key,val,
parent=currentNode)
else:
if currentNode.hasRightChild():
self._put(key,val,currentNode.rightChild)
else:
currentNode.rightChild = TreeNode(key,val,
parent=currentNode)

最佳答案

_put() 方法进行测试和插入,它从不测试是否相等。它只测试插入的 key 是否较小(将 key 插入左 child ),否则它会插入右 child 。

简单地测试是否相等并替换:

def _put(self,key,val,currentNode):
if key == currentNode.key:
currentNode.value = val

elif key < currentNode.key:
if currentNode.hasLeftChild():
self._put(key,val,currentNode.leftChild)
else:
currentNode.leftChild = TreeNode(key,val,
parent=currentNode)
else:
if currentNode.hasRightChild():
self._put(key,val,currentNode.rightChild)
else:
currentNode.rightChild = TreeNode(key,val,
parent=currentNode)

关于python - 实现二进制搜索树以处理 Python 中的重复键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com