我的目标是创造一个功能红黑树。我理解这个理论概念,但是我不理解如何使用Python中的类。我在想:
实现诸如:
RED = 'R'
BLACK = 'B'
class Node:
def __init__(self):
self._color = BLACK
self._key = self._left = self._right = self._p = None
class Tree:
nil = Node()
nil.color = BLACK
def __init__(self, root=nil):
self.root = root
def rb_insert(self, z):
y = self.nil
...
z.p = y # ISSUE... How should I fix it?
但正如预期的那样,z没有属性p(因为它是一个整数)。
因此,我想更改节点的构造函数以接收密钥,但在本例中,我不知道如何处理“nil”节点(它出现在每个叶中)。