0% found this document useful (0 votes)
0 views

12.Red-black_trees

Red-black trees are a type of binary search tree that maintain balance through specific properties regarding node colors and structure. They support operations like insertion and deletion in O(log n) time while ensuring the tree remains balanced. Key concepts include the height and black-height of nodes, as well as the use of rotations to maintain tree properties during modifications.

Uploaded by

Ravi Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

12.Red-black_trees

Red-black trees are a type of binary search tree that maintain balance through specific properties regarding node colors and structure. They support operations like insertion and deletion in O(log n) time while ensuring the tree remains balanced. Key concepts include the height and black-height of nodes, as well as the use of rotations to maintain tree properties during modifications.

Uploaded by

Ravi Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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 either 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, B-Tree
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Black-Height of a Node
h=4
26 bh = 2
h=1 h=3
17 41 bh = 2
bh = 1
h=2
30 h = 2
NIL NIL
47 bh = 1
bh = 1 h=1
bh = 1
NIL 38 NIL 50 h = 1
bh = 1
NIL NIL NIL NIL

• Height of a node: the number of edges in the longest path


to a leaf
• Black-height of a node x: bh(x) is the number of black nodes
(including NIL) on the path from x to a leaf, not counting x
• Fields of Red-balck-tree node: Left, right ,parent, color, key
Upper Bound on Height of a
Red-Black Tree
A red-black tree with n internal nodes
has height at most 2lg(n + 1)

• Claim 1:Any node x with height h(x) has


bh(x) ≥ h(x)/2
– By property 4, at most h/2 red nodes on the path from the
node to a leaf
– Hence at least h/2 are black
• Claim 2: The sub-tree rooted at any node
x contains at least 2bh(x) - 1 internal nodes
– Proof:
• By induction on h[x]
• Basis: h[x] = 0
– x is a leaf (NIL[T]) => bh(x) = 0
– # of internal nodes: 20 - 1 = 0
• Inductive Hypothesis: assume it is true for
h[x]=h-1
• Inductive step:
– Prove it for h[x]=h x
– Let bh(x) = b, then any child y of x has:
– bh (y) = b (if y is a red node) y1 y2 h-1
– bh (y) = b-1 (if y is a black node)
– Height of y is h-1
– As per inductive hypothesis y will have
at least 2bh(y)-1 internal nodes
– The sub-tree rooted at x contains at bh(y1)=bh(x)
least: ≥bh(x)-1
– (2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1
=2·(2bh(x) - 1 - 1) + 1 = 2bh(x) - 1 internal bh(y2)=bh(x)-1
nodes ≥bh(x)-1
A red-black tree with n internal nodes has
height at most 2lg(n + 1).
• Proof: Root=x
• n>= 2bh(x) - 1 (using claim 2)
y1 y2
>=2h(x)/2 -1 (using claim 1)
h(x)<= 2lg(n + 1).
Operations on Red-Black-Trees
• The non-modifying binary-search-tree operations
MINIMUM, MAXIMUM, SUCCESSOR,
PREDECESSOR, and SEARCH run in O(h) time
– They take O(lgn) time on red-black trees

• What about TREE-INSERT and TREE-DELETE?


– They will still run on O(lgn)

– We have to guarantee that the modified tree will still be a red-


black tree
INSERT
INSERT: what color to make the new node?
• Red?
– Property 4 might be violated: if a node is red,
then both its children are black
– Property 2 might be violated : root node is
always black
• Black?
– Property 5 might be violated: all paths from a
node to its leaves contain the same number of
black nodes
Rotations
• Rotation helps in re-structuring the tree
after insert and delete operations to
maintain red-black tree properties.
• There are two types of rotations
– Left rotation
– Right rotation
Left Rotations
• Assumptions for a left rotation on a node x:
– The right child of x (y) is not NIL

Right-Rotate(T,y)

• Idea:
– Pivots around the link from x to y
– Makes y the new root of the sub-tree
– x becomes y’s left child
– y’s left child becomes x’s right child
Example: LEFT-ROTATE
LEFT-ROTATE(T, x)
1. y ← right[x] ►Set y
2. right[x] ← left[y] ► y’s left sub-tree becomes x’s right sub-tree
3. if left[y]  NIL
4. then p[left[y]] ← x ► Set the parent relation from left[y] to x
5. p[y] ← p[x] ► The parent of x becomes the parent of y
6. if p[x] = NIL
7. then root[T] ← y
8. else if x = left[p[x]]
9. then left[p[x]] ← y
10. else right[p[x]] ← y
11. left[y] ← x ► Put x on y’s left
12. p[x] ← y ► y becomes x’s parent
Insertion
• Goal:
– Insert a new node z into a red-black-tree

• Idea:
– Insert node z into the tree as for an ordinary binary search tree
– Color the node red
– Restore the red-black-tree properties
• Use an auxiliary procedure RB-INSERT-FIXUP
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Properties violations
• Property 1 (each node black or red): hold
• Property 2: (root is black), not, if z is root
(and colored red).
• Proper 3: (each leaf is black sentinel): hold.
• Property 4: (the child of a red node must be
black), not, if z’s parent is red.
• Property 5: same number of blacks: hold
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.

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.

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]]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy