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

Trees Graphs Pattern-1

Uploaded by

nityaghanathe7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Trees Graphs Pattern-1

Uploaded by

nityaghanathe7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

UNIT- IV TREES

UNIT-
IIIUNIT
IV
Properties of binary trees
Introduction TerminologyBinary tree
Representation of trees, representation
Binary trees abstract data Trees
type

Binary tree traversals: In order, preorder, post order


Binary search trees Definition
Operations:searching BST, insert into BST, delete from a BST, Height of a BST.

Trees: Non-Linear data structure

A data structure is said to be linear if its elements form a sequence or a linear list. Previous linear
data structures that we have studied like an array, stacks, queues and linked lists organize data in
linear order. A data structure is said to be non linear if its elements form a hierarchical classification
where, data items appear at various levels.

Trees and Graphs are widely used non-linear data structures. Tree and graph structures represent
hierarchical relationship between individual data elements. Graphs are nothing but trees with
certain restrictions removed.

Trees represent a special case of more general structures known as graphs. In a graph, there is no
restrictions on the number of links that can enter or leave a node, and cycles may be present in the
graph. The figure 5.1.1 shows a tree and a non-tree.

Tree is a popular data structure used in wide range of applications. A tree data structure can be
defined as follows...

Tree is a non-linear data structure which organizes data in hierarchical structure and this is a
recursive definition.

A tree data structure can also be defined as follows...


A tree is a finite set of one or more nodes such that:

1
UNIT- IV TREES

There is a specially designated node called the root. The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn are the
subtrees of the root.

A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the top of the
hierarchy. Each node can have at most one link coming into it. The node where the link
originates is called the parent node. The root node has no parent. The links leaving a node (any
number of links are allowed) point to child nodes. Trees are recursive structures. Each child
node is itself the root of a subtree. At the bottom of the tree are leaf nodes, which have no
children.

Advantages of trees

Trees are so useful and frequently used, because they have some very serious advantages:

∙ Trees reflect structural relationships in the data


∙ Trees are used to represent hierarchies
∙ Trees provide an efficient insertion and searching
∙ Trees are very flexible data, allowing to move sub trees around with minimum effort

Introduction Terminology

In a Tree, Every individual element is called as Node. Node in a tree data structure, stores the
actual data of that particular element and link to next element in hierarchical structure. Example

2
UNIT- IV TREES

1. Root

In a tree data structure, the first node is called as Root Node. Every tree must have root node. We
can say that root node is the origin of tree data structure. In any tree, there must be only one root
node. We never have multiple root nodes in a tree. In above tree, A is a Root node

2. Edge

In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree with
'N' number of nodes there will be a maximum of 'N-1' number of edges.

3. Parent

In a tree data structure, the node which is predecessor of any node is called as PARENT NODE. In
simple words, the node which has branch from it to any other node is called as parent node. Parent
node can also be defined as "The node which has child / children". e.g., Parent (A,B,C,D).

4. Child

In a tree data structure, the node which is descendant of any node is called as CHILD Node. In
simple words, the node which has a link from its parent node is called as child node. In a tree, any
parent node can have any number of child nodes. In a tree, all the nodes except root are child
nodes. e.g., Children of D are (H, I,J).

5. Siblings

In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes. Ex: Siblings (B,C, D)

6. Leaf

In a tree data structure, the node which does not have a child (or) node with degree zero is called
as LEAF Node. In simple words, a leaf is a node with no child.

3
UNIT- IV TREES

In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a node
with no child. In a tree, leaf node is also called as 'Terminal' node. Ex: (K,L,F,G,M,I,J)

7. Internal Nodes

In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In simple
words, an internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node is
also said to be Internal Node if the tree has more than one node. Internal nodes are also called as
'Non-Terminal' nodes. Ex:B,C,D,E,H

8. Degree

In a tree data structure, the total number of children of a node (or)number of subtrees of a node is
called as DEGREE of that Node. In simple words, the Degree of a node is total number of children it
has. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'

9. Level

In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In simple
words, in a tree each step from top to bottom is called as a Level and the Level count starts with '0'
and incremented by one at each level (Step). Some authors start root level with 1.

10. Height

In a tree data structure, the total number of edges from leaf node to a particular node in the longest
path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of the
tree. In a tree, height of all leaf nodes is '0'.

11. Depth

In a tree data structure, the total number of edges from root node to a particular node is called as
DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the
longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node in a
tree is said to be depth of that tree. In a tree, depth of the root node is '0'.

4
UNIT- IV TREES 12. Path

In a tree data structure, the sequence of Nodes and Edges from one node to another node is called
as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
13. Sub Tree
In a tree data structure, each child from a node forms a subtree recursively. Every child node will
form a subtree on its parent node.

Tree Representations
A tree data structure can be represented in two methods. Those methods are as follows...

1.List Representation

2. Left Child - Right Sibling Representation

Consider the following tree...

5
UNIT- IV TREES

1. List Representation

In this representation, we use two types of nodes one for representing the node with data and another
for representing only references. We start with a node with data from root node in the tree. Then it is
linked to an internal node through a reference node and is linked to any other node directly. This
process repeats for all the nodes in the tree.

The above tree example can be represented using List representation as follows...
Fig: List representation of above Tree

Fig: Possible node structure for a tree of degree k

2. Left Child - Right Sibling Representation

In this representation, we use list with one type of node which consists of three fields namely Data
field, Left child reference field and Right sibling reference field. Data field stores the actual value of
a node, left reference field stores the address of the left child and right reference field stores the
address of the right sibling node. Graphical representation of that node is as follows...

In this representation, every node's data field stores the actual value of that node. If that node has left
child, then left reference field stores the address of that left child node otherwise that field stores
NULL. If that node has right sibling then right reference field stores the address of right sibling node
otherwise that field stores NULL.

The above tree example can be represented using Left Child - Right Sibling representation as
follows...

UNIT- IV TREES

Representation as a Degree –Two Tree

To obtain degree-two tree representation of a tree, rotate the right- sibling pointers in the left child
right sibling tree clockwise by 45 degrees. In a degree-two representation, the two children of anode
are referred as left and right children.
Binary Trees

Introduction

In a normal tree, every node can have any number of children. Binary tree is a special type of tree
data structure in which every node can have a maximum of 2 children. One is known as left child
and the other is known as right child.

A tree in which every node can have a maximum of two children is called as Binary Tree.

In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2
children. Example

There are different types of binary trees and they are...

7
UNIT- IV TREES

1. Strictly Binary Tree

In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none. That means every internal node must have exactly
two children. A strictly Binary Tree can be defined as follows...

A binary tree in which every node has either two or zero number of children is called Strictly Binary
Tree. Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree

2. Complete Binary Tree

In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none and in complete binary tree all the nodes must have
exactly two children and at every level of complete binary tree there must be 2 level number of
nodes. For example at level 2 there must be 2^2 = 4 nodes and at level 3 there must be 2^3 = 8
nodes.
A binary tree in which every internal node has exactly two children and all leaf nodes are at same
level is called Complete Binary Tree.

Complete binary tree is also called as Perfect Binary Tree

3. Extended Binary Tree


A binary tree can be converted into Full Binary tree by adding dummy nodes to existing nodes
wherever required.

The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary
Tree.

Abstract Data Type

8
UNIT- IV TREES

Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two
disjoint binary trees called left subtree and right subtree.

ADT contains specification for the binary tree ADT.

