9 U9 j- k+ ?( t( @7 B+ z5 Q if node is not None: # ^) b( \1 u2 F) n& } node.color=BLACK 8 g+ \; W) @& l! Q) D4 A9 r
. s: w, J" e8 W5 B# S: y
def rb_erase(self,data):$ z- d8 W: O, w1 ?
tmp_node=Node(data)0 e! ^! E5 V# m* V* L: c
node,parent = self.rb_search_auxiliary(tmp_node) ( a4 B- J! D2 o2 j. k1 C7 c if node is None: 9 t# u! ]7 t5 y& @/ q7 f- \ print "data is not exist." + g* u9 i& w% b3 R C5 L% w" } return % Z+ v$ }% g4 ?6 M9 c) G ) w/ d0 T6 {& b2 ?/ F8 r old =node( Z+ J( S) W/ E- L! {$ f
if node.left and node.right: & l. o1 Z( E0 G" U; x7 Z node=node.right 5 T9 {6 o9 q$ R3 _& Q+ H) t ) K8 R+ [. A |3 |8 v& ?: ]4 d3 l left=node.left $ @2 {8 b' |- V4 f9 L; a% K" Q. c1 H while left is not None:7 R; U( V( ^& q+ w* l$ t
node =left & H8 b+ P: L$ M left=node.left$ F4 ^1 V, h# A D9 ^
# R9 O4 U9 |& H7 h {% H$ S
child=node.right \* h! v6 \: ~ parent=node.parent t; W0 U* Y. Q( F) x }: w4 q color=node.color; z$ {2 j/ u4 I! @
, n9 a2 u8 C0 r5 ]$ _& x7 P9 T: x
if child:1 l; x6 ?# t' ^. F
child.parent=parent + I4 C: ^: T6 f5 {5 u if parent: # ^& a4 j. z0 C, b if parent.left==node: ! A/ o k: q8 O. R: M4 e& M parent.left=child/ m% [7 F* Q1 E5 v( }
else: * c) @9 u' i# F/ L8 K1 Q parent.right=child/ w$ X( T8 g/ Z& E' T
$ s+ a/ q6 }3 p# ?7 {2 a
else:( j0 v6 g {% x# ~6 S/ {
self.root=child7 a/ \* h V& @- [4 h
b4 }6 W) s9 i- X( v( _ V
if node.parent==old: 0 V; E2 t; o" F9 t parent=node % u1 I6 ~' l# F7 @9 z node.parent=old.parent ! k! i3 a2 Z9 ?0 P: u4 v; N node.color=old.color ) H: Y7 |# f% m7 F/ Y( m" k) a* ~8 B node.right=old.right m u2 G; E, x1 K/ M2 X node.left=old.left / p% r2 U3 `( H7 o. a( s' o6 c ( J/ ]3 \( b/ W) M0 q: A0 {. } if old.parent: - x& [6 j& h, H if old.parent.left==old:- N4 x- \- ]0 U7 r7 B- o
old.parent.left=node0 o- t1 m* K& X* T c( h2 h
else: + L+ S0 a {' d; N: w8 O old.parent.right=node 1 H- W- R) q+ @3 P* L0 S else: 9 N! D% w9 i) @: f) a( [7 \( v self.root=node) b3 y3 E4 e% }, x3 P
/ H& P; l& I( [3 j% R. H/ e" k old.left.parent=node $ Y- `2 z1 C/ D$ K* ` R if old.right:2 J% |2 E: q& N6 ~* j7 s: H$ _
old.right.parent=node" A, [6 _+ n5 {. U5 A3 s
; }6 @! U+ V) M" x
else: 1 h" i6 T% Z) M if node.left is None: 7 L& t8 [. t3 b( k7 g4 _* h child =node.right + O2 D3 k6 @ l2 | s5 L" y else: 7 v& E0 p: D! h1 o' b+ a' K if node.left is not None:0 W( f- C/ n: \
child=node.left - s+ F, g% t$ N3 T else: " G( B+ B& O8 P# r child=None ( ?, I, o% l: `" g0 ~# {9 s parent=node.parent # g+ f- ?: X: [$ s color=node.color $ U5 |- ^) l+ E6 ` if child: 8 M# M8 J, ?* R: t* N- j8 x# e" M child.parent=parent# v2 y, G+ _3 u5 ^# d
if parent: 2 ]5 [9 \# n( j5 _* s: a9 ^ if parent.left==node: , n X% D1 q* p9 O% `4 P, \ parent.left=child ! r* a- I! c: H/ Z9 F- U else:" W; r9 k; K' H1 W u4 I: @+ k# I" o* I
parent.right=child! @5 e. g4 H4 r) k
else: L4 r8 p, R. r& P* r self.root=child # f! t7 \& n/ j! R- M4 A/ F5 @! x/ G& M$ F. V0 v3 M9 ?) e) L
if color==BLACK:& t& L( Z* C, k5 ^0 x2 O
, D' D7 y% G+ ?, d8 D6 }7 v# z
self.rb_erase_rebalance(child,parent) # _" h! I/ T5 c8 T" I" p p" V. ^! w, q% b" o' { / l$ q7 v y9 k def rb_travelse(self,node):$ F9 v* D/ l2 {
if node is not None:0 ^5 i5 w8 d. \- \$ C4 X* u- h- t
print str(node.data)+'\t'+str(node.color)1 \0 b& f2 k' _. l: H* Y+ v$ ^( J
self.rb_travelse(node.left)/ c% a0 L: l) |$ D; r* q
if node.parent: / j* F* s, k* z+ t& H" i if node.parent.color==0 and node.color==0:/ f& C* ^, {5 h* M# E
print "error"* P/ @) X3 ]# g, J) {
return # L" K2 l) h1 g' o self.rb_travelse(node.right)/ t7 A+ L! W& |8 o$ |! q- t: j
if node.parent:/ X: ?' L: {# }6 w' ]2 s4 Y
if node.parent.color==0 and node.color==0:; Q! z/ k9 Y5 w2 x
print "error" ) i% o* Z7 S: K7 F: ~8 L ^ return! v# r+ |. n( x1 R1 y
8 e/ q% V, s6 X0 B. C+ t
return) r. o- w& B1 a3 N
7 V/ v2 Q/ _( \7 I' b
% W, g# W% F T4 E$ c# @
def cmp(self,node1,node2):" P' u% W U* N
if node1.data>node2.data :7 ^9 a, N5 p/ v
return 1 ( U1 q/ w7 |9 K G: h. c if node1.data==node2.data : 7 W3 I4 u* [3 k$ l1 l) Z* L3 O! ~ return 0! m! i: Q. U8 g0 U9 T+ A
if node1.data<node2.data:4 H4 Y7 B" I8 S# B) q; ?
return -1 3 t' e& Y3 ?! b6 l Y% w. @ # x& G0 B! B6 n) w4 ~if __name__=="__main__": . u K) |) S+ f1 g5 V. _2 } print "main"7 U7 A& b% p: l3 L
data=[28,6,39,78,6,43,61,56,71,38]$ z% E3 E& m1 s5 W/ Q
#for i in range(10):9 l1 m- V- M; ?5 W' ^1 r
# rand_num = random.randint(0, 100)" m! ~; k' ] b& p) R# m
# data.append(rand_num) 3 v8 { m" E! I1 i7 l #print data4 e! ]8 X6 x/ h) m# `! h0 w2 Q# B
t=RBtree() 9 C0 x8 i" W* n5 w0 V8 z- f- }( x3 ]8 `: C# v
7 h; A/ C, T6 p. b+ f for i in range(10): ; W+ f1 o9 S; R ^! A4 p+ Z t.rb_insert(data[i])* ~- z! [" Z$ O; ~1 x5 p6 h9 ]
4 J; K9 h+ N! R; k
% N0 J2 z' K6 G
t.rb_travelse(t.root); Z/ N- V8 P. e& q* Q
4 \- L9 H4 N: I. D( e- i, W8 w print "---------------------------------"2 L' Z8 u' b6 H, y' B, S0 N
t.rb_erase(data[7]) 6 o8 a a2 c% b0 t# { # e! J4 G; q7 F
t.rb_travelse(t.root)# I4 B- E3 Y1 o
& a+ a# n- J" j; [