红黑树插入未分配正确的颜色

问题描述

我已经用Python实现了Red-Black树,这是我的代码

class node:
    def __init__(self,value,p,dx,sx):
        self.key = value
        self.p = p
        self.right = dx
        self.left = sx

class node_ABR(node):
    def __init__(self,sx):
        node.__init__(self,sx)
        self.color = None

class ABR:
    def __init__(self,nodes,root):
        self.root = root
        self.nodes = nodes

    def tree_insert(self,node):
        y = None
        x = self.root
        while x!= None and x.key != None:
            y = x
            if node.key < x.key:
                x = x.left
            else:
                x = x.right
        node.p = y
        if y == None and self.root == None:
            self.root = node
        elif node.key < y.key:
            node.p.left = node
        else:
            node.p.right = node

class ARN(ABR):
    def __init__(self):
        self.nodes = None
        self.nil = node_ABR(None,None,None)
        self.root = self.nil
        self.nil.color = 'BLACK'
        self.fixupTime = 0

    def left_rotate(self,x):
        y = x.right
        x.right = y.left
        if y.left != self.nil:
            y.left.p = x
        y.p = x.p
        if x.p == self.nil:
            self.root = y
        elif x == x.p.left:
            x.p.left = y
        else:
            x.p.right = y
        y.left = x
        x.p = y
    def right_rotate(self,x):
        y = x.left
        x.left = y.right
        if y.right != self.nil:
            y.right.p = x
        y.p = x.p
        if x.p == self.nil:
            self.root = y
        elif x == x.p.right:
            x.p.right = y
        else:
            x.p.left = y
        y.right = x
        x.p = y

    def tree_insert(self,z):
        y = self.nil
        x = self.root
        while x!= self.nil:
            y = x
            if z.key < x.key:
                x = x.left
            else:
                x = x.right
        z.p = y
        if y == self.nil:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z
        z.left = self.nil
        z.right = self.nil
        z.color = 'RED'
        self.RB_insert_fixup(z)

    def RB_insert_fixup(self,z):
        startTime = time.time()
        while z.p.color == 'RED':
            if z.p == z.p.p.left:
                y = z.p.p.right
                if y.color == 'RED':
                    y.p.color = 'BLACK'
                    y.color = 'BLACK'
                    z.p.p.color = 'RED'
                else:
                    if z == z.p.right:
                        z = z.p
                        self.left_rotate(z)
                    z.p.color = 'BLACK'
                    z.p.p.color = 'RED'
                    self.right_rotate(z.p.p)
            else:
                y = z.p.p.left
                if y.color == 'RED':
                    y.p.color = 'BLACK'
                    y.color = 'BLACK'
                    z.p.p.color = 'RED'
                else:
                    if z == z.p.left:
                        z = z.p
                        self.right_rotate(z)
                    z.p.color = 'BLACK'
                    z.p.p.color = 'RED'
                    self.left_rotate(z.p.p)
        self.root.color = 'BLACK'
        self.fixupTime += (time.time() - startTime)


    def inorder_tree_walk(self,x):
        if x != self.nil:
            self.inorder_tree_walk(x.left)
            print(x.key,x.color)
            self.inorder_tree_walk(x.right)

此实现的问题是项目在树中的顺序正确,并且我敢肯定,这是因为inorder-tree-walk以正确的顺序打印元素,但是与上图的可视化相比,颜色是错误this网站。 我从大学获得了该算法,它看起来与在线找到的算法相同。问题可能在哪里?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)