Python中的红黑树实现只是将所有的叶子都涂成红色,这是为什么?
我已经有了用python实现红黑树的代码。然而,当我运行我的代码时,它所做的似乎只是像插入常规BST一样插入值,然后将所有内部顶点涂成红色。这是一个家庭作业的练习作业,但我被卡住了,没有其他地方可以求助 以下是我的实现:Python中的红黑树实现只是将所有的叶子都涂成红色,这是为什么?,python,data-structures,binary-search-tree,red-black-tree,Python,Data Structures,Binary Search Tree,Red Black Tree,我已经有了用python实现红黑树的代码。然而,当我运行我的代码时,它所做的似乎只是像插入常规BST一样插入值,然后将所有内部顶点涂成红色。这是一个家庭作业的练习作业,但我被卡住了,没有其他地方可以求助 以下是我的实现: class BinaryTreeVertex(object): def __init__(self, data=None, isALeaf=True, colour='r', left=None, right=None): # Assume user w
class BinaryTreeVertex(object):
def __init__(self, data=None, isALeaf=True, colour='r', left=None, right=None):
# Assume user will pass a left & right ONLY IF isALeaf=False
self.data = data
self.isALeaf = isALeaf
self.colour = colour
self.left = left
self.right = right
class BinarySearchTree(object):
def __init__(self, root=None):
self.root = BinaryTreeVertex()
def insert(self, value):
self.root = recInsert(self.root, value)
self.root.colour = 'b'
def searchPath(self, val):
path = []
node = self.root
while node is not None:
path.append(node.data)
if val < node.data:
node = node.left
elif val > node.data:
node = node.right
else:
node = None
return path
def totalDepth(self):
if self.root.isALeaf:
return 0
else:
return recTotalDepth(self.root)
# returns the depth of a given node
# plus it's children's depths
def recTotalDepth(node, currDepth=0):
if node.isALeaf:
return 0
else:
leftDepth = 0
rightDepth = 0
if node.left is not None:
leftDepth = recTotalDepth(node.left, currDepth+1)
if node.right is not None:
rightDepth = recTotalDepth(node.right, currDepth+1)
return leftDepth + currDepth + rightDepth
# print a sideways representation of the BinarySearchTree
# Up = right, down = left
def printTree(node, indent=0):
if node.isALeaf:
return
else:
if node.right is not None:
printTree(node.right, indent+4)
print(" "*indent + str(node.data) + node.colour)
if node.left is not None:
printTree(node.left, indent+4)
# Insert a value into the binary search tree
def recInsert(node, value):
if node.isALeaf:
# Set the data to value, the colour to red, give the vertex two
# empty leaves coloured black
return BinaryTreeVertex(value, False, 'r', BinaryTreeVertex(None, True, 'b'), BinaryTreeVertex(None, True, 'b'))
elif value > node.data:
node.right = recInsert(node.right, value)
# check for balance
if node.colour == 'r':
# no balance check @ red vertices
return node
else: # node.colour is black
# Grandparents/Parents will handle balance of red vertices
# We must only perform balances at black vertices
if node.right.colour == 'r':
# right child is red, possibility for red-red conflict
if node.right.right.colour == 'r':
# red-red conflict on the right-right children
return rightRightFix(node)
elif node.right.left.colour == 'r':
# red-red conflict on the right-left children
return rightLeftFix(node)
else:
# no red-red conflicts
return node
else:
# right child is black, no fixing needed
return node
else: # value < self.data
node.left = recInsert(node.left, value)
# check for balance
if node.colour == 'r':
# no balance checks @ red vertices
return node
else: # node.colour == 'b'
# Grandparents/Parents will handle balance of red vertices
# We must only perform balances at black vertices
if node.left.colour == 'r':
# left child is red, possibility for red-red conflict
if node.left.left.colour == 'r':
# red-red conflict on the left-left children
return leftLeftFix(node)
elif node.left.right.colour == 'r':
# red-red conflict on the left-right children
return leftRightFix(node)
else:
# no red-red conflicts
return node
else:
# left child is black, no fixing needed
return node
def rightRightFix(node):
# red-red conflict on right-right children
child = node.right
sib = node.left
if sib.colour == 'r':
# no need for rotation, just recolour
child.colour == 'b'
sib.colour == 'b'
node.colour == 'r'
return node
else:
# sib's colour is black, single rot
# fix pointers first
node.right = child.left
child.left = node
# fix colours
child.colour = 'b'
node.colour = 'r'
# make the new root, which is now child
return child
def rightLeftFix(node):
# red-red conflict on right-left children
child = node.right
sib = node.left
if sib.colour == 'r':
# no need for rotation, just recolour
child.colour == 'b'
sib.colour == 'b'
node.colour == 'r'
return node
else:
# sib's colour is black, double rot
# fix the pointers
grandchild = child.left
child.left = grandchild.right
node.right = grandchild.left
grandchild.left = node
grandchild.right = child
# fix the colours
grandchild.colour == 'b'
node.colour = 'r'
# return the new root, which is now grandchild
return grandchild
def leftLeftFix(node):
# red-red conflict on right-right children
child = node.left
sib = node.right
if sib.colour == 'r':
# no need for rotation, just recolour
child.colour == 'b'
sib.colour == 'b'
node.colour == 'r'
return node
else:
# sib's colour is black, single rot
# fix pointers first
node.left = child.right
child.right = node
# fix colours
child.colour = 'b'
node.colour = 'r'
# make the new root, which is now child
return child
def leftRightFix(node):
# red-red conflict on left-right children
child = node.left
sib = node.right
if sib.colour == 'r':
# no need for rotation, just recolour
child.colour == 'b'
sib.colour == 'b'
node.colour == 'r'
return node
else:
# sib's colour is black, double rot
# fix the pointers
grandchild = child.right
child.right = grandchild.left
node.left = grandchild.right
grandchild.right = node
grandchild.left = child
# fix the colours
grandchild.colour == 'b'
node.colour = 'r'
# return the new root, which is now grandchild
return grandchild
def main():
myTree = BinarySearchTree()
myList = [13,42,3,6,23,32,72,90,1,10,26,85,88,97,73,80,35,36,88,34,12,92,100,143,123,124,125,126,127,128]
for v in myList:
myTree.insert(v)
printTree(myTree.root)
print(myTree.searchPath(12))
print(myTree.totalDepth())
myTree2 = BinarySearchTree()
myList2 = [6, 10, 20, 8, 3]
for v in myList2:
myTree2.insert(v)
printTree(myTree2.root)
print(myTree2.searchPath(6))
print(myTree2.totalDepth())
main()
类二进制TreeVertex(对象):
定义初始化(self,data=None,isALeaf=True,color=r',left=None,right=None):
#假设只有当isALeaf=False时,用户才会传递一个左键和右键
self.data=数据
self.isALeaf=isALeaf
颜色
self.left=左
self.right=right
类BinarySearchTree(对象):
定义初始化(self,root=None):
self.root=BinaryTreeVertex()
def插入(自身,值):
self.root=recInsert(self.root,value)
self.root.color='b'
def搜索路径(self,val):
路径=[]
node=self.root
当节点不是“无”时:
append(node.data)
如果valnode.data:
node=node.right
其他:
节点=无
返回路径
def总深度(自):
如果self.root.isALeaf:
返回0
其他:
返回rectotalepth(self.root)
#返回给定节点的深度
#再加上儿童的深度
def rectotalepth(节点,currDepth=0):
如果node.isALeaf:
返回0
其他:
leftDepth=0
rightDepth=0
如果node.left不是None:
leftDepth=recTotalDepth(node.left,currDepth+1)
如果node.right不是None:
rightDepth=recTotalDepth(node.right,currDepth+1)
返回leftDepth+currDepth+rightDepth
#打印BinarySearchTree的横向表示形式
#上=右,下=左
def打印树(节点,缩进=0):
如果node.isALeaf:
返回
其他:
如果node.right不是None:
打印树(node.right,缩进+4)
打印(“*indent+str(node.data)+node.color)
如果node.left不是None:
打印树(node.left,缩进+4)
#在二进制搜索树中插入一个值
def recInsert(节点,值):
如果node.isALeaf:
#将数据设置为值,颜色设置为红色,给顶点两个
#黑色的空叶子
返回BinaryTreeVertex(值,False,'r',BinaryTreeVertex(无,True,'b'),BinaryTreeVertex(无,True,'b'))
elif值>节点数据:
node.right=重新插入(node.right,值)
#检查余额
如果node.color==“r”:
#无平衡检查@红色顶点
返回节点
其他:#节点。颜色为黑色
#祖父母/父母将处理红色顶点的平衡
#我们必须只在黑色顶点执行平衡
如果node.right.color==“r”:
#右边的孩子是红色的,红色冲突的可能性
如果node.right.right.color=='r':
#正确儿童的红色冲突
返回RightFix(节点)
elif node.right.left.color=='r':
#左右儿童之间的红色冲突
返回rightLeftFix(节点)
其他:
#没有红色冲突
返回节点
其他:
#右边的孩子是黑色的,不需要修理
返回节点
其他:#值def searchPath(self, val):
path = []
node = self.root
while node is not None:
path.append(node.data)
# break the while when node is the leave
if (node.isALeaf == True):
break
if val < node.data:
node = node.left
elif val > node.data:
node = node.right
else:
node = None
return path