Black = 0 RED = 1 #graphic elements of rbtree for printing VC = ' │ ' HC = '-' size = 3 RIG = ' ┌ ' + HC * SIZE LEF = ' └ ' + HC * Size SP = CHR IND1 = SP * (SIZE + 1) IND2 = VC + SP * SIZE class Rbnode (object): Def __init__ (self, key=none , Value=none, color=black): Self.key = key Self.value = value Self.color = Color Self.left = None Self.right = None self.p = None def __repr__ (self): return '%s%s%s '% (Self.key, ' I
F Self.color is black else ' ◇ ', Self.value ' class Rbtree (object): Def __init__ (self, data=false,default_value=0): Self.nil = Rbnode () self.root = Self.nil Self.default_value = default_value #for method:force_sear CH If hasattr (data, ' __iter__ '): For key, value in Data:self.insert (Rbnode (Key,value)
def __repr__ (self): return ' \ n '. Join (Self.graph ()) def graph (self, x=false, prefix= '): "Beautifully print Rbtree, the big key node, the If x is false:x = Self.root if x are not self.nil:p = X.P Last_prefix = ' If p is not self.nil:pp = P.P Last_prefix = LEF if P.left is x else RIG if PP isn't self.nil:if (Pp.left is P) are (P.left is X
): prefix = prefix + IND1 else:prefix = prefix + IND2 Yield from self.graph (x.right, prefix) yield '%s%s%s ' (prefix, last_prefix, x) Yiel D from Self.graph (X.left, prefix) def search (self, Key, X=false): "Finds node according to Key, return Self.ni
L If not found ' if x is false:x = Self.root while (x is not self.nil) and (Key!= X.key):
If key < X.key:x = X.left else:x = x.right return x
def insert (self, z): "Insert Z node with key and value" y = self.nil x = self.root while x are not self.nil:
y = x if z.key < x.key:x = X.left else:x = x.right
Z.P = y if y is 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
. Insert_fixup (z) def delete (self, z): y = z y_original_color = Y.color if Z.left is Self.nil:
x = Z.right Self.transplant (z, x) elif z.right is self.nil:x = Z.left
Self.transplant (z, x) else:y = Self.minimum (z.right) Y_original_color = Y.color x = Y.right if y.p is z:x.p = y else:self.transplant (y, x) Y.right = z.right y.right.p = y Self.transplant (z, y) y.left = Z.left Y.le FT.P = y Y.color = Z.color if Y_original_color is BLACK:self.delete_fixup (x) def is_ Empty (self): return self.root are Self.nil def right_walk (self, x=false): If X is false:x
= Self.root If x is isn't Self.nil:yield from Self.right_walk (x.right) yield X
Yield from Self.right_walk (x.left) def left_walk (self, x=false): if x is false:x = Self.root If x is not Self.nil:yield to Self.left_walk (x.left) yield x yield from self. Left_walk (x.right) def force_search (self,key): y = self.nil x = self.root while x are not self
. nil:if key = = X.key:return X y = x if key < x.key:
x = X.left else:x = X.right z = Rbnode () Original_z = Z Z.key = key Z.value = self
. default_value z.p = y if y is 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.insert_fixup (z) return original_z def maximum (self, x=false): If X is Fals E:x = Self.root while x.right isn't self.nil:x = x.right return x def mini Mum (self, x=false): if x is false:x = Self.root while x.left are not self.nil:x = X.left return x def successor (self, X): "Return node with smallest key greater than X.key" I F x.right is isn't Self.nil:return self.minimum (x.right) y = x.p while (Y are not self.nil) and
(x is Y.right): x = y y = y.p return y-def predecessor (self, X): "Return node with biggest key lower than X.key ' if X.left ' not Self.nil:return self.maximum (x.left) y = x.p while (Y is
Not Self.nil) and (X are y.left): x = y y = y.p return y def left_rotate (self, x):
y = x.right X.right = Y.left If y.left is not SELF.NIL:Y.LEFT.P = x Y.P = X.P If X.P is self.nil:self.root = y else:if x is 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 is not SELF.NIL:Y.RIGHT.P = x Y.P = X.P If X.P is self.nil:self.root = y else:if x is X.p.right:x.p.righ
T = y else: X.p.left = y Y.right = x x.p = y def insert_fixup (self, z): while Z.p.color I
S Red:if z.p is z.p.p.left:y = Z.p.p.right if Y.color is RED: Z.p.color = Black Y.color = Black Z.p.p.color = RED z =
Z.P.P else:if Z is 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 is RED: Z.p.color = Black Y.color = Black Z.p.p.color = RED z = z.p. P Else:if Z is 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) s
Elf.root.color = Black def delete_fixup (self, x): while (X is not self.root) and (X.color are black):
If x is x.p.left:w = X.p.right if w.color are red:w.color = Black
X.p.color = RED self.left_rotate (x.p) w = x.p.right If (W.left.color is black) and (W.right.color are black): W.color = RED x =
X.P else:if W.right.color is Black:w.left.color = Black
W.color = RED self.right_rotate (w) w = x.p.right
W.color = X.p.color X.p.color = Black W.right.color = Black Self.left_rOtate (X.P) x = self.root Else:w = X.p.left if W.color is
Red:w.color = Black X.p.color = RED self.right_rotate (X.P)
W = X.p.left if (W.right.color is black) and (W.left.color are black):
W.color = RED x = x.p else:if W.left.color is black:
W.right.color = Black W.color = RED self.left_rotate (w)
w = x.p.left W.color = X.p.color X.p.color = Black W.left.color = Black Self.right_rotate (X.P) x = Self.root X.color = Black def transplant (self, U, v): If u.p are self.nil:self.root = v elif u is u.p.left : U.p.left = V else:u.p.right = v V.P = U.P if __name__ = = ' __main__ ': trd={' a ': 7, ' B ': 1, ' C ': 8, ' d ': 2 , ' E ': 3} tr=rbtree (Trd.items ()) print ("After builds from Dict:") print (TR) to C in ' SFSDFSAHFWERUOIWEJUCKJD
FLSFJ ': nd = Tr.force_search (c) Nd.value + + 1 print ("\nafter insert from a string:") print (TR)
While Tr.root!=tr.nil:tr.delete (tr.root) print ("\nafter Delete all node:") print (TR)
Print results: