Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何放置';零节点';在红黑树代码中?_Python_Tree - Fatal编程技术网

Python 如何放置';零节点';在红黑树代码中?

Python 如何放置';零节点';在红黑树代码中?,python,tree,Python,Tree,我需要实现红黑树 我知道每个叶节点都应该指向“data:NIL/color:Black”中的一个节点 我试过了 def insert(self, value, tree): if tree == None and self.root == None: self.root = Node(value, 'B') NILnode = Node('NIL', 'B') self.root.left = NILnode self.r

我需要实现红黑树

我知道每个叶节点都应该指向“data:NIL/color:Black”中的一个节点

我试过了

def insert(self, value, tree):
    if tree == None and self.root == None:
        self.root = Node(value, 'B')

        NILnode = Node('NIL', 'B')
        self.root.left = NILnode
        self.root.right = NILnode
        return
它失败了

这是我的密码

class Node():
    def __init__(self, data, color='B', left=None, right=None, parent=None):
        self.value = data
        self.left = left
        self.right = right
        self.parent = parent
        self.color = color        

    def __str__(self):
        return self.value.__str__()

class RBT:
    def __init__(self):
        self.root = None

    def insert(self, value, tree):
        if tree == None and self.root == None:
            self.root = Node(value, 'B')
            return

        if tree.value < value:
            if tree.right == None or tree.right.value == 'NIL':
                node = Node(value, 'R')
                tree.right = node
                node.parent = tree
                self.rebuild(node)

            else:
                self.insert(value, tree.right)

        else:
            if tree.left == None or tree.left.value == 'NIL':
                node = Node(value, 'R')
                tree.left = node
                node.parent = tree
                self.rebuild(node)

            else:
                self.insert(value, tree.left)

    def rebuild(self, node):
        while node.parent != None and node.parent.color == 'R':
            parent_sibling = self.get_sibling(node.parent)

            #case-1
            if parent_sibling.color == 'R':
                node.parent.color == 'B'
                parent_sibling.color == 'B'
                node.parent.parent.color == 'R'
                node = node.parent.parent
            else:
                if self.which_child(node.parent) == 'L':
                    #case-2-1
                    if self.which_child(node) == 'R' and self.which_child(node.parent) == 'L':
                        self.rotate_left(node.parent)
                        node = node.left
                    #case-2-2
                    if self.which_child(node) == 'L' and self.which_child(node.parent) == 'L':
                        node.parent.color = 'B'
                        node.parent.parent.color = 'R'
                        self.rotate_right(node.parent.parent)
                else:
                    #case-2-3
                    if self.which_child(node) == 'L' and self.which_child(node.parent) == 'R':
                        self.rotate_right(node.parent)
                        node = node.right
                    #case-2-4
                    if self.which_child(node) == 'R' and self.which_child(node.parent) == 'R':
                        node.parent.color = 'B'
                        node.parent.parent.color = 'R'
                        self.rotate_left(node.parent.parent)
class节点():
定义初始化(self,data,color='B',left=None,right=None,parent=None):
self.value=数据
self.left=左
self.right=right
self.parent=parent
self.color=颜色
定义(自我):
返回self.value.\uuu str\uuuu()
类别RBT:
定义初始化(自):
self.root=None
def插入(自身、值、树):
如果树==无且self.root==无:
self.root=节点(值“B”)
返回
如果tree.value
我没有发布最后一个代码,因为它看起来像rotate() 函数与NIL无关

如何将“NIL节点”放入红黑树代码中


感谢您阅读冗长的代码。

问题究竟出在哪里还不清楚-您说您失败了,它不起作用,但您不清楚您期望或需要什么以及您得到了什么。但是,您的代码显然是错误的,因为您创建了一个
节点('NIL','B')
节点,然后继续将
.left
.right
指向同一个节点-除非您想象使用一个“NIL节点”,否则这将永远不会按预期工作。但是,我想知道为什么你不使用
None
来表示死胡同。很抱歉,我的回复太晚了。正如你所说,我没有使用NIL而使用None,并且左和右分别指向对方None。我想我得到了我想要的。非常感谢。现在还不清楚到底是什么问题——你说你失败了,它不起作用,但你不清楚你期望或需要什么,以及你得到了什么。但是,您的代码显然是错误的,因为您创建了一个
节点('NIL','B')
节点,然后继续将
.left
.right
指向同一个节点-除非您想象使用一个“NIL节点”,否则这将永远不会按预期工作。但是,我想知道为什么你不使用
None
来表示死胡同。很抱歉,我的回复太晚了。正如你所说,我没有使用NIL而使用None,并且左和右分别指向对方None。我想我得到了我想要的。非常感谢你。