Structure Binary_Tree(abbreviated BinTree) is

objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right
Binary_Tree.

Functions:

for all bt, bt1, bt2 ∈ BinTree, item ∈ element

Bintree Create()::= creates an empty binary tree

Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE

BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right
subtree is bt2, and whose root node contains the data item
Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt

element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt
Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt
Samples of Trees
12 A
B A Complete
A Binary Tree
B
C BCF
Skewed Binary Tree 3
D H
D
4 EGI
E 5
CHAPTER 5 10

Differences between A Tree and A Binary Tree

• The subtrees of a binary tree are ordered; those of a tree are not ordered.

UNIT- IV TREES

Above two trees are different when viewed as binary trees. But same when viewed as trees.

Properties of Binary Trees

1.Maximum Number of Nodes in BT

∙ The maximum number of nodes on level i of a binary tree is 2i-1, i>=1.


∙ The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1.

Proof By Induction:

Induction Base: The root is the only node on level i=1.Hence ,the maximum number of nodes on
level i=1 is 2i-1=20=1.

Induction Hypothesis: Let I be an arbitrary positive integer greater than 1.Assume that maximum
number of nodes on level i-1 is 2i-2.
Induction Step: The maximum number of nodes on level i-1 is 2i-2by the induction hypothesis. Since
each node in a binary tree has a maximum degree of 2,the maximum number of nodes on level i is
two times the maximum number of nodes on level i-1,or 2i-1.
k

1
∑ =−
i
−k
=
The maximum number of nodes in a
2211
binary tree of depth k is i
2.Relation between number of leaf nodes and degree-2 nodes: For any nonempty binary tree, T, if
n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0=n2+1.

PROOF: Let n and B denote the total number of nodes and branches in T. Let n0, n1, n2
represent the nodes with zero children, single child, and two children respectively.

B+1=n B=n1+2n2 ==> n1+2n2+1= n,

n1+2n2+1= n0+n1+n2 ==> n0=n2+1

3. A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0.

A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes
numbered from 1 to n in the full binary tree of depth k.

Binary Tree Representation

A binary tree data structure is represented using two methods. Those methods are 1)Array
Representation 2)Linked List Representation

10
UNIT- IV TREES

1)Array Representation: In array representation of binary tree, we use a one dimensional array (1-D
Array) to represent a binary tree. To represent a binary tree of depth 'n' using array representation,
we need one dimensional array with a maximum size of

A complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for
any node with index i, 1<=i<=n, we have: a) parent(i) is at i/2 if i!=1. If i=1, i is at the root and
2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child.
has no parent. b)left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no left child. c) right_child(i) is at
Disadvantages:(1) waste of space [1] [2] [3] [4]
[1] ABCD
(2) insertion/deletion problem
A
C [8] [9] . D -- . BCF E
[2] [3] [4]
D [5] [6] [7] F
A [5] [6] [7] B -- C -- -- -- GHI
A [8] [9]
B
E D
[16] E EG
CHAPTER 5 13

H
I
2. Linked Representation

We use linked list to represent a binary tree. In a linked list, every node consists of three fields. First
field, for storing left child address, second for storing actual data and third for storing right child
address. In this linked list representation, a node has the following structure...

typedef
struct node *tree_pointer;
typedef struct node {

int data;

tree_pointer left_child, right_child;};

11

UNIT- IV TREES

Binary Tree Traversals

When we wanted to display a binary tree, we need to follow some order in which all the nodes of
that binary tree must be displayed. In any binary tree displaying order of nodes depends on the
traversal method. Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree
Traversal.

There are three types of binary tree traversals.


1)In - Order Traversal 2)Pre - Order Traversal 3)Post - Order Traversal Binary
Tree Traversals
• 1. In - Order Traversal ( leftChild - root - rightChild )
•I-D-J-B-F-A-G-K-C–H
• 2. Pre - Order Traversal ( root - leftChild - rightChild )
•A-B-D-I-J-F-C-G-K–H
• 3. Post - Order Traversal ( leftChild - rightChild - root )
•I-J-D-F-B-K-G-H-C–A

CHAPTER 5 14

1. In - Order Traversal ( leftChild - root - rightChild )

In In-Order traversal, the root node is visited between left child and right child. In this traversal, the
left child node is visited first, then the root node is visited and later we go for visiting right child
node. This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.

In the above example of binary tree, first we try to visit left child of root node 'A', but A's left child is
a root node for left subtree. so we try to visit its (B's) left child 'D' and again D is a root for subtree
with nodes D, I and J. So we try to visit its left child 'I' and it is the left most child. So first we

12
UNIT- IV TREES

visit 'I'then go for its root node 'D' and later we visit D's right child 'J'. With this we have completed
the left part of node B. Then visit 'B' and next B's right child 'F' is visited. With this we have
completed left part of node A. Then visit root node 'A'. With this we have completed left and root
parts of node A. Then we go for right part of the node A. In right of A again there is a subtree with
root C. So go for left child of C and again it is a subtree with root G. But G does not have left part so
we visit 'G' and then visit G's right child K. With this we have completed the left part of node C.
Then visit root node'C' and next visit C's right child 'H' which is the right most child in the tree so we
stop the process.

That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-Order


Traversal.

In-Order Traversal for above example of binary tree is

I-D-J-B-F-A-G-K-C–H

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.

Step 2 − Visit root node.

Step 3 − Recursively traverse right subtree.

void inorder(tree_pointer ptr) /* inorder tree traversal */ Recursive {


if (ptr) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
}
2. Pre - Order Traversal ( root - leftChild - rightChild )
In Pre-Order traversal, the root node is visited before left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order
traversal is applicable for every root node of all subtrees in the tree.

In the above example of binary tree, first we visit root node 'A' then visit its left child 'B' which is a
root for D and F. So we visit B's left child 'D' and again D is a root for I and J. So we visit D's left
child'I' which is the left most child. So next we go for visiting D's right child 'J'. With this we have
completed root, left and right parts of node D and root, left parts of node B. Next visit B's right
child'F'. With this we have completed root and left parts of node A. So we go for A's right child 'C'
which is a root node for G and H. After visiting C, we go for its left child 'G' which is a root

13
UNIT- IV TREES

for node K. So next we visit left of G, but it does not have left child so we go for G's right child 'K'.
With this we have completed node C's root and left parts. Next visit C's right child 'H' which is the
right most child in the tree. So we stop the process. That means here we have visited in the order of
A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal.

Algorithm

Until all nodes are traversed −

Step 1 − Visit root node.

Step 2 − Recursively traverse left subtree.

Step 3 − Recursively traverse right subtree.

void preorder(tree_pointer ptr) /* preorder tree traversal */ Recursive {


if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left_child);
preorder(ptr->right_child);
}
}
3. Post - Order Traversal ( leftChild - rightChild - root )
In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left
child node is visited first, then its right child and then its root node. This is recursively performed
until the right most node is visited.
Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-Order Traversal.

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
void postorder(tree_pointer ptr) /* postorder tree traversal */ Recursive {
if (ptr) {
postorder(ptr->left_child);
postorder(ptr->right_child);
printf(“%d”, ptr->data);
}
}

14
UNIT- IV TREES

Arithmetic Expression Using BT


* inorder traversal postorder traversal
/ A/B*C*D+E AB/C*D*E+
B infix expression postfix expression
+ E level order
* preorder traversal traversal
D +**/ABCDE +*E*D/CAB
A C prefix expression
CHAPTER 5 15

