Unit 4
Unit 4
UNIT - 4
Syllabus
• General Trees , Tree Terminologies
• Tree Representation , Tree Traversal
• Binary Tree Representation , Expression Trees
• Binary Tree Traversal , Threaded Binary Tree
• Binary Search Tree :Construction, Searching
Insertion and Deletion
• AVLTrees: Rotations , Insertions
• B-Trees Constructions , B-Trees Search
• B-Trees Deletions, Splay Trees
• Red Black Trees , Red Black Trees Insertion
General Trees
• Linear access time for linked lists too high.
• Solution – group data into trees.
• Trees – non linear data structure
• Used to represent data contains a
hierarchical relationship among elements
example: family, record
• Worst Case time – O(log n)
• Tree can be defined in many ways, eg:
recursively
General Trees
• Tree consists of collection of nodes
arranged in hierarchical pattern
Path A-G: A – B – D –
G
Path A-G: A – B – D – G
A – Ancestor for G, B, D…
G – Descendant of D,B,A
Binary Tree
• A binary tree is a data structure specified
as a set of so-called node elements. The
topmost element in a binary tree is called
the root node, and each node has 0, 1, or
at most 2 kids.
Similar binary tree
• Tree with same structure
Copies of Binary Tree
• Same structure and same data node
Complete Binary Tree
• Except last level, all the nodes need to
completely filled
• All nodes filled from left to right
Extended Binary Tree
• Extended binary tree is a type of binary
tree in which all the null sub tree of the
original tree are replaced with special
nodes called external nodes whereas
other nodes are called internal nodes
• Application - To convert binary tree in
Complete binary tree
Representation of Binary Tree
• Each node – three portion – Data portion,
left child pointer, Right child pointer
Linked List Representation of Tree
Struct NODE
{
Struct NODE *leftchild;
Int value;
Struct NODE *rightchild;
};
Expression Tree
• Used to store algebraic expression
• Example 1: exp = (a+b)*(b+c)
Expression Tree
• Used to store algebraic expression
• Example 1: exp = x+y/z*w-u
• Can be written as: exp = ((x+ ((y/z)*w)-u)
Binary Tree Traversal
• Traversal – visiting all node only once
• Based on the order of visiting :
– In – order traversal
– Pre – order traversal
– Post – order traversal
In-order Traversal
• Traverse left node , visit root node,
Traverse right node
Traverse Left node– B
No left child for B subtree
Visit root node B
No right child for B subtree
Visit root node A
B–A–C Traverse Right node– C
No left child for C subtree
Visit root node C
No right child for C subtree
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C, I,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C, I,
F,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C, I,
F,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C, I,
F, K,
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C, I,
F, K, J
In-order Traversal
Algorithm: Inorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Inorder(Tree->left)
3. Write(Tree->Data)
4. Inorder(Tree->right)
5. End
Result : G, D, H, L, B, E, A, C, I,
F, K, J
Pre – order Traversal
• Visit root node, Traverse left node ,
Traverse right node
Visit root node A
Traverse Left node– B
Visit root node B
No left child for B subtree
No right child for B subtree
A–B–C Traverse Right node– C
Visit root node C
No left child for C subtree
No right child for C subtree
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D,G,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C, F,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C, F,
I,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C, F,
I, J,
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C, F,
I, J, K
Pre-order Traversal
Algorithm: Preorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Write(Tree->Data)
3. Preorder(Tree->left)
4. Preorder(Tree->right)
5. End
Result: A, B, D, G, H, L, E, C, F,
I, J, K
Post– order Traversal
• Traverse left node, Traverse right node ,
visit root node
Traverse Left node – B
No left child for B subtree
No right child for B subtree
Visit root node B
Traverse Right node– C
B–C–A No left child for C subtree
No right child for C subtree
Visit root node C
Visit root node A
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I, K,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I, K, J,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I, K, J,
F,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I, K, J,
F, C,
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I, K, J,
F, C, A
Post-order Traversal
Algorithm: Postorder(Tree)
1. Repeat step 2 – 4 while Tree != Null
2. Postorder(Tree->left)
3. Postorder(Tree->right)
4. Write(Tree->Data)
5. End
Result : G, L, H, D, E, B, I, K, J,
F, C, A
Threaded Binary Trees
• Inorder traversal of a Binary tree can either
be done using recursion or with the use of a
auxiliary stack.
• The idea of threaded binary trees is to make
inorder traversal faster and do it without
stack and without recursion.
• A binary tree is made threaded by making
all right child pointers that would normally
be NULL point to the inorder successor of
the node (if it exists).
Threaded Binary Trees
• Nodes that does not have right child, have a
thread to their inorder successor.
• Node structure:
Struct NODE1
{
struct NODE1 *lchild;
int Node1_data;
Struct NODE1 *rchild;
Struct NODE1*trd;
}
Threaded Binary Trees
• Consider the following tree:
• Inorder traversal : D B A E C
Threaded Binary Trees
• Consider the following tree:
• Inorder traversal : D B A E C
http://btv.melezinek.cz/binary-
search-tree.html
BINARY SEARCH TREE
• Binary Search Tree is a binary tree in which every node
contains only smaller values in its left sub tree and
only larger values in its right sub tree.
• Also called as ORDERED binary tree
BST– properties:
• It should be Binary tree.
• Left subtree < Root Node < = Right subtree
(or)
Left subtree < =Root Node < Right subtree
Example:
Binary search trees or not ?
• Operations: Searching, Insertion, Deletion of a Node
• TimeComplexity :
BEST CASE WORST CASE
Step 2: END
EXAMPLE : Inserting nodes with values 55 in the given
binary search tree
AVL TREE
AVL TREE
• Named after Adelson-Velskii and Landis as AVL tree
• Also called as self-balancing binary search tree
AVL tree – properties:
• It should be Binary search tree
• Balancing factor: balance of every node is either -1 or 0 or
1
where balance(node) = height(node.left subtree) –
height(node.right subtree)
• Maximum possible number of nodes in AVL tree of height H
= 2H+1 – 1
• Operations: Searching, Insertion, Deletion of a Node
• TimeComplexity : O(log n)
Height of a Tree
Balancing Factor
• Balance factor = heightOfLeftSubtree –
heightOfRightSubtree
Example 1 : Check - AVL Tree?
Example 2: Check - AVL Tree?
6
4 8
1 5 7 11
2
operations on AVL tree
1.SEARCHING
2.INSERTION
3.DELETION
Search Operation in AVL Tree
ALGORITHM:
STEPS:
1 : Get the search element
2 : check search element == root node in the tree.
3 : If both are exact match, then display “element found" and
end.
4 : If both are not matched, then check whether search element
is < or > than that node value.
5: If search element is < then continue search in left sub tree.
6: If search element is > then continue search in right sub tree.
7: Repeat the step from 1 to 6 until we find the exact element
8: Still the search element is not found after reaching the leaf
node display “element not found”.
INSERTION or DELETION
• After performing any operation on AVL tree - the balance factor of each
node is to be checked.
• After insertion or deletion there exists either any one of the following:
Scenario 1:
• After insertion or deletion , the balance factor of each node is either 0
or 1 or -1.
• If so AVL tree is considered to be balanced.
• The operation ends.
Scenario 1:
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Searching in a B-Tree
• Similar to searching in a binary search tree.
• Consider the B-Tree shown here.
• If we wish to search for 72 in this tree, first consider
the root, the search key is greater than the values in
the root node. So go to the right sub-tree.
• The right sub tree consists of two values and again 72 is greater
than 63 so traverse to right sub tree of 63.
• The right sub tree consists of two values 72 and 81. So we found
our value 72.
Insertion in a B-Tree
• Insertions are performed at the leaf level.
• Search the B-Tree to find suitable place to
insert the new element.
• If the leaf node is not full, the new element can
be inserted in the leaf level.
• If the leaf is full,
– insert the new element in order into the existing
set of keys.
– split the node at its median into two nodes.
– push the median element up to its parent’s node.
If the parent’s node is already full, then split the
parent node by following the same steps.
Insertion - Example
• Consider the B-Tree of order 5
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Insertion - Example
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Exercise
• Consider the B-Tree of order 3, try to insert
121 and 87.
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Deletion - Example
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Deletion - Example
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Exercise
• Consider the B-Tree of order 3, try to
delete 36 and 109
Splay Trees
• Self-balancing BST with an additional property that
recently accessed elements can be re-accessed fast.
• All operations can be performed in O(log n) time.
• All operations can be performed by combining with
the basic operation called splaying.
• Splaying the tree for a particular node rearranges the
tree to place that node at the root.
• Each splay step depends on three factors:
• Whether N is the left or right child of its parent P,
• Whether P is the root or not, and if not,
• Whether P is the left or right child of its parent, G (N’s
grandparent).
Splay Trees
• Depending on these three factors, we have
one splay step based on each factor.
• Zig step
Splay Trees
• Zig – Zig Step
Splay Trees
• Zig – Zag Step
Red-Black Trees
• Self balancing binary search tree
• Also called as symmetric binary B-tree
• Although red-black tree is complex, all operations can be done in
a worst case time complexity of O(log n) where n is the number
of nodes in the tree.
• Properties of red-black trees
• A red-black tree is a BST which has the following properties
1. The color of a node is either red or black.
2. The color of the root node is always black.
3. All leaf nodes are black.
4. Every red node has both the children colored in black.
5. Every simple path from a given node to any of its leaf nodes has
an equal number of black nodes
Red-Black Tree - Example
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Exercise
• Say whether the following trees are red-
black or not.
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Red Black Trees - Insertion
• Insertion operation starts in the same way as we add new
node in the BST.
• The difference is, in BST the new node is added as a leaf node
whereas in red-black tree there is no data in the leaf node.
• So we add the new node as a red interior node which has two
black child nodes.
• When a new node is added, it may violate some properties of
the red-black tree.
• So in order to restore their property, we check for certain
cases and restore the property depending on the case that
turns up after insertion.
• Terminology
• Grandparent node (G) of node (N) - parent of N’s parent (P)
• Uncle node (U) of node (N) - sibling of N’s parent (P)
Red Black Trees - Insertion
• When we insert a new node in a red-black tree, note the following:
• All leaf nodes are always black. So property 3 always holds true.
• Property 4 (both children of every red node are black) is
threatened only by adding a red node, repainting a black node red,
or a rotation.
• Property 5 (all paths from any given node to its leaf nodes has
equal number of black nodes) is threatened only by adding a
black node, repainting a red node black, or a rotation.
• Case 1: The new node N is added as the root of the Tree
–In this case, N is repainted black, as the root should be black
always.
• Case 2: The new node’s parent P is black
–In this case, both children of every red node are black, so Property 4
is not invalidated. Property 5 is also not threatened.
Red Black Trees - Insertion
• Case 3: If Both the Parent (P) and the
Uncle (U) are Red
• In this case, Property 5 which says all
paths from any given node to its leaf
nodes have an equal number of black
nodes is violated.
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Red Black Trees - Insertion
• Case 4: The Parent P is Red but the Uncle
U is Black and N is the Right Child of P
and P is the Left Child of G
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
Red Black Trees - Insertion
• Case 5: The parent P is red but the uncle U
is black and the new node N is the left
child of P, and P is the left child of its
parent G.
Source : http://masterraghu.com/
subjects/Datastructures/ebooks/
rema%20thareja.pdf
References
1. Reema Thareja, Data Structures Using C, 1st
ed., Oxford Higher Education, 2011
2. Thomas H Cormen, Charles E Leiserson,
Ronald L Revest, Clifford Stein, Introduction to
Algorithms 3rd ed., The MIT Press Cambridge,
2014
3. Mark Allen Weiss, Data Structures and
Algorithm Analysis in C, 2nd ed., Pearson
Education, 2015
4.http://masterraghu.com/subjects/
Datastructures/ebooks/rema%20thareja.pdf