SlideShare a Scribd company logo
Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored  red  or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes
Red Black Trees Colored Edges Definition Binary search tree. Child pointers are colored  red  or black. Pointer to an external node is black. No root to external node path has two consecutive  red  pointers. Every root to external node path has the same number of black pointers.
Example Red-Black Tree 10 7 8 1 5 30 40 20 25 35 45 60 3
Properties The height of a red black tree that has  n  (internal) nodes is between  log 2 (n+1)  and  2log 2 (n+1) .
Properties Start with a red black tree whose height is  h ; collapse all red nodes into their parent black nodes to get a tree whose node-degrees are between  2  and  4,  height is  >= h/2 ,   and all external nodes are at the same level.
Properties 10 7 8 1 5 30 40 20 25 35 45 60 3
Properties Let  h’>= h/2  be the height of the collapsed tree.  In worst-case,  all internal nodes of collapsed tree have degree  2 . Number of internal nodes in collapsed tree  >= 2 h’ -1 . So,  n   >= 2 h’ -1 So,  h <= 2 log 2  (n + 1)
Properties At most  1  rotation and  O(log n)  color flips per insert/delete. Priority search trees. Two keys per element. Search tree on one key, priority queue on other. Color flip doesn’t disturb priority queue property. Rotation disturbs priority queue property. O(log n)  fix time per rotation  =>  O(log 2 n)  overall time.
Properties O(1)  amortized complexity to restructure following an insert/delete. C++ STL implementation java.util.TreeMap  =>  red black tree
Insert New pair is placed in a new node, which is inserted into the red-black tree. New node color options. Black node  =>  one root-to-external-node path has an extra black node (black pointer). Hard to remedy. Red node  =>  one root-to-external-node path may have two consecutive red nodes (pointers). May be remedied by color flips and/or a rotation.
Classification Of 2 Red Nodes/Pointers XYz X  =>  relationship between  gp  and  pp . pp  left child of  gp  =>  X = L . Y  =>  relationship between  pp  and  p . p  right child of  pp  =>  Y = R . z = b  (black) if  d = null  or a black node. z = r  (red) if  d  is a red node. LLb a b c d gp pp p
XYr Color flip. Move  p ,  pp , and  gp  up two levels. Continue rebalancing if necessary. a b c d gp pp p a b c d gp pp p
LLb Rotate. Done! Same as LL rotation of AVL tree. y x a b z c d a b c d gp pp p x y z
LRb Rotate. Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric. y x a b z c d b c a d gp pp p y x z
Delete Delete as for unbalanced binary search tree. If red node deleted, no rebalancing needed. If black node deleted, a subtree becomes one black pointer (node) deficient.
Delete A Black Leaf Delete  8 . 10 7 8 1 5 30 40 20 25 35 45 60 3
Delete A Black Leaf y y  is root of deficient subtree. py  is parent of  y . py 10 7 1 5 30 40 20 25 35 45 60 3
Delete A Black Degree 1 Node Delete  45 . y y  is root of deficient subtree. py 10 7 8 1 5 30 40 20 25 35 45 60 3
Delete A Black Degree 2 Node Not possible, degree 2 nodes are never deleted. 10 7 8 1 5 30 40 20 25 35 45 60 3
Rebalancing Strategy If  y  is a red node, make it black. 10 7 8 1 5 30 40 20 25 35 45 60 3 y py
Rebalancing Strategy Now, no subtree is deficient.  Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y py
Rebalancing Strategy y  is a black root (there is no  py ). Entire tree is deficient. Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y
Rebalancing Strategy y  is black but not the root (there is a  py ). Xcn y  is right child of  py   =>  X = R . Pointer to  v  is black  =>  c = b . v  has 1 red child  =>  n = 1 . a b y py v
Rb0 (case 1) Color change. Now,  py  is root of deficient subtree. Continue! a b y py v y a b py v
Rb0 (case 2) Color change. Deficiency eliminated. Done! a b y py v y a b py v
Rb1 (case 1) LL rotation. Deficiency eliminated. Done! a b y py v a b y v py
Rb1 (case 2) LR rotation. Deficiency eliminated. Done! a y py v b c w c y w py a b v
Rb2 LR rotation. Deficiency eliminated. Done! a y py v b c w c y w py a b v
Rr(n) n =  # of red children of  v ’s right child  w . Rr(2) a y py v b c w
Rr(0) LL rotation. Done! a b y py v a b y v py
Rr(1) (case 1) LR rotation. Deficiency eliminated. Done! a y py v b w c y c w py a v b
Rr(1) (case 2) Rotation. Deficiency eliminated. Done! a y py v b w c d x y d x py a v b c w
Rr(2) Rotation. Deficiency eliminated. Done! a y py v b w c d x d y x py a v b c w