Trace Operations of Inorder Traversal


Call of inorder Value in root Action Call of inorder Value in root Action
1 + 11 C
2 * 12 NULL
3 * 11 C printf
4 / 13 NULL
5 A 2 * printf
6 NULL 14 D
5 A printf 15 NULL
7 NULL 14 D printf
4 / printf 16 NULL
8 B 1 + printf
9 NULL 17 E
8 B printf 18 NULL
10 NULL 17 E printf
3 * printf 19 NULL
CHAPTER 5 16

Iterative Inorder Traversal (using stack)


void iter_inorder(tree_pointer node)
{
int top= -1; /* initialize stack */
tree_pointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->left_child)
add(&top, node); /* add to stack */
node= delete(&top); /* delete from stack */
if (!node) break; /* empty stack */
printf(“%D”, node->data);
node= node->right_child;
}
}
In iterative inorder traversal, we must create our own stack to add and remove nodes as in recursion.
Analysis: Let n be number of nodes in tree, every node of tree is placed on and removed from the
stack exactly once. So time complexity is O(n). The space requirement is equal to the depth of tree
which is O(n).
15
UNIT- IV TREES

Level Order Traversal ( Using Queue) --Traversal without Stack


void level_order(tree_pointer ptr) /* level order tree traversal */
{
int front = rear = 0;
tree_pointer queue[MAX_QUEUE_SIZE];
if (!ptr) return; /* empty queue */
addq(front, &rear, ptr);
for (;;) {
ptr = deleteq(&front, rear);
if (ptr) {
printf(“%d”, ptr->data);
if (ptr->left_child)
addq(front, &rear, ptr->left_child);
if (ptr->right_child)
addq(front, &rear, ptr->right_child);
}
else break;
}}
Level order Traversal is implemented with circular queue. In this order, we visit the root first, then
root’s left child followed by root’s right child. We continue in this manner, visiting the nodes at each
new level from left most to right most nodes.
We begin by adding root to the queue. The function operates by deleting the node at the front of the
queue, printing out the node’s data field, and adding the node’s left and right children to the queue.
The level order traversal for above arithmetic expression is + * E * D / C A B.

Binary Search Trees

Binary Search Tree Representation


Binary Search tree exhibits a special behavior. A node's left child must have value less than its
parent's value and node's right child must have value greater than it's parent value.

16
UNIT- IV TREES

We're going to implement tree using node object and connecting them through references.

Definition: A binary search tree (BST) is a binary tree. It may be empty. If it is not empty,then all
nodes follows the below mentioned properties −

∙ Every element has a unique key.


∙ The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root
of subtree.
∙ The keys in a nonempty right subtree larger than the key in the root of subtree. ∙
The left and right subtrees are also binary search trees.

left sub-tree and right sub-tree and can be defined as −

left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)

Fig: Example Binary Search Trees


ADT for Dictionary:

BST Basic Operations


The basic operations that can be performed on binary search tree data structure, are following
− ∙ Search − search an element in a binary search tree.

∙ Insert − insert an element into a binary search tree / create a tree.

∙ Delete − Delete an element from a binary search tree.

∙ Height -- Height of a binary search tree.

Searching a Binary Search Tree


Let an element k is to search in binary search tree. Start search from root node of the search tree. If
root is NULL, search tree contains no nodes and search unsuccessful. Otherwise, compare k with the
key in the root. If k equals the root’s key, terminate search, if k is less than key value, search

17
UNIT- IV TREES

element k in left subtree otherwise search element k in right subtree. The function search recursively
searches the subtrees.

Algorithm:Recursive search of a Binary Search Tree

tree_pointer search(tree_pointer root, int key)


{
/* return a pointer to the node that contains key. If there is no such node, return NULL */
if (!root) return NULL;
if (key == root->data) return root;
if (key < root->data)
return search(root->left_child, key);
return search(root->right_child,key);
}

Algorithm: Iteraive search of a Binary Search Tree

tree_pointer search2(tree_pointer tree, int key)


{
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->left_child;
else tree = tree->right_child;
}
return NULL;
}
Analysis of Recursive search and Iterative Search Algorithms:
If h is the height of the binary search tree, both algorithms perform search in O(h) time.
Recursive search requires additional stack space which is O(h).
Inserting into a Binary Search Tree
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted. First
locate its proper location. Start search from root node then if data is less than key value, search
empty location in left sub tree and insert the data. Otherwise search empty location in right sub tree
and insert the data.

18
UNIT- IV TREES

In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary
search tree, new node is always inserted as a leaf node. The insertion operation is performed as
follows...

Step 1: Create a newNode with given value and set its left and right to

NULL. Step 2: Check whether tree is Empty.

Step 3: If the tree is Empty, then set set root to newNode.

Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than the
node (here it is root node).

Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode
is larger than the node, then move to its right child.

Step 6: Repeat the above step until we reach a node (e.i., reach to NULL) where search terminates.

Step 7: After reaching a last node, then insert the newNode as left child if newNode is smaller or
equal to that node else insert it as right child.

Algorithm

Create newnode

If root is NULL
then create root node

return

If root exists then

compare the data with node.data

while until insertion position is located

If data is greater than node.data

19
UNIT- IV TREES goto right subtree

else

goto left subtree

endwhile

insert newnode

end If

Implementation
The implementation of insert function should look like this −
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty, create root node
if(root == NULL) {
root = tempNode;
}else {
current = root;
parent = NULL;
while(1) {
parent = current;
//go to left of the tree
if(data < parent->data) {
current = current->leftChild;

//insert to the left


if(current == NULL) {
parent->leftChild = tempNode;

20
UNIT- IV TREES
return; }
}

//go to right of the tree


else {
current = current->rightChild;
//insert to the right
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}}
}
}

Deleting a node
Remove operation on binary search tree is more complicated, than insert and search. Basically, in
can be divided into two stages:

∙ search for a node to remove

∙ if the node is found, run remove algorithm.

Remove algorithm in detail

Now, let's see more detailed description of a remove algorithm. First stage is identical to algorithm
for lookup, except we should track the parent of the current node. Second part is more tricky. There
are three cases, which are described below.
1.Node to be removed has no children. --This case is quite simple. Algorithm sets corresponding
link of the parent to NULL and disposes the node.

Example. Remove -4 from a BST.

