6156 - Data Structures and Algorithms
6156 - Data Structures and Algorithms
1
Data Structures and Algorithm 2022
Unit: I
Introduction and Overview: Basic Terminology – Data Structures – Data Structure
Operations –Mathematical Notations and Functions – Control Structures – Algorithms:
Time-space Trade-off –Complexity of Algorithms – Asymptotic Notations – Arrays –
Introduction – Linear Array, Representation of Linear Array in Memory, Traversing
Linear Arrays, Inserting and Deleting, Two Dimensional Arrays – Representation of Two
Dimensional Array in Memory.
Data Structures:
A data structure is a specialized format for organizing, processing, retrieving and storing
data.Data structures refer to data and representation of data objects within a program, that is,
the implementation of structured relationships. A data structure is a collection of atomic and
composite data types into a set with defined relationships.
The term Data Structure refers to the organization of data elements and the interrelationships
among them.
Data Structure is a way to store and organize data so that it can be used efficiently.
2
Data Structures and Algorithm 2022
3
Data Structures and Algorithm 2022
4
Data Structures and Algorithm 2022
5
Data Structures and Algorithm 2022
6
Data Structures and Algorithm 2022
TRAVERSING/VISITING:
▪ Accessing each record exactly once so that certain items in the record may be
processed.
▪ SEARCHING:
Finding the location of the record with a given key value, or finding the locations of
all records which satisfy one or more conditions.
▪ INSERTING: Adding a new record to the structure.
▪ DELETION: Removing a record from the structure.
▪ OPERATIONS USED IN SPECIAL SITUATIONS
▪ SORTING: Arranging the records in some logical order. (Numerical/Alphabetical)
▪ MERGING:Combining records in two different sorted files into a single sorted file.
▪ OTHER OPERATIONS
Copying , concatenation , etc.
7
Data Structures and Algorithm 2022
8
Data Structures and Algorithm 2022
9
Data Structures and Algorithm 2022
10
Data Structures and Algorithm 2022
11
Data Structures and Algorithm 2022
12
Data Structures and Algorithm 2022
13
Data Structures and Algorithm 2022
14
Data Structures and Algorithm 2022
Asymptotic Notations
Asymptotic notations are the mathematical notations used to describe the
running time of an algorithm when the input tends towards a particular
value or a limiting value.
There are mainly three asymptotic notations:
1. Big-O notation
2. Omega notation
3. Theta notation
15
Data Structures and Algorithm 2022
ARRAY:
16
Data Structures and Algorithm 2022
INTRODUCTION:
➢ Data structures are classified as either linear or non – linear.
➢ The linear relationship between the elements represented by means of sequential
memory locations. These linear structures are called arrays.
➢ The operations performed in an array are,
➢ Traversal – Processing each element in the list.
➢ Search – Finding the location of the element with a given value or the record with a
given key.
➢ Insertion – Adding a new element to the list.
➢ Deletion – Removing an element from the list.
➢ Sorting – Arranging the elements in an order.
➢ Merging – Combining two lists into a single list.
LINEAR ARRAYS:
o A linear array is a list of a finite number n of homogenous data elements (data elements
of same type).
o The elements of the array are referenced by an index set consisting of n consecutive
numbers.
o The elements of the array are store respectively in successive memory locations.
o The number n of elements is called the length or size of the array.
o The index set consists of the integers 1, 2,…. ,n.
o The length or the number of data elements of the array can be obtained from the index
set by the formula,
Length = UB-LB+1
17
Data Structures and Algorithm 2022
18
Data Structures and Algorithm 2022
LOC(DATA[56]=204
LOC(DATA[29]=208….
19
Data Structures and Algorithm 2022
20
Data Structures and Algorithm 2022
21
Data Structures and Algorithm 2022
22
Data Structures and Algorithm 2022
23
Data Structures and Algorithm 2022
24
Data Structures and Algorithm 2022
25
Data Structures and Algorithm 2022
Unit: II
Stacks- Array Representation of Stacks – Operations on Stack – Arithmetic Expressions:
Polish Notation– Reverse Polish Notation – Evaluation of a postfix expression –
Transforming Infix Expression into Postfix – Recursion – Queues – Representation of
Queues – Operations on Queues – Deques.
26
Data Structures and Algorithm 2022
Linke d List Represe ntation of Stacks : Although array representation of stacks is very
easy and convenient but it a llows the representation of only fixed sized stacks. In several
applications, the size of the stack may vary dur ing program execution. An obvious
solution to this problem is to represent a stack using a linked list.
A single linked list structure is sufficient to represent any stack. Here, the DATA field is
for the ITEM, and the LINK field is, as usua l, to point to the next' item. Above Figure b
depicts such a stack using a single linked list.
In the linked list representation, the first node on the list is the current item that is the
item at the top of the stack and the last node is the node containing the bottom-most item.
Thus, a PUSH operation w ill add a new node in the front and a POP operation will remove
a node from the front of the list.
POP:To remove an item from a stack, STATUS: To know the present state of a stack
27
Data Structures and Algorithm 2022
Here, we have assumed that the array index varies from 1 to SIZE and TOP points
the location of the current top-most item in the stack. The following algorithm Pop_
Array defines the POP of an item from a stack which is represented using an array A.
Now let us see how the same operations can be defined for a stack represented
with a single linked list.
28
Data Structures and Algorithm 2022
Thus, with the below rules of precedence and associativity of operators, the
evaluation will take place for the above-mentioned expression in the sequence
(sequence is according to the number 1, 2, 3, ... , etc.)
We translate, step by step, the following infix expressions into polish notation
using brackets to indicate a partial translation:
(A+B)*C = [+AB]*C=*+ABC
A+(B*C) = A+[*BC]=+A*BC
(A+B)/(C-D) = [+AB]/[-CD]=/+AB-CD
The fundamental property of polish notation is that the order in which the
operations are to be performed is completely determined by the positions of
the operators and operands in the expression.
29
Data Structures and Algorithm 2022
2.5 REVERSE POLISH NOTATION refers to the analogous notation in which the
operator symbol is placed after its two operands: AB+ , CD-, EF*,GH/.
Again, one never needs parentheses to determine the order of the operations in any
arithmetic expression written in reverse Polish notation.
In each step, the stack is the main tool that is used to accomplish the given task. We
illustrate these applications of stack in reverse order. That is, first we show how
stacks are used to evaluate postfix expressions and then we show how stacks are
used to transform infix expressions into postfix expressions.
Algorithm:
30
Data Structures and Algorithm 2022
Next character scanned is "*", which is an operator. Thus, we pop the top
two elements from the stack and perform the"*"operation with the two
operands. The second operand will be the first element that is popped.
The value of the expression(2*3) that has been evaluated(6) is pushed into the stack.
31
Data Structures and Algorithm 2022
The value of the expression (1+6) that has been evaluated(7) is pushed into the
stack.
Next character scanned is "-", which is an operator. Thus, we pop the top two elements
from the stack and perform the "-" operation with the two operands. The second
The value of the expression (7-4) that has been evaluated(3) is pushed into the stack.
Now, since all the characters are scanned, the remaining element in the
stack (there will be only one element in the stack) will be returned.
End result : Postfix String : 123*+4-
Result : 3
32
Data Structures and Algorithm 2022
Algorithm : POLISH(Q,P)
33
Data Structures and Algorithm 2022
2.8 RECURSION:
Recursion is an important concept in computer science. Suppose P is a procedure containing
either a call statement to itself or a call statement to a second procedure that may eventually
result in a call statement back to the original procedure P. then P is called recursive
procedure. A recursive procedure must have the following two properties:
• Base Case: It is nothing more than the simplest instance of a problem, consisting of a condition
that terminates the recursive function. This base case evaluates the result when a given
condition is met.
• Recursive Step: It computes the result by making recursive calls to the same function, but with
the inputs decreased in size or complexity.
For example, consider this problem statement: Print sum of n natural numbers using recursion.
This statement clarifies that we need to formulate a function that will calculate the summation of
all natural numbers in the range 1 to n. Hence, mathematically you can represent the function as:
34
Data Structures and Algorithm 2022
35
Data Structures and Algorithm 2022
The only difference between a stack and a queue is that in the case of stack insertion and
deletion (PUSH and POP) operations are at one end (TOP) only, but in a queue insertion
(called ENQUEUE) and deletion (called DEQUEUE) operations take place at two ends
called the REAR and FRONT of the queue, respectively. Figure represents a model of a
queue structure. Queue is also termed first-in first-out (FIFO)
Representation of a Queue using an Array A one-dimensional array, say Q[l ... N], can be
used to represent a queue. Figure shows an instance of such a queue. With this
representation, two pointers, namely FRONT and REAR, are used to indicate the two ends
of the queue. For the insertion of the next element, the pointer REAR will be the
consultant and for deletion the pointer FRONT will be the consultant.
FRONT:=FRONT+1
Similarly, whenever an element is added to the queue, the value of REAR
is increased by 1; this can be implemented by the assignment
REAR:=REAR+1
36
Data Structures and Algorithm 2022
This procedure deletes and element from a queue and assigns it to the variable ITEM.
1. If FRONT:=NULL then write UNDERFLOW and return
2. Set ITEM:=QUEUE[FRONT]
3. If FRONT=REAR then
Set FRONT:=NULL and REAR:=NULL
Else if
FRON
T=N
then
Set
FRON
T:=1
Else
Set FRONT:=FRONT+1
4. Return
37
Data Structures and Algorithm 2022
38
Data Structures and Algorithm 2022
2.12 DEQUES
A deque is a linear list in which elements can be added or removed at either end but not
in the middle. The term deque is a contraction of the name double ended queue. There
are various ways of representing a deque in a computer.
There are two variations of a deque- namely an input restricted deque and an output
restricted deque—which are intermediate between a deque and a queue.
Specifically, an input restricted deque is a deque which allows insertions at only one
end of the list but allows deletions at both ends of the list; and an output restricted
deque is a deque which allows deletions at only one end of the list but allows insertions
at both ends of the list.
39
Data Structures and Algorithm 2022
Unit: III
Linked List – Representation of Linked Lists in Memory – Traversing a Linked List –
Insertion into a Linked List – Deletion from a Linked List – Two-way Linked Lists –
Operations on Two-way Lists.
A linked list or one way list is a linear collection of data elements, called nodes, where the
linear order is given by means of pointers. That is, each node is divided into two parts: the
first part contains the information of the element, and the second part, called the link field
or next pointer field, contains the address of the next node in the list.
The null pointer, denoted by X in the diagram, signals the end of the list. The linked list
also contains a list pointer variable—called START or NAME which contains the address
of the first node in the list; hence there is an arrow drawn from START to the first node.
Clearly, we need only this address in START to trace through the list. A special case is the
list that has no nodes. Such a list is called the null list or empty list and is denoted by the
null pointer in the variable START.
Example:
40
Data Structures and Algorithm 2022
41
Data Structures and Algorithm 2022
42
Data Structures and Algorithm 2022
43
Data Structures and Algorithm 2022
Algorithms which insert nodes into linked lists come up in various situations.
• The first one inserts a node at the beginning of the list,
• Second one inserts a node after the node with a given location,
• Third one inserts anode into a sorted list.
All algorithms assume that the linked list is in memory in the form
LIST(INFO,LINK,START,AVAIL) and that the variable ITEM contains
the new information to be added to the list.
Since our insertion algorithms will use a node in the AVAIL list, all of the
algorithms will include the following steps:
(a) Checking to see if space is available in the AVAIL list. If not, that
is, if AVAIL=NULL, then the algorithm will print the message
OVERFLOW.
(b) Removing the first node from the AVAIL list. Using the variable
NEW to keep track of the location of the new node, this step can be
implemented by the pair of assignments.
NEW:=AVAIL, AVAIL:=LINK[AVAIL]
44
Data Structures and Algorithm 2022
Algorithm: INSFIRST(INFO,LINK,START,AVAIL,ITEM)
This algorithm inserts ITEM as the first node in the list.
1. If AVAIL=NULL, then write OVERFLOW, and exit.
2. Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM(copies new data into new node)
4. Set LINK[NEW]:=START(New node now points to original first node)
5. Set START:=NEW (changes START so it points to the new node)
6. Exit
45
Data Structures and Algorithm 2022
Algorithm: INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
This algorithm inserts ITEM so that ITEM follows the node with
location LOC or inserts ITEM as the first node when LOC=NULL.
1. If AVAIL=NULL, then write OVERFLOW and exit
2. Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM
4. If LOC=NULL, then
Set LINK [NEW]:=START and START:=NEW
Else
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW
5. Exit
Suppose ITEM is to be inserted into assorted linked LIST. Then ITEM must be inserted
between nodes A and B so that INFO(A) < ITEM ≤ INFO(B)
The following is a procedure which finds the location LOC of node A, that is, which finds
the location LOC of the last node in LIST whose value is less than ITEM.
46
Data Structures and Algorithm 2022
Algorithm : INSERT(INFO,LINK.START,AVAIL,ITEM)
This algorithm inserts ITEM into a sorted linked list.
1. Call FINDA(INFO,LINK,START,ITEM,LOC)
2. Call INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
3. Exit
You can delete either from the beginning, end or from a particular position.
1. Delete from beginning
• Point head to the second node, ie START will point to second node.
47
Data Structures and Algorithm 2022
In order to delete the node, which is present after the specified node, we need to skip the
desired number of nodes to reach the node after which the node will be deleted. We need
to keep track of the two nodes. The one which is to be deleted the other one if the node
which is present before that node.
Algorithm: DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
ThisalgorithmdeletesthenodeNwithlocationLOC.LOCPisthelocationofthe
nodewhich precedes N or, when N is the first node,LOCP=NULL.
1. If LOCP=NULL,thenSet START:=LINK[START]
Else
Set LINK[LOCP]:=LINK[LOC].
2. Set LINK[LOC]:=AVAIL andAVAIL:=LOC
3. Exit.
Deleting the node with a given ITEM of information
Let LIST be a linked list in memory. Suppose we are given an ITEM of information and we
want to delete from the LIST, the first node N which contains ITEM. Recall that before we
can delete N from the list, we need to know the location of the node preceding N. Accordingly,
first we give a procedure which finds the location LOC of the node N containing ITEM and
the location LOCP of the node preceding node N. If N is the first node, we set LOCP=NULL,
and if ITEM does not appear in LIST, we setLOC=NULL.
Procedure: FINDB(INFO,START,ITEM,LOC,LOCP)
This procedure finds the location LOC of the first node N which contains
ITEM and the
locationLOCPofthenodeprecedingN.IfITEMdoesnotappearinthelist,thent
48
Data Structures and Algorithm 2022
heprocedure sets LOC=NULL, and if ITEM appears in the first node, then
it setsLOCP=NULL.
1. If START =NULLthen
Set LOC:=NULL and LOCP:=NULL and return.
2. If INFO[START]=ITEM,then
Set LOC:=START and LOCP=NULL and return.
3. Set SAVE:=START andPTR:=LINK[START]
4. Repeat Steps 5 and 6 while PTR≠NULL
5. If INFO[PTR]=ITEM then
Set LOC:=PTR and LOCP:=SAVE and return.
6. Set SAVE:=PTR andPTR:=LINK[PTR]
7. SetLOC:=NULL
8. Return
Now we can easily present an algorithm to delete the first node N from a
linked list which contains a given ITEM of information.
Algorithm: DELETE(INFO,LINK,START,AVAIL,ITEM)
This algorithm deletes from a linked list the first node N which contains
the given ITEM of information.
1. Call FINDB(INFO,LINK,START,ITEM,LOC,LOCP)
2. If LOC=NULL, then Write ITEM not in list, and exit.
3. If LOCP=NULL then
Set START:=LINK[START]
Else
Set LINK[LOCP]:=LINK[LOC].
4. Set LINK[LOC]:=AVAIL and AVAIL:=LOC
5. Exit
49
Data Structures and Algorithm 2022
The list also requires two list pointer variables: FIRST, which points to the first node in the list,
and LAST, which points to the last node in the list.
Observe that the null pointer appears in the FORW field of the last node in the list and also in the
BACK field of the first node in the list.
Observe that, using the variable FIRST and the pointer field FORW, we can traverse a two way
list in the forward direction as before. On the other hand, using the variable LAST and the pointer
field BACK, we can also traverse the list in the backward direction.
Suppose LOCA and LOCB are the locations, respectively, of nodes A and B in a two way list.
Then the way that the pointers FORW and BACK are defined gives us the following:
50
Data Structures and Algorithm 2022
• Traversing: Visiting each node of the list at least once in order to perform some
specific operation like searching, sorting, display, etc.
• Searching: Comparing each node data with the item to be searched and return the
location of the item in the list if the item found else return null.
• Inserting :For each insertion operation, we need to consider the three cases. These three
cases also need to be considered when removing data from the doubly linked list.
51
Data Structures and Algorithm 2022
• Deleting : We are given the location LOC of a node N in LISR , and we want to
delete N from the list. BACK [LOCK] and FORW [LOC] are the locations.
FORW[BACK[LOC]]=FORW[LOC] and BACK[FORW[LOC]]=BACK[LOC].
Algorithm
DELTWL (INFO, FORW, BACK, START, AVAIL, LOC)
1. [Delete node.]
Set FORW[BACK[LOC]]=FORW[LOC] and BACK[FORW[LOC]]=BACK[LOC].
2.[Return node to AVAIL list]
Set FORW[LOC]=AVAIL and AVAIL=LOC
3. Exit
52
Data Structures and Algorithm 2022
Unit: IV
Trees - Binary Trees – Representing Binary Trees in Memory – Traversing
Binary Tree – Threads –Binary Search Tree – Graph Theory – Terminology
– Sequential Representation of Graph: Adjacency Matrix, Path Matrix –
Traversing a Graph, Breadth First Search, Depth First Search.
INTRODUCTION
In linear data structure data is organized in sequential order and in non-linear data structure data is
organized in random order. A tree is a very popular non-linear data structure used in a wide range of
applications. Tree is a non-linear data structure which organizes data in hierarchical structure and this
is a recursive definition.
DEFINITION OF TREE:
Tree is collection of nodes (or) vertices and their edges (or) links. In tree data structure, 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.
53
Data Structures and Algorithm 2022
In a normal tree, every node can have any number of children. A 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 a
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 Binary
Tree. In a binary tree, every node can have either 0 children or 1 child or 2
children butnot more than 2 children.
Example:
TREE TERMINOLOGIES:
1. Root Node: In a Tree data structure, the first node is called as Root Node. Every tree
must have a root node. We can say that the root node is the origin of the tree data structure.
In any tree, there must be only one root node. We never have multiple root nodes in a tree.
54
Data Structures and Algorithm 2022
2. Edge: In a Tree, 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 Node: In a Tree, the node which is a predecessor of any node is called as
PARENT NODE. In simple words, the node which has a branch from it to any other
node is called a parent node. Parent node can also be defined as "The node which
has child / children
55
Data Structures and Algorithm 2022
6. Internal Nodes: In a Tree data structure, the node which has at least one child is called as
INTERNAL Node. In simple words, an internal node is a node with at least 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.
56
Data Structures and Algorithm 2022
7. Degree: In a Tree data structure, the total number of children 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'
8.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 aLevel andthe Level
count starts with '0' and incremented by one at each level (Step).
9.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'.
57
Data Structures and Algorithm 2022
11.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 - Jhas length 4.
12.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.
58
Data Structures and Algorithm 2022
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.
The full binary tree obtained by adding dummy nodes to a binary tree is called as
Extended Binary Tree.
In above figure, a normal binary tree is converted into full binary tree by adding dummy
nodes.
59
Data Structures and Algorithm 2022
To represent a binary tree of depth 'n' using array representation, we need one
dimensional array with a maximum size of 2n + 1.
2.Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a double linked list,every node consists
of three fields.
o First field for storing left child address.
o second for storing actual data and third for the right child address.
o In this linked list representation, a node has the following structure
60
Data Structures and Algorithm 2022
The above example of the binary tree represented using Linked list representationis shown as
follows
In In-Order traversal, the root node is visited between the 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 the right child node. This in-order traversal is applicable for every root node of all sub
trees in the tree. This is performed recursively for all nodes in the tree.
Step-1: Visit the left
subtree, using inorder.
Step-2: Visit the root.
Step-3: Visit the right subtree, using inorder.
61
Data Structures and Algorithm 2022
In the above example of a binary tree, first we try to visit left child of root node 'A', but A's
left child 'B' 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
leftmost child. So first we 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 the
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 rightmost child in the tree. So we stop the process.
In Pre-Order traversal, the root node is visited before the 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 subtreesin the tree. Preorder search
is also called backtracking
.
Algorithm:
Step-1: Visit the root.
Step-2: Visit the left subtree, usingpreorder.
Step-3: Visit the right subtree, using preorder
62
Data Structures and Algorithm 2022
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 leftmost 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 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
rightmost 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.
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 itsroot node. This
is recursively performed until the right most nodes are visited.Algorithm:
Step-1: Visit the left subtree, using post order.
Step-2: Visit the right subtree, using post order
Step-3: Visitthe root.
using Post-OrderTraversal
63
Data Structures and Algorithm 2022
• You can utilize these fields in such a way so that the empty left child of anode points to
its in order predecessor and empty right child of the node points to its inorder successor.
• One way threading:- A thread will appear in a right field of a node andwill point to the
next node in the inorder traversal.
• Two way threading:- A thread will also appear in the leftfield of a nodeand will point
to the preceding node in the inorder traversal.
64
Data Structures and Algorithm 2022
o Similarly, the empty right child field of a node can be used to point to its in-
order Such a type of binary tree is known as a one way threaded binary tree.
o A field that holds the address of its in-order successor is known as
thread. In-order :- 30 40 50 60 65 69 72 80
o Similarly, the empty right child field of a node can be used to point to its inorder successor.
Inorder :- 30 40 50 60 65 69 72 80
65
Data Structures and Algorithm 2022
Node 30 does not have an inorder predecessor because itis the first node to be traversed
ininorder sequence
The threaded binary tree is represented as the left child of the header node.
66
Data Structures and Algorithm 2022
A binary search tree (BST) is a tree in which all nodes follows the below mentioned
properties:
o The left sub-tree of a node has key less than or equal to its parent node's key.
o The right sub-tree of a node has key greater than or equal to its parent node's key.
Thus, a binary search tree (BST) divides all its sub-trees into two
segments; left sub-tree and right sub-tree and can be defined as−
BST is a collection of nodes arranged in a way where they maintain BST properties. Each
node has key and associated value. While searching, the desired key is compared to the
keys in BST and if found, the associated value is retrieved.An example of BST −
We observe that the root node key (27) has all less-valued keys on the left sub-tree and
higher valued keys on the right sub-tree.
4.6 GRAPH
A graph is defined as Graph is a collection of vertices and arcs which connects vertices in the graph. A
graph G isrepresented as G = ( V , E ), where V is set of vertices and E is set of edges.
67
Data Structures and Algorithm 2022
Graph Terminology
1. Vertex : An individual data element of a graph is called as Vertex. Vertex is also known as
node. In above example graph, A, B, C, D & E are known as vertices.
2. Edge : An edge is a connecting link between two vertices. Edge is also known as Arc. An
edge isrepresented as(starting Vertex, ending Vertex). In above graph, the link between
vertices A and B isrepresented as (A,B).
Types of Graphs
1.Undirected Graph
2.Directed Graph
3.Complete Graph
A graph in which any V node is adjacent to all other nodes present in the graph is
known as a complete graph. An undirected graph contains the edges that are equal to
edges = n(n-1)/2 where n is the number of vertices present in the graph. The following
figure shows a complete graph.
68
Data Structures and Algorithm 2022
4.Weighted Graph
Outgoing Edge
A directed edge is said to be outgoing edge on its orign vertex.
Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.
Degree
Total number of edges connected to a vertex is said to be degree of that vertex.
Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that
vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
Self-loop
An edge (undirected or directed) is a self-loop if its two endpoints coincide.
Adjacent nodes
When there is an edge from one node to another then these nodes are called adjacent nodes.
Incidence
In an undirected graph the edge between v1 and v2 is incident on node v1 and v2.
69
Data Structures and Algorithm 2022
Sub Graph
A graph S is said to be a sub graph of a graph G if all the vertices and all the
edges of S are in G, and eachedge ofS has the same end vertices in S as in G. A
subgraph of G is a graph G’ such that V(G’) V(G) and E(G’) E(G)
Connected Graph
A graph G is said to be connected if there is at least one path between every pair of
vertices in G. Otherwise, G is disconnected.
This graph is disconnected because the vertex v1 is not connected with the other vertices
of the graph.
1.Adjacency Matrix
In this representation, graph can be represented using a matrix of size total number of vertices by
totalnumber ofvertices; means if a graph with 4 vertices can be represented using a matrix of 4X4
size.
70
Data Structures and Algorithm 2022
The adjacency matrix for an undirected graph is symmetric; the adjacency matrix
for a digraph neednot be symmetric.
2. Path Matrix in Graph Theory
Graph Theory is dependent on vertex (node) and edge (path) relationship. So, each and every
processneeds path matrix in graph theory. To find a path between two vertex or node path matrix
is the most easiest way. If you have a path matrix defined for a graph you can say whether a node
can be traveledfrom another specific node.
Below is a real life Data Structure example of Path Matrix in Graph Theory.
This graph defines train routes among India, Pakistan and Turkey.
So, We can answer the following answer from the path matrix of the graph.
Is there a train route between New Delhi and Istanbul?Ans: Yes.
Is there a train route between Kolkata and Istanbul?Ans: Yes.
Is there a train route between Islamabad and Kolkata?Ans: Yes.
And that is how path matrix works.
71
Data Structures and Algorithm 2022
Graph traversal is a technique used for searching a vertex in a graph. The graph traversal
is also used todecide the order of vertices is visited in the search process. A graph traversal
finds the edges to be usedin 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. BFS (Breadth First Search)
2. DFS (Depth First Search)
72
Data Structures and Algorithm 2022
nodes that are to be processed, while QUEUE2 holds all the nodes that are processed and
deleted fromQUEUE1.
Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all
neighbors of node A toqueue1.
1. QUEUE1 = {B, D}
2. QUEUE2 = {A}
Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all
neighbors of node B toqueue1.
1.QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}
Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all
neighbors of node D to queue1.The only neighbor of Node D is F since it is
already inserted, so it will not be inserted again.
1. QUEUE1 = {C, F}
2. QUEUE2 = {A, B, D}
Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors of
node C to queue1.
1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}
Step 6 - Delete node E from queue1. Since all of its neighbors have already been
added, so we will notinsert them again. Now, all the nodes are visited, and the target
node E is encountered into queue2.
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
73
Data Structures and Algorithm 2022
2.DFS algorithm
DFS algorithm in the data structure. It is a recursive algorithm to search all the
vertices of a tree data structure or a graph. The depth-first search (DFS) algorithm
starts with the initial node of graph G and goes deeper until we find the goal node
or the node with no children.
Algorithm
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state
(whose STATUS = 1) and settheir STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
74
Data Structures and Algorithm 2022
Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH all the
neighbors of A onto thestack that are in ready state.
1. Print: A
2. STACK: B, D
Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH all the
neighbors of D onto thestack that are in ready state.
1. Print: D
2. STACK: B, F
Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all
the neighbors of F onto thestack that are in ready state.
1. Print: F
2. STACK: B
Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all
the neighbors of B onto thestack that are in ready state.
1. Print: B
2. STACK: C
Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all
the neighbors of C onto thestack that are in ready state.
1. Print: C
2. STACK: E, G
Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of
G onto the stack that arein ready state.
1. Print: G
2. STACK: E
Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of
E onto the stack that arein ready state.
1. Print: E
2. STACK:
Now, all the graph nodes have been traversed, and the stack is empty.
75
Data Structures and Algorithm 2022
Unit: V
Sorting and Searching : Sorting- Bubble sort, Insertion, Selection, Merge sort , Quick
sort, Heap sort Searching: Liner search, Binary search.
Sorting
Sorting is the process of arranging the elements in the ascending or descending order.
The default sorting is ascending order.
1. External Sorting
2. Internal Sorting
External Sorting: It is used to handle massive amount of data for sorting. It is requied when
the data is being sorted do not fit into the main memory of a computing device(usually main
memory).
Internal Sorting: It is the type of sorting occur within a main memory. Internal sorting
algorithm types are as follows
1. Bubble sort
2. Insertion sort
3. Selection sort
4. Merge sort
5. Quick sort
6. Heap sort
Bubble sort: In this the adjacent or adjoining values are compared and exchanged if they
are not in the proper order. This process is repeated until the entire array is sorted.
Example: Consider the following numbers to be sort using bubble sort method
5 12 2 10 6
Pass 1
76
Data Structures and Algorithm 2022
5 5
12 2
2 12
10 10
6 6
5 5
2 2
12 10
10 12
6 6
5 5
2 2
10 10
12 6
6 12
Pass 2
5 2
2 5
10 10
6 6
12 12
2
5
10
6
12
77
Data Structures and Algorithm 2022
2 2
5 5
10 6
6 10
12 12
2
5
6
10
12
Now we can find all the elements are sorted in the ascending order using bubble sort .
1. Start
2. Read n [No .of elements]
3. Read data[] [No of elements]
4. Repeat steps 5,6 and 7 until 1 to n-1
5. Initially in a pass assume no interchange
InterchangeFALSE
6. Repeat steps 7 8 9
7. If data[i]>data[i+1] then
a. data[i]=data[i+1]
b. interchangeTRUE
// End of step 6
78
Data Structures and Algorithm 2022
Insertion sort
In this each successive element is picked and inserted at an appropriate position in
the previously sorted array.
Example: Consider the following numbers to be sort using insertion sort method
40 70 10 20 60 90
10 20 40 60 70 90
1. Start
2. A[0]=minimum integer value /* Now start sorting the array*/
3. Repeat steps 4 through 9 for k=1,2,3….N-1
{
4. Temp=A[k]
5. ptr=k-1
6. Repeat steps 7 to 8 while temp<A[ptr]
{
7. A[ptr+1]=A[ptr] //Moves element forward
8. ptr=ptr-1
}
9. A[ptr+1]=temp
}
10. End
79
Data Structures and Algorithm 2022
INSERTION SORT:
A[]
-∞ 40 70 10 20 60 90
0 1 2 3 4 5 6
Pass-1
K=2, temp=70,ptr=1
70<40→false
Then A[2]=70
Pass-2
K=3, temp=10,ptr=2
10<70→true
Then swap→A[3]=70,ptr=1
10<40→true
swap→A[2]=40,ptr=0
10<-∞→false
Then A[1]=10
10 40 70 20 60 90
Pass-3
K=4, temp=20,ptr=3
20<70→true
Then swap→A[4]=70,ptr=2
20<40→true
Again swap→A[3]=40,ptr=1
20<10→false
Then A[2]=20
10 20 40 70 60 90
80
Data Structures and Algorithm 2022
Pass-4
K=5, temp=60,ptr=4
60<70→true
Then swap→A[5]=70,ptr=3
60<40→false
Then A[4]=60
Pass-5
K=6, temp=90,ptr=5
90<70→false
Then A[6]=90
10 20 40 60 70 90
Selection Sort
In this the smallest or largest key from the remaining unsorted array us searched
for and put in the sorted array.
Example: Consider the following numbers to be sort using insertion sort method
40 70 10 20 60 90
In the above table in the unsorted array smallest element is picked and placed in the sorted
array one by one for ascending order. For descending order largest element should be picked.
81
Data Structures and Algorithm 2022
2.Call MIN(A,K,N,LOC)
3.Temp=A[k],A[K]=A[LOC],A[LOC]=Temp
[End of loop]
4.Exit
MIN(A,K,NLOC)
1.MIN:=A[K],LOC=K
[end loop]
3.Return
40 70 10 20 60 90
1 2 3 4 5 6
Pass-1
K=1,
40>70 false
J=3
40>10→ True
J=4→10>20→false
J=5→10>60→false
J=6→10>90→false
10 40 70 20 60 90
82
Data Structures and Algorithm 2022
Pass-2
K=2 MIN=40,LOC=2
J=3→40>70→false
MIN=40 LOC=3
J=4→40>20→True
MIN=20 LOC=4
J=5→20>60→false
J=6→20>90→false
10 20 40 70 60 90
Pass-3
K=3 MIN=40,LOC=3
J=4→40>70→false
J=5→40>60→false
J=6→40>90→false
0 20 40 70 60 90
Pass-4
K=4 MIN=70,LOC=4
J=5→70>60→true
MIN=60,LOC=5
J=6→60>90→false
10 20 40 60 70 90
Pass-5
J=6→70>90→false
10 20 40 60 70 90
83
Data Structures and Algorithm 2022
Quick Sort:- It is also called as partition exchange sort. In this we should place pivot
element in the correct position [middle] and partition the remaining into two sets.
One set contains the number less than pivot element and other with greater.
Example: Consider the following list of numbers
35 26 10 13 45 92 30 60
Here 35 is the pivot element, so it should be placed at center
26 10 13 30 35 45 60 92
10 13 26 30 35 45 60 92
10 13 26 30 35 45 60 92
10 13 26 30 35 45 60 92
0
• Left partition contains the numbers less than pivot element. The
partition may not be sorted.
• Right partition contains the numbers greater than pivot element. The
partition may not be sorted.
• In the left partition the bigger number is removed and placed before
the pivot element.
• In the right partition the bigger number is removed and placed at the
last
• In each steps both numbers from left and right partition is taken and
placed.
• Finally we will get the sorted elements.
84
Data Structures and Algorithm 2022
Merge Sort
✓ Merging means combing elements of two arrays to form a new array.
✓ Simplest way of merging two arrays is first copy all the elements of one array
into new array and then copy all the elements of other array into new array
.Then sort the new array. Another popular technique to have a sorted array
while merging. It is called merge sort.
Example
Step1:- Let us consider following two arrays A[7] and B[5] are to be merged to form a new
array. The new array say C will have 7+5=12 elements.
0 1 2 3 4 5 6
2 5 7 8 9 12 13
A
3 5 6 9 15
B
0 1 2 3 4 5 6 7 8 9 10 11
85
Data Structures and Algorithm 2022
Step2:- Compare A[0] and B[0]; since A[0] <B[0], Move A[0] to C[0]. Move the pointers if
array A and array C
0 1 2 3 4 5 6
2 5 7 8 9 12 13
A
3 5 6 9 15
B
0 1 2 3 4 5 6 7 8 9 10 11
2
C
Step3:- Compare A[1] and B[0]; Now B[0]<A[1]; Move B[0] to C[1] and move the pointer to
next element in an array.
0 1 2 3 4 5 6
2 5 7 8 9 12 13
A
3 5 6 9 15
B
0 1 2 3 4 5 6 7 8 9 10 11
2 3
C
Step 4:- Continuing the same way, the resultant array C is having all the elements of C in sorted
manner.
86
Data Structures and Algorithm 2022
87
Data Structures and Algorithm 2022
Heap Sort
✓ The heap is used in an elegant sorting algorithm called heap sort
✓ Let ‘H’ be heap and ‘N’ be the node. ‘H’ is maintained in the memory by linear
array using sequential representation not the linked representation.
✓ ‘H’ is called heap , if each node N of H should satisfy the following property
o The value at N is greater than or equal to the value at any of the descendants.
Example
44 30 50 22 60 55 77
a) Insert 44
44
b) Insert 30
44
30
c) Insert 50
44
30 50
50
30 44
88
Data Structures and Algorithm 2022
d) Insert 22
50
30 44
22
e) Insert 60
50
30 44
22 60
50
60 44
22 30
60
50 44
22 30
89
Data Structures and Algorithm 2022
f) Insert 55
60
50 44
22 30 55
60
50 55
22 30 44
g) Insert 77
60
50 55
22 30 44 77
60
50 77
22 30 44 55
77
50 60
22 30 44 55
◼ In the heap tree we can observe that the root element is the greater number among the
given number
◼ By repeatedly deleting the root element in the heap tree we can perform the sorting.
90
Data Structures and Algorithm 2022
Deleting the root element continuously and stored in the array as follows
77
50 60
22 30 44 55
Delete 77
60
50 55
22 30 44
77
Delete 60
55
50 44
22 30
60 77
Delete 55
50
30 44
22
55 60 77
91
Data Structures and Algorithm 2022
Delete 50
44
30
22
50 55 60 77
Delete 44
30
22
44 50 55 60 77
Delete 30
22
30 44 50 55 60 77
Delete 22
22 30 44 50 55 60 77
Now the array contains sorted elements.
1. Start
2. For i= 1 to n
a. Insert data[i] into heap
3. For i=n-1 down to 1
a. data[i]delete max from heap /* delete heap include reheaping also.
4. end
92
Data Structures and Algorithm 2022
Searching:
Searching is a technique to find the data element is present in the data structure or not.
Algorithm
93
Data Structures and Algorithm 2022
10 45 25 90 89 08
while(1<5)
{
If A[1]==ITEM)
45 ==25
// since the above condition is false the IF block will not execute
}
ctr=ctr+1→1+1=2
STEP 3
Now ctr=2
while(2<5)
{
If A[2]==ITEM)
25 ==25
PRINT “SEARCH SUCCESSFUL”
exit // program will be terminated after the execution of this statement
}
Now we get the output
“SEARCH SUCCESSFUL” 2 is the location of the element 25
94
Data Structures and Algorithm 2022
Binary Search
Binary Search is the popular search technique which searches the given ITEM in
minimum possible comparisons. The binary search requires the array, to be scanned , must be
sorted in any order(for instance , it may be ascending order). In binary search, the ITEM is
searched for in smaller segment (nearly half the previous segment) after each stage. For the
first stage, the segment will contain the entire array.
To search for ITEM in a sorted array(in ascending order), the ITEM is compared with
middle element of the segment(i.e., in the entire array for the first time. If the ITEM is more
than the middle element, latter part of the segment becomes new segment to be scanned; if the
item is less than the middle element, former part becomes new segment to be scanned, the same
process is repeated for the new segment(s) until either the ITEM is found(search successful) or
the segment is reduced to the single element and still the ITEM is not found(search
unsuccessful.
Algorithm
Case 1: Array A [L:U] is stored in the ascending order
95
Data Structures and Algorithm 2022
beg=1 last=12
Solution:
Step 1
beg=1 last=12
mid=INT(1+12)/2=int (6.5)=6
1 2 3 4 5 6 7 8 9 10 11 12
28
Step 2
data [ mid ] i.e., data[6] is 28
28<44 then
beg=mid+1→6+1=7
96
Data Structures and Algorithm 2022
Step 3
7 8 9 10 11 12
42
beg=7 last=12
Now mid=INT(beg+last)/2=int(7+12/2)=int(9)=9
data [ mid ] i.e., data[9] is 42
42<44 then
beg=mid+1→9+1=10
Step 4
10 11 12
49
beg=10 last=12
mid=INT(10+12/2)=int(11)=11
data [ mid ] i.e., data[11] is 49
49>44 then
last=mid-1→11-1=10
Step 5
mid=INT(beg+last)/2=10+10/2=20/2=10
data[10] ie., 44=44
Search successful at the location number 10
97
Data Structures and Algorithm 2022
2. https://www.geeksforgeeks.org/stack-data-structure/
3. http://www.it.griet.ac.in/wp-content/uploads/2014/08/UNIT-
V_QA.pdf
4. https://medium.com/learning-python-programming-
language/sorting-algorithms-insertion-sort-selection-sort-
quick-sort-merge-sort-bubble-sort-4f23bda6f37a
98