More Related Content

PPTX
Red black trees1109
PPTX
Red black trees
PPTX
Red black tree
PDF
Red black tree
PPTX
Red black tree
PDF
Red Black Trees
PPT
Red Black Trees
Red black trees1109
Red black trees
Red black tree
Red black tree
Red black tree
Red Black Trees
Red Black Trees

What's hot (20)

PPTX
Red black trees
PPTX
Balanced Tree(AVL Tree,Red Black Tree)
PDF
Red black trees
PPT
Red black tree
PDF
Red black tree
PPT
Red black trees presentation
PDF
10 Red-Black Trees
PPT
Advanced data structures and implementation
PPT
Red-black trees
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PPT
PPTX
Balance tree. Short overview
PPT
lecture 14
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Chap 7 binary threaded tree
PPT
Data Structure: TREES
PPT
Trees
PPT
Avl trees
PPSX
Data Structure (Tree)
Red black trees
Balanced Tree(AVL Tree,Red Black Tree)
Red black trees
Red black tree
Red black tree
Red black trees presentation
10 Red-Black Trees
Advanced data structures and implementation
Red-black trees
Balanced Tree (AVL Tree & Red-Black Tree)
Balance tree. Short overview
lecture 14
Trees, Binary Search Tree, AVL Tree in Data Structures
Chap 7 binary threaded tree
Data Structure: TREES
Trees
Avl trees
Data Structure (Tree)
Ad

Similar to Red black 1 (20)

PPTX
Red-black-tree presentation in Algorithm
PPT
16 rbtrees
PPT
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
PPT
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
PPT
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
PPT
rbtrees.ppt
PPT
Red-black_trees.ppt by shivam sharma ofo
PPT
operations insertion Red-black_treesver1.ppt
PDF
Red-Black Tree Presentation By Mobin Nesari.pdf
PPTX
Data structure
PPTX
Kishan Kaushik - Red Black Tree Presentation
PDF
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
PPT
Red blacktrees
PDF
Cse 225 rbt_red_black_tree
PPTX
red black tree.pptxMMMMMMMMMMMMMMMMMMMMMMMMMM
PPT
ch10_EffiBinSearchTrees ch10_EffiBinSearchTrees
PPT
ch10_EffiBinSearchTrees.ppt
PDF
bst trees AVL trees-red black trees spay trees
PPT
PDF
docsity-red-black-trees-data-structures-lecture-slides_2.pdf
Red-black-tree presentation in Algorithm
16 rbtrees
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
rbtrees.ppt
Red-black_trees.ppt by shivam sharma ofo
operations insertion Red-black_treesver1.ppt
Red-Black Tree Presentation By Mobin Nesari.pdf
Data structure
Kishan Kaushik - Red Black Tree Presentation
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
Red blacktrees
Cse 225 rbt_red_black_tree
red black tree.pptxMMMMMMMMMMMMMMMMMMMMMMMMMM
ch10_EffiBinSearchTrees ch10_EffiBinSearchTrees
ch10_EffiBinSearchTrees.ppt
bst trees AVL trees-red black trees spay trees
docsity-red-black-trees-data-structures-lecture-slides_2.pdf
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Business Ethics Teaching Materials for college
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPH.pptx obstetrics and gynecology in nursing
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Business Ethics Teaching Materials for college
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
human mycosis Human fungal infections are called human mycosis..pptx