21
UNIT- IV TREES
2.Node to be removed has one child. In this case, node is cut from the tree and algorithm links single
child (with it's subtree) directly to the parent of the removed node.

3.Node to be removed has two children. --This is the most complex case. The deleted node can be
replaced by either largest key in its left subtree or the smallest in its right subtree. Preferably which
node has one child.

Deletion Operation in BST

In a binary search tree, the deletion operation is performed with O(log n) time complexity. Deleting
a node from Binary search tree has following three cases...

Case 1: Deleting a Leaf node (A node with no children)

Case 2: Deleting a node with one child

22
UNIT- IV TREES

Case 3: Deleting a node with two children

Case 1: Deleting a leaf node


We use the following steps to delete a leaf node from BST...

Step 1: Find the node to be deleted using search operation

Step 2: Delete the node using free function (If it is a leaf) and terminate the

function. Case 2: Deleting a node with one child

We use the following steps to delete a node with one child from BST...

Step 1: Find the node to be deleted using search operation

Step 2: If it has only one child, then create a link between its parent and child

nodes. Step 3: Delete the node using free function and terminate the function.

Case 3: Deleting a node with two children

We use the following steps to delete a node with two children from BST...

Step 1: Find the node to be deleted using search operation

Step 2: If it has two children, then find the largest node in its left subtree (OR) the smallest node in
its right subtree.

Step 3: Swap both deleting node and node which found in above step.

Step 4: Then, check whether deleting node came to case 1 or case 2 else goto steps

2 Step 5: If it comes to case 1, then delete using case 1 logic.

Step 6: If it comes to case 2, then delete using case 2 logic.

Step 7: Repeat the same process until node is deleted from the tree.

/* deletion in binary search tree */


void deletion(struct treeNode **node, struct treeNode **parent, int data) {
struct treeNode *tmpNode, *tmpParent;
if (*node == NULL)
return;
if ((*node)->data == data) {
/* deleting the leaf node */
if (!(*node)->left && !(*node)->right) {
if (parent) {
/* delete leaf node */

23
UNIT- IV TREES

if ((*parent)->left == *node)
(*parent)->left = NULL;
else
(*parent)->right = NULL;
free(*node);
} else {
/* delete root node with no children */
free(*node);
}
/* deleting node with one child */
} else if (!(*node)->right && (*node)->left)
{ /* deleting node with left child alone
*/ tmpNode = *node;
(*parent)->right = (*node)->left;
free(tmpNode);
*node = (*parent)->right;
} else if ((*node)->right && !(*node)->left)
{ /* deleting node with right child alone
*/ tmpNode = *node;
(*parent)->left = (*node)->right;
free(tmpNode);
(*node) = (*parent)->left;
} else if (!(*node)->right->left) {
/*
* deleting a node whose right child
* is the smallest node in the right
* subtree for the node to be deleted.
*/

tmpNode = *node;

(*node)->right->left = (*node)->left;

(*parent)->left = (*node)->right;
free(tmpNode);
*node = (*parent)->left;
} else {
/*
* Deleting a node with two
children. * First, find the smallest
node in
* the right subtree. Replace the
* smallest node with the node to be
* deleted. Then, do proper
connections * for the children of
replaced node.
*/
tmpNode = (*node)->right;
while (tmpNode->left) {

24
UNIT- IV TREES

tmpParent = tmpNode;
tmpNode = tmpNode->left;
}
tmpParent->left = tmpNode->right;
tmpNode->left = (*node)->left;
tmpNode->right =(*node)->right;
free(*node);
*node = tmpNode;
}
} else if (data < (*node)->data) {
/* traverse towards left subtree */
deletion(&(*node)->left, node, data);
} else if (data > (*node)->data) {
/* traversing towards right subtree */
deletion(&(*node)->right, node, data);
}
}
Height of a Binary Search Tree:

Height of a Binary Tree For a tree with just one node, the root node, the height is defined to be 0, if
there are 2 levels of nodes the height is 1 and so on. A null tree (no nodes except the null node) is
defined to have a height of –1.

The following height function in pseudocode is defined recursively

25
UNIT- IV TREES
Example

Construct a Binary Search Tree by inserting the following sequence of numbers...

10,12,5,4,20,8,7,15 and 13

Above elements are inserted into a Binary Search Tree as follows...

26
UNIT- IV TREES
27

AVL Tree Data structure


AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a

binary search tree but it is a balanced tree. A binary tree is said to be balanced if, the
difference between the heights of left and right subtrees of every node in the tree is
either -1, 0 or +1. In other words, a binary tree is said to be balanced if the height of
left and right children of every node differ by either -1, 0 or +1. In an AVL tree, every
node maintains an extra information known as balance factor. The AVL tree was
introduced in the year 1962 by G.M. Adelson-Velsky and E.M. Landis.

An AVL tree is defined as follows...


An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of
every node is either -1, 0 or +1.

Balance factor of a node is the difference between the heights of the left and right subtrees of that node. The balance
factor of a node is calculated either height of left subtree - height of right subtree (OR) height of
right subtree - height of left subtree. In the following explanation, we calculate as follows...
Balance factor = height Of Left Subtree - height Of Right Subtree

Example of AVL Tree

The above tree is a binary search tree and every node is satisfying balance
factor condition. So this tree is said to be an AVL tree.

Every AVL Tree is a binary search tree but every Binary Search Tree need not
be AVL tree.

AVL Tree Rotations

In AVL tree, after performing operations like insertion and deletion we need
to check the balance factor of every node in the tree. If every node satisfies
the balance factor condition then we conclude the operation otherwise we
must make it balanced. Whenever the tree becomes imbalanced due to any
operation we use rotation operations to make the tree balanced.
Rotation operations are used to make the tree balanced.
Rotation is the process of moving nodes either to left or to right to make
the tree balanced.
Operations on an AVL Tree
The following operations are performed on AVL tree...

1. Search
2. Insertion
3. Deletion

Search Operation in AVL Tree


In an AVL tree, the search operation is performed with O(log n) time complexity. The search operation in the AVL
tree is similar to the search operation in a Binary search tree. We use the following steps to search an element in AVL
tree...

∙ Step 1 - Read the search element from the user.


∙ Step 2 - Compare the search element with the value of root node in the tree. ∙ Step
3 - If both are matched, then display "Given node is found!!!" and terminate the
function
∙ Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
∙ Step 5 - If search element is smaller, then continue the search process in left
subtree.
∙ Step 6 - If search element is larger, then continue the search process in right
subtree.
∙ Step 7 - Repeat the same until we find the exact element or until the search
element is compared with the leaf node.
∙ Step 8 - If we reach to the node having the value equal to the search value, then
display "Element is found" and terminate the function.
∙ Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.

Insertion Operation in AVL Tree


In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a new node is
always inserted as a leaf node. The insertion operation is performed as follows...

∙ Step 1 - Insert the new element into the tree using Binary Search Tree insertion
logic.
∙ Step 2 - After insertion, check the Balance Factor of every node. ∙ Step 3 - If the
Balance Factor of every node is 0 or 1 or -1 then go for next operation.
∙ Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform suitable Rotation to make it
balanced and go for next operation.

Example: Construct an AVL Tree by inserting numbers from


1 to 8.
Splay Tree Data structure
Splay tree is another variant of a binary search tree. In a splay tree, recently accessed element is

placed at the root of the tree. A splay tree is defined as follows...

Splay Tree is a self - adjusted Binary Search Tree in which every operation on

element rearranges the tree so that the element is placed at the root position of the

tree.

In a splay tree, every operation is performed at the root of the tree. All the operations in splay

tree are involved with a common operation called "Splaying".

Splaying an element, is the process of bringing it to the root position by

performing suitable rotation operations.

In a splay tree, splaying an element rearranges all the elements in the tree so that splayed

element is placed at the root of the tree.

By splaying elements we bring more frequently used elements closer to the root of the tree so

that any operation on those elements is performed quickly. That means the splaying operation
automatically brings more frequently used elements closer to the root of the tree.

Every operation on splay tree performs the splaying operation. For example, the insertion

operation first inserts the new element using the binary search tree insertion process, then the

newly inserted element is splayed so that it is placed at the root of the tree. The search

operation in a splay tree is nothing but searching the element using binary search process and

then splaying that searched element so that it is placed at the root of the tree.

In splay tree, to splay any element we use the following rotation operations...
Rotations in Splay Tree

∙ 1. Zig Rotation

∙ 2. Zag Rotation

∙ 3. Zig - Zig Rotation

∙ 4. Zag - Zag Rotation

∙ 5. Zig - Zag Rotation

∙ 6. Zag - Zig Rotation

Example

Zig Rotation

The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree rotations. In zig

rotation, every node moves one position to the right from its current position. Consider the

following example...

Zag Rotation

The Zag Rotation in splay tree is similar to the single left rotation in AVL Tree rotations. In zag

rotation, every node moves one position to the left from its current position. Consider the

following
example...

Zig-Zig Rotation

The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation, every node moves

two positions to the right from its current position. Consider the following
example...

Zag-Zag Rotation

The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation, every node

moves two positions to the left from its current position. Consider the following

example...

Zig-Zag Rotation

The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed by zag rotation. In zig

zag rotation, every node moves one position to the right followed by one position to the left from

its current position. Consider the following example…


Zag-Zig Rotation

The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig rotation. In zag

zig rotation, every node moves one position to the left followed by one position to the right from

its current position. Consider the following example…

Every Splay tree must be a binary search tree but it is need not to be balanced tree.

Insertion Operation in Splay Tree

The insertion operation in Splay tree is performed using following steps...

∙ Step 1 - Check whether tree is Empty.

∙ Step 2 - If tree is Empty then insert the newNode as Root node and exit from the operation.

∙ Step 3 - If tree is not Empty then insert the newNode as leaf node using Binary Search tree

insertion logic.

∙ Step 4 - After insertion, Splay the newNode

Deletion Operation in Splay Tree


The deletion operation in splay tree is similar to deletion operation in Binary Search Tree. But

before deleting the element, we first need to splay that element and then delete it from the root

position. Finally join the remaining tree using binary search tree logic.

Binary Search Tree


In a binary tree, every node can have a maximum of two children but there is no need to

maintain the order of nodes basing on their values. In a binary tree, the elements are
arranged in the order they arrive at the tree from top to bottom and left to right.

A binary tree has the following time complexities...

1. Search Operation - O(n)


2. Insertion Operation - O(1)
3. Deletion Operation - O(n)

To enhance the performance of binary tree, we use a special type of binary tree known
as Binary Search Tree. Binary search tree mainly focuses on the search operation in a
binary tree. Binary search tree can be defined as follows...

Binary Search Tree is a binary tree in which every node contains only smaller values
in its left subtree and only larger values in its right subtree.

In a binary search tree, all the nodes in the left subtree of any node contains smaller
values and all the nodes in the right subtree of any node contains larger values as
shown in the following figure…

Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains nodes with smaller values
and right subtree of every node contains larger values.
Operations on a Binary Search Tree
The following operations are performed on a binary search tree...

1. Search
2. Insertion
3. Deletion

Search Operation in BST


In a binary search tree, the search operation is performed with O(log n) time complexity. The search operation is
performed as follows...

∙ Step 1 - Read the search element from the user.


∙ Step 2 - Compare the search element with the value of root node in the tree. ∙ Step
3 - If both are matched, then display "Given node is found!!!" and terminate the
function
∙ Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
∙ Step 5 - If search element is smaller, then continue the search process in left
subtree.
∙ Step 6- If search element is larger, then continue the search process in right
subtree.
∙ Step 7 - Repeat the same until we find the exact element or until the search
element is compared with the leaf node
∙ Step 8 - If we reach to the node having the value equal to the search value then
display "Element is found" and terminate the function.
∙ Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.

Insertion Operation in BST


In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary search tree,
new node is always inserted as a leaf node. The insertion operation is performed as follows...

∙ Step 1 - Create a newNode with given value and set its left and right to NULL. ∙
Step 2 - Check whether tree is Empty.
∙ Step 3 - If the tree is Empty, then set root to newNode.
∙ Step 4 - If the tree is Not Empty, then check whether the value of newNode is
smaller or larger than the node (here it is root node).
∙ Step 5 - If newNode is smaller than or equal to the node then move to its left child.
If newNode is larger than the node then move to its right child. ∙ Step 6- Repeat the
above steps until we reach to the leaf node (i.e., reaches to NULL).
∙ Step 7 - After reaching the leaf node, insert the newNode as left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.

Deletion Operation in BST


In a binary search tree, the deletion operation is performed with O(log n) time complexity. Deleting a node from
Binary search tree includes following three cases...

∙ Case 1: Deleting a Leaf node (A node with no children)


∙ Case 2: Deleting a node with one child
∙ Case 3: Deleting a node with two children

Case 1: Deleting a leaf node


We use the following steps to delete a leaf node from BST...

∙ Step 1 - Find the node to be deleted using search operation


∙ Step 2 - Delete the node using free function (If it is a leaf) and terminate the
function.

Case 2: Deleting a node with one child

We use the following steps to delete a node with one child from BST...

∙ Step 1 - Find the node to be deleted using search operation


∙ Step 2 - If it has only one child then create a link between its parent node and
child node.
∙ Step 3 - Delete the node using free function and terminate the function.

Case 3: Deleting a node with two children

We use the following steps to delete a node with two children from BST...
∙ Step 1 - Find the node to be deleted using search operation
∙ Step 2 - If it has two children, then find the largest node in its left subtree (OR)
the smallest node in its right subtree.
∙ Step 3 - Swap both deleting node and node which is found in the above step. ∙
Step 4 - Then check whether deleting node came to case 1 or case 2 or else
goto step 2
∙ Step 5 - If it comes to case 1, then delete using case 1 logic. ∙
Step 6- If it comes to case 2, then delete using case 2 logic.
∙ Step 7 - Repeat the same process until the node is deleted from the tree.

Example

Construct a Binary Search Tree by inserting the following sequence of numbers...


10,12,5,4,20,8,7,15 and 13

Above elements are inserted into a Binary Search Tree as follows...


B - Tree Data structure
In search trees like binary search tree, AVL Tree, Red-Black tree, etc., every node contains only

one value (key) and a maximum of two children. But there is a special type of search tree called

B-Tree in which a node contains more than one value (key) and more than two children. B-Tree

was developed in the year 1972 by Bayer and McCreight with the name Height Balanced m

way Search Tree. Later it was named as B-Tree.

B-Tree can be defined as follows...


B-Tree is a self-balanced search tree in which every node contains multiple keys and

has more than two children.

Here, the number of keys in a node and number of children for a node depends on the order of

B Tree. Every B-Tree has an order.

B-Tree of Order m has the following properties...

∙ Property #1 - All leaf nodes must be at same level.

∙ Property #2 - All nodes except root must have at least [m/2]-1 keys and maximum of m 1

keys.

∙ Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at least m/2

children.
∙ Property #4 - If the root node is a non leaf node, then it must have atleast 2 children. ∙

Property #5 - A non leaf node with n-1 keys must have n number of children. ∙ Property

#6 - All the key values in a node must be in Ascending Order.

For example, B-Tree of Order 4 contains a maximum of 3 key values in a node and maximum of 4

children for a node.

Example

Operations on a B-Tree
The following operations are performed on a B-Tree...

1. Search

2. Insertion

3. Deletion

Search Operation in B-Tree

The search operation in B-Tree is similar to the search operation in Binary Search Tree. In a

Binary search tree, the search process starts from the root node and we make a 2-way decision

every time (we go to either left subtree or right subtree). In B-Tree also search process starts

from the root node but here we make an n-way decision every time. Where 'n' is the total

number of children the node has. In a B-Tree, the search operation is performed with O(log n)

time complexity. The search operation is performed as follows...

∙ Step 1 - Read the search element from the user.

∙ Step 2 - Compare the search element with first key value of root node in the tree. ∙ Step 3 -

If both are matched, then display "Given node is found!!!" and terminate the function

∙ Step 4 - If both are not matched, then check whether search element is smaller or larger

than that key value.


∙ Step 5 - If search element is smaller, then continue the search process in left subtree. ∙

Step 6 - If search element is larger, then compare the search element with next key value in

the same node and repeate steps 3, 4, 5 and 6 until we find the exact match or until the

search element is compared with last key value in the leaf node.

∙ Step 7 - If the last key value in the leaf node is also not matched then display "Element is

not found" and terminate the function.

Insertion Operation in B-Tree

In a B-Tree, a new element must be added only at the leaf node. That means, the new keyValue

is always attached to the leaf node only. The insertion operation is performed as follows...

∙ Step 1 - Check whether tree is Empty.

∙ Step 2 - If tree is Empty, then create a new node with new key value and insert it into the

tree as a root node.

∙ Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is

added using Binary Search Tree logic.

∙ Step 4 - If that leaf node has empty position, add the new key value to that leaf node in

ascending order of key value within the node.

∙ Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its

parent node. Repeat the same until the sending value is fixed into a node. ∙ Step 6 - If the

spilting is performed at root node then the middle value becomes new root node for the tree

and the height of the tree is increased by one.

Example

Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.


Red Black Tree
Red black tree is another variant of binary search tree in which every node is colored either red or black
we can define a red black tree as follows:

Red black tree is a binary search tree in which every node is colored either red or

black. A red-black tree's node structure would be:

struct t_red_black_node {
enum { red, black } colour;
void *item;
struct t_red_black_node *left,
*right,
*parent;
}
In red black tree the color of node is decided based on the properties of red black tree. Every red black
tree has the following properties:

1. Red black tree must be a binary search tree.

2.The root node must be colored black.

3. The children of red color node must be colored black. There should not be two consecutive red

nodes. 4. In all the paths of the tree there should be same number of black color nodes. 5. Every new

node must be inserted with red color.

6. Every leaf( i.e null node) must be colored black.

Example:

Following is a red black tree which is created by inserting number from 1 to 9.

4
6

1358

79

The above tree is a red black tree where every node is satisfying all the properties of red black tree.
Insertion into red black tree:
In a red black tree, every new node must be inserted with the color red. The insertion operation in red
black tree is similar to insertion operation in binary search tree. But it is inserted with a color property.
After every insertion operation, we need to check all the properties of red black tree. If all the properties
are satisfied then we go to next operation otherwise we perform the following operation to make it red
black tree.

The operations are

1. Recolor

2. Rotation

3. Rotation followed by recolor.

The insertion operation in red black tree is performed using the following steps:

Step 1: Check whether tree is empty.

Step2: If tree is empty when insert the newnode as root node with color black an exit from
the operation.

Step3: If tree is not empty then insert the newnode as leaf node with color

red. Step4: If the parent of newnode is black then exit from the operation.

Step5: If the parent of newnode is r red then change the color of parent node’s sibling of newnode.

Step6: If it is colored black or null then make suitable rotation and recolor it.

Step7: If it is colored red then perform recolor.

Repeat the same until tree becomes red black tree.

Example:

Create a red black tree by inserting following sequence of number :-

8, 18, 5, 15, 17, 25, 40, and 80

Insert (8)

Tree is empty. So insert newnode as root node with black color.

8
Insert (18)
Tree is not empty. So insert newnode with red color.

18
8

Insert (5)

Tree is not empty. So insert newnode with red color.

5 18

Insert (15)

Tree is not empty. So insert newnode with red color.

8Here there are two consecutive red nodes 18 and 15.


The newnode’s parent sibling color is red and
parent’s parent is root node. So we use recolor to
5 make it red black tree.
18

15

After recolor 8

15
After recolor operation, the trees
satisfying
18
all red black tree properties.
88
88
18
5 18
Insert (17)

The tree is not empty. So insert newnode with red color.


8

5 18 88 88 18 18

17
Here there two consecutive red
15
nodes 15 and 17.The newnode’s
parent sibling is null. So we need
rotation. Here we need LR rotation
After left rotation 8 and recolor.

8
After right rotation and recolor
5

17

15 18
15
18 88 88 18 18

5 17
Insert (25)

There are two consecutive red nodes 18 and

Tree is not empty. So insert newnode with red color. 8

5 18 recolor and recheck.


25. The newnode’s
parent sibling color is
red and parent’s
15
parent is not root
17 88 88 18 18
node. So we use 25
After recolor

88
88
18
After recolor operation, the tree is satisfying all red
17
5 black tree properties.
15
18
18

25
Insert( 40)

Tree is not empty. So insert newnode with red color.

8
Here there are two consecutive red nodes

After LL rotation and recolor 5 40


17
88
88
18
15 After LL rotation and recolor
25 18
17 operation, the tree satisfying all red
88 black tree properties.
88
18
15 18
18 18
25 and 40. The newnode’s parent
sibling is null. So we need rotation
and recolor. Here we use LL rotation
and recheck. 40

25
Insert(80)

Tree is not empty. So insert newnode with red color.


88

5 and parent’s parent is not root node. So


we
88
There are two consecutive red nodes 40
and
17

use recolor and recheck.


80. The newnodes parent sibling color is 18
red

18

40

After recolor
80
15
25 18
8
After recolor again there are two
consecutive red nodes 17 and 25. The
newnode’s parent sibling color is black.
So we need rotation. We use left rotation
And recolor.

5
17
88
88 40
18
15
25
18
80

18

After left rotation And recolor


17
8 25

5 15
18 80
40

Finally above tree is satisfying all the properties of red black tree and it is a perfect red black tree.

Deletion operation in red black tree


The deletion operation in red black tree is similar to deletion operation in BST. But after every deletion
operation we need to check with the red black tree properties. If any of the properties violated then
make suitable operations like recolor, rotation and rotation followed by recolor to make it red black
tree.

Operations on Red Black Tree in details:

Rotations:

A rotation is a local operation in a search tree that preserves in-order traversal key ordering.

Note that in both trees, an in-order traversal yields:

A x B y C
The left_rotate operation may be encoded:
left_rotate( Tree T, node x ) {
node y;
y = x->right;
/* Turn y's left sub-tree into x's right sub-tree */
x->right = y->left;
if ( y->left != NULL )
y->left->parent = x;
/* y's new parent was x's parent */
y->parent = x->parent;
/* Set the parent to point to y instead of x */
/* First see whether we're at the root */
if ( x->parent == NULL ) T->root = y;
else
if ( x == (x->parent)->left )
/* x was on the left of its parent */
x->parent->left = y;
else
/* x must have been on the right */
x->parent->right = y;
/* Finally, put x on y's left */
y->left = x;
x->parent = y;
}
Insertion:

Insertion is somewhat complex and involves a number of cases. Note that we start by inserting the new
node, x, in the tree just as we would for any other binary tree, using the tree_insert function. This
new node is labelled red, and possibly destroys the red-black property. The main loop moves up the
tree, restoring the red-black property.

Algorithm to Insert a New Node

Following steps are followed for inserting a new element into a red-black tree:

1. The newNode be:

2. Let y be the leaf (ie. NIL) and x be the root of the tree. The new node is inserted in the
following tree.

3. Check if the tree is empty (ie. whether x is NIL). If yes, insert newNode as a root node and color
it black.
4. Else, repeat steps following steps until leaf (NIL) is reached.
a. Compare newKey with rootKey.
b. If newKey is greater than rootKey, traverse through the right subtree.
c. Else traverse through the left subtree.
5. Assign the parent of the leaf as parent of newNode.
6. If leafKey is greater than newKey, make newNode as rightChild.
7. Else, make newNode as leftChild.

8. Assign NULL to the left and rightChild of newNode.


9. Assign RED color to newNode.

10. Call InsertFix-algorithm to maintain the property of red-black tree if violated.

Why newly inserted nodes are always red in a red-black tree?

This is because inserting a red node does not violate the depth property of a red-black tree.

If you attach a red node to a red node, then the rule is violated but it is easier to fix this problem
than the problem introduced by violating the depth property.
Algorithm to Maintain Red-Black Property After Insertion

This algorithm is used for maintaining the property of a red-black tree if insertion of a newNode
violates this property.

1. Do the following until the parent of newNode p is RED.


2. If p is the left child of grandParent gP of newNode, do the following.
Case-I:
a. If the color of the right child of gP of newNode is RED, set the color of both the children
of gP as BLACK and the color of gP as RED.

b. Assign gP to newNode.

Case-II:
c. (Before moving on to this step, while loop is checked. If conditions are not satisfied, it
the loop is broken.)
Else if newNode is the right child of p then, assign p to newNode.
d. Left-Rotate newNode.

Case-III:
e. (Before moving on to this step, while loop is checked. If conditions are not satisfied, it
the loop is broken.)
Set color of p as BLACK and color of gP as RED.

f. Right-Rotate gP.
3. Else, do the following.
a. If the color of the left child of gP of z is RED, set the color of both the children of gP
as BLACK and the color of gP as RED.
b. Assign gP to newNode.
c. Else if newNode is the left child of p then, assign p to newNode and
Right-Rotate newNode.
d. Set color of p as BLACK and color of gP as RED.
e. Left-Rotate gP.
4. (This step is perfomed after coming out of the while loop.)
Set the root of the tree as BLACK.

The final tree look like this:


The insertion operation is encoded as:

rb_insert( Tree T, node x ) {


/* Insert in the tree in the usual way */
tree_insert( T, x );
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) { if (
x->parent == x->parent->parent->left ) { /* If x's parent is a
left, y is x's right 'uncle' */ y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
}
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */ x = x->parent;
left_rotate( T, x );
}
/* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
right_rotate( T, x->parent->parent );
}
}
else {
/* repeat the "if" part with right and left
exchanged */
}
}
/* Colour the root black */
T->root->colour = black;
}

