0% found this document useful (0 votes)
18 views5 pages

Red Black Tree 1

Uploaded by

097Shashank Sahu
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)
18 views5 pages

Red Black Tree 1

Uploaded by

097Shashank Sahu
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/ 5

KIET Group of Institutions, Ghaziabad

Department of Computer Applications


(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Design and Analysis of Algorithm
RCA 303: Session 2020-21

Unit-2 Lecture-17 Topic: Introduction to RB TREE


Video Link

Red-Black Trees

Do you know what balanced binary search tree is?

A "perfect balanced" binary search tree is basically a complete tree and we should also get a
complete tree after any operations like search, delete and insert etc. In this case, the height of
the tree will be O(lgn) and thus, these basics operations can be performed in O(lgn) worst-case
time as height of tree h is O(lgn).

But the thing is we can't rebuild the entire tree as a complete tree after inserting or deleting
any node because the building process will be very expensive and then there won't be any use
of inserting or deleting in O(lgn) time.
However, even if a tree is not perfectly balanced but pretty good balanced (little out of balance)
can have its height O(lgn) So, we can still perform basic operations in O(lgn) worst-case time.
And the benefit is that we can maintain that balance without reconstructing the entire tree.

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Design and Analysis of Algorithm
RCA 303: Session 2020-21

Red-black trees, AVL trees are an example of such trees which use some set of rules
which ensure that they are balanced. In both trees, the height of the tree is O(lgn) And we will
also see that maintaining those rules after insertion or deletion doesn't take much time which
will affect the worst-case running time of O(lgn) of these operations.

Properties of Red-Black Trees

A red-black tree ensures that its height is O(lgn) by following some properties, which are:
1. Every node is colored either red or black.
2. Root of the tree is black.
3. All leaves are black.
4. Both children of a red node are black i.e., there can't be consecutive red nodes.
5. All the simple paths from a node to descendant leave contain the same number of
black nodes.

Since all the leaves are black, we have used blank nodes or NIL for them as shown in the
above picture. We can also use only one node to represent all NIL with black color as shown
above. So instead of NULL, we are using an ordinary node to represent (NIL) to represent it.
This technique will save a lot of space for us.

Black Height of Red-Black Tree

Black height is an important term used with red-black trees. It is the number of black nodes on
any simple path from a node x (not including it) to a leaf.
Black height of any node x is represented by bh(x)
According to property 5, the number of black nodes from a node to any leaf is the same. Thus,
the black height of any node counted on any path to any leaf will be unique.

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Design and Analysis of Algorithm
RCA 303: Session 2020-21

Look at the picture given below with black height of nodes

Black height of the leaf (NIL) is 0 because we exclude the node for which we are counting the
black height. Root has a black height of 2 because there are 2 black nodes (excluding the root
itself) on a path from the root to leaf.
Let's look at one more picture of a red-black tree.

We have omitted the leaves while representing the above tree and we are going to follow the
same pattern in further discussion. You should keep in mind that there is a NIL node
representing the leaves in each example.
Prove that: A red-black tree with n internal nodes has height at most 2 lg(n + 1).
To prove the above statement, we need to prove the following statements first:
bh(x)
1. A subtree rooted at any node x has at least 2 – 1 internal nodes.
2. Any node x with height h(x) has bh(x) ≥ h(x)/2

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Design and Analysis of Algorithm
RCA 303: Session 2020-21

We are going to prove the first statement by the method of induction. The base case will be
when height of x is 0 i.e., x is a leaf. According to the statement, numbers of internal nodes
are 20−1=0 Since x is a leaf, this statement is true in the base case.
Now, consider a node x with two children l and r.

Let bh(x)=b. Now if the color of the child is red, then its black height will also be ‘b’. However, if
the color of the child is black, then its black height will be ‘b−1’.

b−1 bh(x)−1
According to the inductive hypothesis, child must have at least 2 −1 = 2 −1 internal
nodes. We have assumed that the inductive hypothesis is true for the child, now we need to
show that it is also true for the parent i.e., node x and hence completing the proof.
The node x must have at least 1 + least number of internal nodes that can be present on the
right child + least number of internal nodes that can be present on the left child. i.e.
(2bh(l) – 1) + (2bh(r) – 1)+ 1

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Design and Analysis of Algorithm
RCA 303: Session 2020-21

bh(x)−1
Internal nodes of x ≥ (2 – 1) +(2bh(x)−1 – 1 )+1
bh(x)−1)
or, Internal nodes of x ≥ 2 ∗ (2 ) -1
bh(x
or, Internal nodes of x ≥ 2 −1
We assumed it to be true for the child and it is also true for the node x, so the statement is
proved.
Let's prove the second statement.
Since the leaves are black and there can't be two consecutive red nodes, so the number of red
nodes can't exceed the number of black nodes on any simple path from a node to a leaf.
Therefore, we can also say that at least half of the nodes on any simple path from a root to a
leaf, not including the node, must be black.
It means that bh(x) ≥ h(x)/2 Thus, the second statement is also true.
According to the second statement, bh(root)≥h/2 where h is the height of the tree.
bh(root)
Using statement 1, n ≥ 2 -1
h/2
or, n ≥ 2 −1 (bh(root)≥h/2)
h/2
or, n+1 ≥ 2
Taking log on both sides,
lg(n+1) ≥ h/2
or, h ≤ 2lg(n+1) hence proved. it is also proved now Thus, the height of a red-black tree
is O(lgn)
we know that basic operations require O(h) time in a binary search tree and we have proved
that the height h is O(lgn). Thus, all operations can be done in O(lgn) time in a red-black tree.

Compiled by: Prashant Agrawal, Associate Professor

You might also like