Red black 1

  • 1. Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes
  • 2. Red Black Trees Colored Edges Definition Binary search tree. Child pointers are colored red or black. Pointer to an external node is black. No root to external node path has two consecutive red pointers. Every root to external node path has the same number of black pointers.
  • 3. Example Red-Black Tree 10 7 8 1 5 30 40 20 25 35 45 60 3
  • 4. Properties The height of a red black tree that has n (internal) nodes is between log 2 (n+1) and 2log 2 (n+1) .
  • 5. Properties Start with a red black tree whose height is h ; collapse all red nodes into their parent black nodes to get a tree whose node-degrees are between 2 and 4, height is >= h/2 , and all external nodes are at the same level.
  • 6. Properties 10 7 8 1 5 30 40 20 25 35 45 60 3
  • 7. Properties Let h’>= h/2 be the height of the collapsed tree. In worst-case, all internal nodes of collapsed tree have degree 2 . Number of internal nodes in collapsed tree >= 2 h’ -1 . So, n >= 2 h’ -1 So, h <= 2 log 2 (n + 1)
  • 8. Properties At most 1 rotation and O(log n) color flips per insert/delete. Priority search trees. Two keys per element. Search tree on one key, priority queue on other. Color flip doesn’t disturb priority queue property. Rotation disturbs priority queue property. O(log n) fix time per rotation => O(log 2 n) overall time.
  • 9. Properties O(1) amortized complexity to restructure following an insert/delete. C++ STL implementation java.util.TreeMap => red black tree
  • 10. Insert New pair is placed in a new node, which is inserted into the red-black tree. New node color options. Black node => one root-to-external-node path has an extra black node (black pointer). Hard to remedy. Red node => one root-to-external-node path may have two consecutive red nodes (pointers). May be remedied by color flips and/or a rotation.
  • 11. Classification Of 2 Red Nodes/Pointers XYz X => relationship between gp and pp . pp left child of gp => X = L . Y => relationship between pp and p . p right child of pp => Y = R . z = b (black) if d = null or a black node. z = r (red) if d is a red node. LLb a b c d gp pp p
  • 12. XYr Color flip. Move p , pp , and gp up two levels. Continue rebalancing if necessary. a b c d gp pp p a b c d gp pp p
  • 13. LLb Rotate. Done! Same as LL rotation of AVL tree. y x a b z c d a b c d gp pp p x y z
  • 14. LRb Rotate. Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric. y x a b z c d b c a d gp pp p y x z
  • 15. Delete Delete as for unbalanced binary search tree. If red node deleted, no rebalancing needed. If black node deleted, a subtree becomes one black pointer (node) deficient.
  • 16. Delete A Black Leaf Delete 8 . 10 7 8 1 5 30 40 20 25 35 45 60 3
  • 17. Delete A Black Leaf y y is root of deficient subtree. py is parent of y . py 10 7 1 5 30 40 20 25 35 45 60 3
  • 18. Delete A Black Degree 1 Node Delete 45 . y y is root of deficient subtree. py 10 7 8 1 5 30 40 20 25 35 45 60 3
  • 19. Delete A Black Degree 2 Node Not possible, degree 2 nodes are never deleted. 10 7 8 1 5 30 40 20 25 35 45 60 3
  • 20. Rebalancing Strategy If y is a red node, make it black. 10 7 8 1 5 30 40 20 25 35 45 60 3 y py
  • 21. Rebalancing Strategy Now, no subtree is deficient. Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y py
  • 22. Rebalancing Strategy y is a black root (there is no py ). Entire tree is deficient. Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y
  • 23. Rebalancing Strategy y is black but not the root (there is a py ). Xcn y is right child of py => X = R . Pointer to v is black => c = b . v has 1 red child => n = 1 . a b y py v
  • 24. Rb0 (case 1) Color change. Now, py is root of deficient subtree. Continue! a b y py v y a b py v
  • 25. Rb0 (case 2) Color change. Deficiency eliminated. Done! a b y py v y a b py v
  • 26. Rb1 (case 1) LL rotation. Deficiency eliminated. Done! a b y py v a b y v py
  • 27. Rb1 (case 2) LR rotation. Deficiency eliminated. Done! a y py v b c w c y w py a b v
  • 28. Rb2 LR rotation. Deficiency eliminated. Done! a y py v b c w c y w py a b v
  • 29. Rr(n) n = # of red children of v ’s right child w . Rr(2) a y py v b c w
  • 30. Rr(0) LL rotation. Done! a b y py v a b y v py
  • 31. Rr(1) (case 1) LR rotation. Deficiency eliminated. Done! a y py v b w c y c w py a v b
  • 32. Rr(1) (case 2) Rotation. Deficiency eliminated. Done! a y py v b w c d x y d x py a v b c w
  • 33. Rr(2) Rotation. Deficiency eliminated. Done! a y py v b w c d x d y x py a v b c w

Editor's Notes

  • #2: Every red node has a black parent.
  • #4: Height = 5
  • #7: Collapsed height = 3.
  • #9: Color flip to be defined later. Binary search trees in which each node contains a binary search tree are used, for example, in IP router tables. Each rotation of the outer binary search tree requires changes in the binary search trees of the involved nodes resulting in an O(log n) cost per rotation.
  • #12: Initially, p is newly inserted node. Pp and gp may be null. In each round, p, pp, and gp are moved up by 2 levels. If p is a red root, make it black. If p or pp black done! If pp is a red root, make it black. O.w., gp exists
  • #18: If py doesn’t exist, the whole tree is one black node deficient. This is OK.
  • #24: y represents both a subtree and its root. This includes the case when y is null. Broken lines indicate pointers whose colors are unspecified. v must exist because y is one black node/pointer deficient.