Examination of the code reveals only one loop. In that loop, the node at the root of the sub-tree
whose red-black property we are trying to restore, x, may be moved up the tree at least one
level in each iteration of the loop. Since the tree originally has O(log n) height, there are O(log
n) iterations. The tree_insert routine also has O(log n) complexity, so overall the rb_insert
routine also has O(log n) complexity.

Red-black trees:
Trees which remain balanced - and thus guarantee O(logn) search times - in a dynamic
environment. Or more importantly, since any tree can be re-balanced - but at considerable
cost - can be re-balanced in O(logn) time.

Time complexity in big O notation


Algorithm Average Worst case
Space O(n) O(n)
Search O(log n)[1]O(log n)[1]
Insert O(log n)[1]O(log n)[1]
Delete O(log n)[1]O(log n)[1]

Applications:
Red black tree offer worst case guarantee for insertion time, deletion time and search time. Not only
does this make them valuable in time sensitive applications such as real time applications but it makes
them valuable building blocks in other data structures which provide worst case guarantees.

1. Most of the self-balancing BST library functions like map and set in C++ (OR TreeSet and
TreeMap in Java) use Red Black Tree
2. It is used to implement CPU Scheduling Linux. Completely Fair Scheduler uses it.
GRAPHS
UNIT IV: Graph Theory Terminology, Graph Representations, Graph operations- Graph
Traversals (BFS & DFS), Connected components, Spanning Trees, Biconnected Components,
Minimum Spanning Trees- Krushkal’s Algorithm , Prim’s Algorithm, Shortest paths,
Transitive closure, All pairs Shortest path-Marshall’s Algorithm.

