Red Black Tree Prof Amir Geva Netzer Eitan
Properties Every node is either red or black Every son of a leaf is black (A.K.A null is black) Son of a red node cant be red (A.K.A no two reds in a row) For every track from the root to a leaf contains the same number of black node on the way
Rotates
Insert Add new like normal binary search tree Paint new node as red If new node father is black then red black tree properties are held See options ahead
Notation X: new node F: father(X) G: father(F) (if exist) U: son(G) s.t U~=F (if exist) F color is red G color is black
A) F is root F F X X
B) U is red G G U U F F X X
(mirror) G G U U F X X F
(mirror) F G U F X G U X
Insert summary If After inserting if is G painted Red continue processes up the tree Else Done Time complexity
Delete If delete node has both sons replace delete node with minimum node in right sub-tree (new delete node has only one son if any) If delete node is red then red black tree properties are held If delete node is black and its son is red, paint its son black and delete node Both delete node are black
Notation Y: node to delete X: son(Y) W: brother of X AFTER we delete Y F: father of X and W AFTER we delete Y A,B have sub trees (the have sons) X and Y are black
A) W is red W F B W F X B A A X
B) W is black and F is red F F W W X X B B A A
C) W, F and the sons of W are black X X B B A A
D) W is black and it’s a right child of its father and has left red son X W X W B A B
(solution for D) F W W X A F A B B X
Delete summary If After deletion processes continue up or down the tree *circled node is the new “X” in other cases Else Done Time complexity