280 likes | 707 Views
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)
E N D
Red Black Tree Prof Amir Geva NetzerEitan
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
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 F F G G X X U U
C) X is right son of F and U is right of G(mirror) F X G G X U U F
F D) X is left son of F and U is right son of F(mirror) X F G U X G U
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 nod and its 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 F W W B X F 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 F F W W X X B B A A
D) W is black and it’s a right child of its father and has left red son F F A X W X W B A B
E) W is black and has v red son(solution for D) F W W A X 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