BASIC CONCEPTS
A graph is an abstract data structure that is used to implement the mathematical concept of
graphs. It is basically a collection of vertices (also called nodes) and edges that connect these
vertices. A graph is often viewed as a generalization of the tree structure, where instead of
having a purely parent-to-child relationship between tree nodes, any kind of complex
relationship can exist.

WHY GRAPHS ARE USEFUL


Graphs are widely used to model any situation where entities or things are related to each other
in pairs. For example, the following information can be represented by graphs:

∙ Family trees: in which the member nodes have an edge from parent to each of their children.
∙ Transportation networks: in which nodes are airports, intersections, ports, etc. The edges can be
airline flights, one-way roads, shipping routes, etc.

Definition
A graph G is defined as an ordered set (V, E), where V(G) represents the set of vertices and E(G)
represents the edges that connect these vertices.

A graph with V(G) = {A, B, C, D and E} and E(G) = {(A, B), (B, C), (A, D), (B, D), (D, E), (C,
E)}. Note that there are five vertices or nodes and six edges in the graph.
A graph can be directed or undirected. In an undirected graph, edges do not have any direction
associated with them. That is, if an edge is drawn between nodes A and B, then the nodes can be
traversed from A to B as well as from B to A.

www.Jntufastupdates.com 1
In a directed graph, edges form an ordered pair. If there is an edge from A to B, then there is a
path from A to B but not from B to A. The edge (A, B) is said to initiate from node A (also
known as initial node) and terminate at nodeB (terminalnode).
Directed Graph

