SlideShare a Scribd company logo
Red-Black tree
• Recall binary search tree
– Key values in the left subtree <= the node value
– Key values in the right subtree >= the node value
• Operations:
– insertion, deletion
– Search, maximum, minimum, successor,
predecessor.
– O(h), h is the height of the tree.
Red-black trees
• Definition: a binary tree, satisfying:
1. Every node is red or black
2. The root is black
3. Every leaf is NIL and is black
4. If a node is red, then both its children are black
5. For each node, all paths from the node to
descendant leaves contain the same number of
black nodes.
• Purpose: keep the tree balanced.
• Other balanced search tree:
– AVL tree, 2-3-4 tree, Splay tree, Treap
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fields and property
• Left, right, ,parent, color, key
• bh(x), black-height of x, the number of black
nodes on any path from x (excluding x) to a leaf.
• A red-black tree with n internal nodes has height
at most 2log(n+1).
– Note: internal nodes: all normal key-bearing nodes.
External nodes: Nil nodes or the Nil Sentinel.
– A subtree rooted at x contains at least 2bh(x)
-1 internal
nodes.
– By property 4, bh(root)≥h/2.
– n ≥ 2h/2
-1
Some operations in log(n)
• Search, minimum, maximum, successor,
predecessor.
• Let us discuss insert or delete.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Left rotation:
y=right[x]; right[x] left[y]; If(left[y]!=nil) p[left[y]]=x; p[y]=p[x]; if(p[x]==nil)
{root=y;} else if (left[p[x]]==x) left[p[x]]=y; else right[p[x]]=y;
left[y]=x; p[x]=y;
Right rotation:
x=left[y]; left[y]=right[x];
If(right[x]!=nil) p[right[x]]=y;
p[x]=p[y]; if(p[y]==nil) root=x;
If(left[p[y]]=y) left[p[y]]=x;
else right[p[y]]=x;
right[x]=y; p[y]=x;
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Properties violations
• Property 1 (each node black or red): hold
• Proper 3: (each leaf is black sentinel): hold.
• Property 5: same number of blacks: hold
• Property 2: (root is black), not, if z is root
(and colored red).
• Property 4: (the child of a red node must be
black), not, if z’s parent is red.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Case 1,2,3: p[z] is the left child of p[p[z]].
Correspondingly, there are 3 other cases,
In which p[z] is the right child of p[p[z]]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
case 1: z’s uncle is red.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
What is the running time of RB_INSERT_FIX? And RB_INSERT?
Case 2: z’s uncle is black and z is a right child. Case 3: z’s uncle is black and z is a left child
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

More Related Content

PPT
lecture 14
PPT
Red black tree
PPT
rbtrees.ppt
PPT
16 rbtrees
PDF
Red black trees
PPT
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
PPT
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
PPT
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
lecture 14
Red black tree
rbtrees.ppt
16 rbtrees
Red black trees
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN

Similar to operations insertion Red-black_treesver1.ppt (15)

PPTX
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
PPT
Red-black trees
PDF
Red black tree
PPTX
Red black trees
PDF
Red-Black Tree Presentation By Mobin Nesari.pdf
PPTX
Data structure
PPTX
Red black trees
PDF
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
PPT
Red Black Trees
PDF
Red black tree
PPT
Advanced data structures and implementation
PPTX
Balanced Tree(AVL Tree,Red Black Tree)
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PPTX
Red black trees1109
PPT
UNIT II.ppt
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
Red-black trees
Red black tree
Red black trees
Red-Black Tree Presentation By Mobin Nesari.pdf
Data structure
Red black trees
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
Red Black Trees
Red black tree
Advanced data structures and implementation
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
Red black trees1109
UNIT II.ppt
Ad

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Classroom Observation Tools for Teachers
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PDF
Business Ethics Teaching Materials for college
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Structure & Organelles in detailed.
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
Classroom Observation Tools for Teachers
RMMM.pdf make it easy to upload and study
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
Business Ethics Teaching Materials for college
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Structure & Organelles in detailed.
Ad

operations insertion Red-black_treesver1.ppt

  • 1. Red-Black tree • Recall binary search tree – Key values in the left subtree <= the node value – Key values in the right subtree >= the node value • Operations: – insertion, deletion – Search, maximum, minimum, successor, predecessor. – O(h), h is the height of the tree.
  • 2. Red-black trees • Definition: a binary tree, satisfying: 1. Every node is red or black 2. The root is black 3. Every leaf is NIL and is black 4. If a node is red, then both its children are black 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes. • Purpose: keep the tree balanced. • Other balanced search tree: – AVL tree, 2-3-4 tree, Splay tree, Treap
  • 3. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 4. Fields and property • Left, right, ,parent, color, key • bh(x), black-height of x, the number of black nodes on any path from x (excluding x) to a leaf. • A red-black tree with n internal nodes has height at most 2log(n+1). – Note: internal nodes: all normal key-bearing nodes. External nodes: Nil nodes or the Nil Sentinel. – A subtree rooted at x contains at least 2bh(x) -1 internal nodes. – By property 4, bh(root)≥h/2. – n ≥ 2h/2 -1
  • 5. Some operations in log(n) • Search, minimum, maximum, successor, predecessor. • Let us discuss insert or delete.
  • 6. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Left rotation: y=right[x]; right[x] left[y]; If(left[y]!=nil) p[left[y]]=x; p[y]=p[x]; if(p[x]==nil) {root=y;} else if (left[p[x]]==x) left[p[x]]=y; else right[p[x]]=y; left[y]=x; p[x]=y; Right rotation: x=left[y]; left[y]=right[x]; If(right[x]!=nil) p[right[x]]=y; p[x]=p[y]; if(p[y]==nil) root=x; If(left[p[y]]=y) left[p[y]]=x; else right[p[y]]=x; right[x]=y; p[y]=x;
  • 7. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 8. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 9. Properties violations • Property 1 (each node black or red): hold • Proper 3: (each leaf is black sentinel): hold. • Property 5: same number of blacks: hold • Property 2: (root is black), not, if z is root (and colored red). • Property 4: (the child of a red node must be black), not, if z’s parent is red.
  • 10. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Case 1,2,3: p[z] is the left child of p[p[z]]. Correspondingly, there are 3 other cases, In which p[z] is the right child of p[p[z]]
  • 11. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 12. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. case 1: z’s uncle is red.
  • 13. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. What is the running time of RB_INSERT_FIX? And RB_INSERT? Case 2: z’s uncle is black and z is a right child. Case 3: z’s uncle is black and z is a left child
  • 14. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 15. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 16. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.