Graph Terminology
Adjacent nodes or neighbours
For every edge, e = (u, v) that connects nodes u and v, the nodes u and v are the end-points and
are said to be the adjacent nodes or neighbours.

Degree of a node
Degree of a node u, deg(u), is the total number of edges containing the node u. If
deg(u) = 0, it means that u does not belong to any edge and such a node is known
as an isolated node.
Regular graph
It is a graph where each vertex has the same number of neighbours. That is, every node has the
same degree. A regular graph with vertices of degree k is called a k–regular graph or a regular
graph of degree k.

Path
A path P written as P = {v0 , v1 , v2 , ..., vn ), of length n from a node u to v is defined as a
sequence of (n+1) nodes. Here, u = v0 , v = vn and vi–1 is adjacent to vi for i = 1, 2, 3, ..., n.

Closed path
A path P is known as a closed path if the edge has the same end-points. That is, if v0 = vn .
Simple path

A path P is known as a simple path if all the nodes in the path are distinct with an exception that
v0 may be equal to vn . If v0 = vn , then the path is called a closed simple path.
Cycle
A path in which the first and the last vertices are same. A simple cycle has no repeated edges or
vertices (except the first and last vertices).
Connected graph
A graph is said to be connected if for any two vertices (u, v) in V there is a path from u to v.
That is to say that there are no isolated nodes in a connected graph. A connected graph that does
not have any cycle is called a tree. Therefore, a tree is treated as a special graph

www.Jntufastupdates.com 2
Complete graph A graph G is said to be complete if all its nodes are fully connected. That is,
there is a path from one node to every other node in the graph. A complete graph has n(n–1)/2
edges, where n is the number of nodes in G

Labelled graph or weighted graph


A graph is said to be labelled if every edge in the graph is assigned some data. In a weighted
graph, the edges of the graph are assigned some weight or length. The weight of an edge
denoted by w(e) is a positive value which indicates the cost of traversing the edge.
Multiple edges Distinct edges which connect the same end-points are called multiple edges.
That is, e = (u, v) and e' = (u, v) are known as multiple edges of G.

Loop An edge that has identical end-points is called a loop. That is, e = (u, u). Multi-graph
A graph with multiple edges and/or loops is called a multi-graph. Size of a graph The size of a
graph is the total number of edges in i

Directed Graphs
A directed graph G, also known as a digraph, is a graph in which every edge has a direction
assigned to it. An edge of a directed graph is given as an ordered pair (u, v) of nodes in G. For
an edge (u, v),

∙ The edge begins at u and terminates at v.


∙ u is known as the origin or initial point of e.Correspondingly, v is known as the destination or
terminal point of e.
∙ u is the predecessor of v. Correspondingly, v is the successor of u. ∑ Nodes u and v are
adjacent to each other

Terminology of a Directed Graph

Out-degree of a node The out-degree of a node u, written as outdeg(u), is the number of edges that
originate at u.

In-degree of a node The in-degree of a node u, written as indeg(u), is the number of edges that
terminate at u.

Degree of a node The degree of a node, written as deg(u), is equal to the sum of in degree and
out-degree of that node. Therefore, deg(u) = indeg(u) + outdeg(u).

www.Jntufastupdates.com 3
Isolated vertex A vertex with degree zero. Such a vertex is not an end-point of any edge.

Pendant vertex (also known as leaf vertex) A vertex with degree one.

Cut vertex A vertex which when deleted would disconnect the remaining graph.

Source A node u is known as a source if it has a positive out-degree but a zero in degree.

Sink A node u is known as a sink if it has a positive in-degree but a zero out-degree.

Reachability A node v is said to be reachable from node u, if and only if there exists a
(directed) path from node u to node v. For example, if you consider the directed graph given in
Fig. 13.5(a), you will observe that node D is reachable from node A.
Strongly connected directed graph A digraph is said to be strongly connected if and only if
there exists a path between every pair of nodes in G. That is, if there is a path from node u to v,
then there must be a path from node v to u.

Weakly connected digraph A directed graph is said to be weakly connected if it is connected


by ignoring the direction of edges. That is, in such a graph, it is possible to reach any node from
any other node by traversing edges in any direction (may not be in the direction they point). The
nodes in a weakly connected directed graph must have either out-degree or in-degree of at least
1.

Parallel/Multiple edges Distinct edges which connect the same end-points are called multiple
edges. That is, e = (u, v) and e' = (u, v) are known as multiple edges of G. In below diagram e3
and e5 are multiple edges connecting nodes C and D.

Simple directed graph A directed graph G is said to be a simple directed graph if and only if it
has no parallel edges. However, a simple directed graph may contain cycles with an exception
that it cannot have more than one loop at a given node.

REPRESENTATION OF GRAPHS
There are three common ways of storing graphs in the computer’s memory.

1. Adjacency Matrix
2. Adjacency List

www.Jntufastupdates.com 4
Adjacency Matrix Representation
In this representation, the graph is represented using a matrix of size total number of vertices by
a total number of vertices. That means a graph with 4 vertices is represented using a matrix of
size 4X4. In this matrix, both rows and columns represent vertices. This matrix is filled with
either 1 or 0. Here, 1 represents that there is an edge from row vertex to column vertex and 0
represents that there is no edge from row vertex to column vertex.

For example, consider the following undirected graph representation...


Since an adjacency matrix contains only 0s and 1s, it is called a bit matrix or a Boolean matrix.
The entries in the matrix depend on the ordering of the nodes in G. Therefore, a change in the
order of nodes will result in a different adjacency matrix.

Directed graph representation...

Graphs and their corresponding adjacency matrices

www.Jntufastupdates.com 5
From the above examples, we can draw the following conclusions:
1. For a simple graph (that has no loops), the adjacency matrix has 0s on the diagonal. 2. The
adjacency matrix of an undirected graph is symmetric.
3. The memory use of an adjacency matrix is O(n2 ), where n is the number of nodes in the
graph.
4. Number of 1s (or non-zero entries) in an adjacency matrix is equal to the number of edges in
the graph.
5. The adjacency matrix for a weighted graph contains the weights of the edges connecting the
nodes.

Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.
For example, consider the following directed graph representation implemented using linked
list...

This representation can also be implemented using an array as follows..

www.Jntufastupdates.com 6
The key advantages of using an adjacency list are:
1 It is easy to follow and clearly shows the adjacent nodes of a particular node.
2. It is often used for storing graphs that have a small-to-moderate number of edges. That is, an
adjacency list is preferred for representing sparse graphs in the computer’s memory; otherwise,
an adjacency matrix is a good choice.
3. Adding new nodes in G is easy and straightforward when G is represented using an adjacency
list. Adding new nodes in an adjacency matrix is a difficult task, as the size of the matrix needs
to be changed and existing nodes may have to be reordered.
4. For a directed graph, the sum of the lengths of all adjacency lists is equal to the number of
edges in G.
5. For an undirected graph, the sum of the lengths of all adjacency lists is equal to twice the
number of edges in G because an edge (u, v) means an edge from node u to v as well as an edge
from v to u.

Graph Traversal
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is also
used to decide the order of vertices is visited in the search process. A graph traversal finds the
edges to be used in the search process without creating loops. That means using graph traversal
we visit all the vertices of the graph without getting into looping path.
There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)

Breadth First Search (BFS) Algorithm

Breadth first search is a graph traversal algorithm that starts traversing the graph from root node
and explores all the neighboring nodes. Then, it selects the nearest node and explore all the
unexplored nodes. The algorithm follows the same process for each of the nearest node until it
finds the goal.

BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Queue data structure with maximum size of total number of vertices in
the graph to implement BFS traversal.

We use the following steps to implement BFS traversal...

∙ Step 1 - Define a Queue of size total number of vertices in the graph.


∙ Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the
Queue.
∙ Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue
and insert them into the Queue.
∙ Step 4 - When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.
∙ Step 5 - Repeat steps 3 and 4 until queue becomes empty.
∙ Step 6 - When queue becomes empty, then produce final spanning tree by removing unused
edges from the graph

www.Jntufastupdates.com 7
www.Jntufastupdates.com 8
Example 2:

∙ Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
∙ Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue. ∙ Rule 3 −
Repeat Rule 1 and Rule 2 until the queue is empty.

www.Jntufastupdates.com 9
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep
on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the program is
over.

Example

Consider the graph G shown in the following image, calculate the minimum path p from node A
to node E. Given that each edge has a length of 1.

Solution:
Minimum Path P can be found by applying breadth first search algorithm that will begin at node
A and will end at E. the algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1
holds all the nodes that are to be processed while QUEUE2 holds all the nodes that are
processed and deleted from QUEUE1.

Lets start examining the graph from Node A.

1. Add A to QUEUE1 and NULL to QUEUE2.

QUEUE1 = {A}
QUEUE2 = {NULL}

www.Jntufastupdates.com 10

You might also like