Download as pdf or txt
Download as pdf or txt
You are on page 1of 682

ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

Neetu Narayan

1
Introduction to Data Structures
ASET

OBJECTIVES

• To understand basic definitions and types of


data structures.
• To understand algorithm design and complexity.
• To understand time-space tradeoff

2
Definitions ASET

What is Data?
• Data is a collection of raw facts and figures.
• It is produced by many sources such as: recordings and
readings from a scientific experiment, generated by
some sensor or hardware, census..etc.

Image Source: https://www.building-blocks.com/content/blogs/different-types-of-data-sources 3


Definition ASET

What is a Data Structure?

• A data structure is a way to organize and store this data


in computer memory so that it can be managed and/or
processed by the computer effectively.

Why are you studying Data Structure?

4
Types of Data Structures ASET

• There are two major categories of data structures:

1) Simple or basic or primitive


e.g., char, short, int, long, float, double etc.

2) Complex or compound
A) Linear : sequential
e.g. arrays, stacks, queues, linked list etc.
B) Non-Linear : Non Sequential
e.g. trees, graphs etc.

5
Classification ASET

Image source: https://www.atnyla.com/tutorial/data-structure-introduction/3/299 6


What data structure to use? ASET

Data structures let the input and output be represented in a way


that can be handled efficiently and effectively.
array

Linked list

queue
tree stack
ASET

Contiguous(Array) Vs Noncontiguous(List)

8
Memory representation ASET

9
ASET

10
Operations on Data Structures ASET

Following are the basic operations supported by a


data structure:

• Traversal − print all the elements one by one.


• Insertion − add an element at given location.
• Deletion − delete an element at given location.
• Search − search an element using by value.
• Update − update an element at given location.
• Sorting − organizing data in chronological order
11
Algorithm Design ASET

What is an algorithm?

• An algorithm is a step-by-step procedure to perform


some task.

• It is a sequence of computational steps that transform


the input to output.

12
Algorithm ASET

Set of steps or instructions to process input

13
Complexity Theory ASET

What do you mean by Complexity of an algorithm?

• The complexity of algorithm is a way to compare the


performance of this algorithm with other similar
algorithms to perform the same task.

• The complexity can be measured in terms of either


“time” or “space”.

14
Time-Space Tradeoff ASET

• It states that either the time taken by the


algorithm or the space occupied by it can
be improved at a time.

• We can not design an algorithm which is


efficient in terms of both time and space.

15
ASET

Three cases to analyse an algorithm:


1) Worst Case
In the worst case analysis, we calculate upper bound on
running time of an algorithm.

2) Average Case
In average case analysis, we take all possible inputs and
calculate computing time for all of the inputs. Sum all the calculated
values and divide the sum by total number of inputs.

3) Best Case
In the best case analysis, we calculate lower bound on running
time of an algorithm.

16
ASET

Algorithm Notation:
• Before the steps, define the purpose of the algorithm.
• Number of steps in an algorithm should be finite.
• Use READ for input statement and PRINT OR WRITE for output
statement.
• Comments are enclosed within square brackets. [ ]
• Use EXIT or STOP to show end of an algorithm
• For Selection logic, use
 Single alternative
If condition, then:
[Module A]
[End of Structure]

17
ASET

Algorithm Notation:
 Double Alternative  Multiple Alternative
If condition, then: If condition(1), Then:
[Module B] [Module A]
Else: Else If condition(2), Then:
[Module A] [Module B]
[End of If structure] .
.
Else If condition(N), Then:
[Module N]
Else:
[Module M]
[End of If structure]
18
ASET

• For Iteration Logic:


 Repeat for loop:
Repeat for K=R to S by T:
[Module]
[End of loop]
 Repeat while loop
Repeat while condition:
[Module]
[End of loop]

19
ASET

Linear Arrays
A linear array is a list of finite number n of homogeneous
data elements such that:
a) The elements are referenced by an index set consisting
of n consecutive numbers.
b) The elements are stored in consecutive memory
locations.

Length or size of the array:


Length=UB-LB+1
Where UB is the index of last element and LB is the index
of first element.
20
ASET

Examples:
1. Let A be a linear array with LB=25 and UB=74. Find the
length of an array.

2. An automobile company uses an array AUTO to record


the total number of automobiles sold every year from
1932 through 1984. Find the number of records present
in an array.

21
ASET

Representation of Linear Arrays in Memory

Since array elements are stored in consecutive memory


locations, the computer need not to know the address of
every element of an array but keeps track of the address of
first element. The address of first element is called the
base address.

It is denoted by Base(arr).
Address of kth element of an array arr is given by:
Address(arr[k]) or LOC(arr[k]) = base(arr)+w(k-LB)
Where w is the number of words per memory cell.
22
ASET

Traversing a Linear Array


Let A be a linear array with N elements. This algorithm
traverses the given array A.

Step 1: Set I=1 list_trav(A,N)


Step 2: Repeat while I<=N {
PRINT: A[I]
int i;
Set I=I+1
[End of Step 2 loop]
for(i=0;i<N;i++)
Step 3: EXIT printf(“%d”, A[i]);
}

23
ASET

Insertion at the end of a linear Array


Let A be a linear array with N elements. This algorithm
inserts one element at the end of the list

Step 1: Set I=N+1 list_insert(A,N, VALUE)


Step 2: Set A[I]=VALUE {
Step 3: N=N+1 A[N]=VALUE;
Step 3: EXIT N=N+1;
Return(N);
}

24
ASET

Insertion at a given location in a linear Array


Let A be a linear array with N elements. This algorithm inserts
an element VALUE at Jth location of the list where j<N
Step 1: Set I=N list_insert(A, VALUE, J, N)
Step 2: Repeat while I>=J {
A[I+1]=A[I] int i;
Set I=I-1 for(i=N-1;i>=J-1;i--)
[End of Step 2 loop] {
Step 3: Set A[I+1]= VALUE A[i+1]=A[i];
Step 4: N=N+1 }
Step 5: EXIT A[i+1]=VALUE;
N++;
Return(N);
} 25
Insertion in an Array ASET

Step 1:

Find proper place to insert new element

Step 2:

Shift elements to the right to make room for new element

Step 3:

Insert new element 26


Deletion in an Array ASET

Step 1:
Find element to delete

Step 2:
Shift elements to the left to fill
the gap

Step 3:
Decrease No. of elements in
array by 1

27
ASET

Algorithm to delete the Kth element from a linear Array

Let A be a linear array with N elements. This algorithm deletes


Kth element from an array A where K<=N
Step 1: Repeat for I=K+1 to N
A[I-1]= A[I]
[End of Loop]
Step 2: N=N-1
Step: EXIT

28
ASET

Linear Search Algorithm:


Let A be a linear Array with N elements. This algorithm
finds the location of an element X in the array.

Step 1: Repeat for i =1 to N


Step 2: If A[i]=X, Then:
goto step 5
[end of If structure]
[End of For loop]
Step 3: PRINT: “element is not present”
Step 4: Goto step 6
Step 5: PRINT: “element is present at I location”
Step 6: EXIT

29
ASET

Binary Search Algorithm


Let A be a sorted array with N elements. This algorithm
finds the location of an element X in the given array.
Step 1: Set low = 1, high = n, Step 3: If A[mid]=X, then:
mid=(low+high)/2 Write: X is present at mid
Step 2: Repeat Steps while low<=high
location
if A[mid]=X, Then:
else:
goto step 3
Write: X is not present
else if A[mid] >X, then:
Step 4: EXIT
set high=mid-1
else:
set low=mid+1
[end of if structure]
set mid=(low+high)/2
[end of loop] 30
ASET

Example:
Let A be a sorted array with 12 elements.
10,15, 22, 34, 45, 58,69, 70,77,89,95, 112
Use binary search algorithm to find element
10 in the list

31
ASET

Bubble Sort Algorithm:


Let A be a linear array with N elements. This algorithm
sorts the given list.

Step 1: Repeat for i=1 to N-1


Step 2: Repeat for j=1 to N-i
if A[j]>A[j+1], then:
M=a[j+1]
A[j+1]=A[j]
A[j]=M
[end of If structure]
[end of step 2 loop]
[end of step 1 loop]
Step 3: EXIT
32
2D Array ASET

• The syntax for creating a two-dimensional array is:

<data type> <array name>[size of Dim1][size of Dim2];


e.g., int a[6][4];
ASET

Address of an element of a 2D Array


Let A[M x N] having base address Base(A), w is the number of words
per memory cell. Then address of LOC(A[J,K]) is given by :

• By Row Major Order

• By Column major order


ASET

Examples:
Q1. Let A be an array of size 100x50 is given. Find the address of the
element A[52,13] f the array is stored using
a) Row-Major order
b) Column-Major order
where w=2.

Q2. Suppose each student in a class of 25 students is given 4 tests.


Assuming the students are numbered from 1 to 25 and the test scores can
be assigned to a 25x4 matrix array TIME. Suppose the Base(TIME)=293.
The programming language stores 2D arrays using row-major order. Find
the address of TIME[12,3]

35
ASET

Multidimensional Arrays:

36
ASET

Address of an element of Multidimensional Array


Let A[m1x m2 …..mn ] be an n-dimensional array. Then
address of LOC(A[k1x k2 …..kn ]) is given by:

a) Using Row-Major order


LOC(A[k1x k2 …..kn ])=Base(A)+w[(..((E1L2 + E2)L3 + E3)L4 +
…+ En-1)Ln + En]

b) Using Column-Major order


LOC(A[k1x k2 …..kn ])=Base(A)+w[(..((EnLn-1 + En-1)Ln-1 )+
…+ E3)L2 + E2 )L1 + E1 ]

37
References ASET

• https://www.building-blocks.com/content/blogs/different-types-of-data-sources
• https://www.atnyla.com/tutorial/data-structure-introduction/3/299
• https://www.programmingsimplified.com/c/source-code/c-program-linear-search
• https://www.geeksforgeeks.org/introduction-to-algorithms/

38
ASET

39
Amity School of Engineering and Technology

Data Structures using C


(CSIT 124)
Module-2
Stack and Queues

Prepared By: Neetu Narayan 1


Amity School of Engineering and Technology

Contents :​
Stack
Definition
Stack Representation
Array Representation
Linked List Representation
 Operations on Stacks- Push & Pop

2
Amity School of Engineering and Technology

Learning Objectives​
• Impart in-depth knowledge of data structure and its implementation
in computer programs.
• Make students understand the concepts of Stack linear data structure.
• Make students understand the applications of Stack.

3
Amity School of Engineering and Technology

Recommended Reading​
Textbooks: ​
• Yashwant Kanetkar,”Data Structure using C”, BPB Publication, 5th Edition ,2011
• A.Tannenbaum,Y. Lanhgsam and A.J. Augenstein ,” Data Structures Using C And C++ “,Prentice Hall of
India,2nd Edition,2009.
• Jean-Paul Tremblay, P.G Sorenson, “An Introduction to Data Structures with applications”, Mcgraw-Hill
,2nd Edition ,1984.
​Reference Book: ​
• Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
• Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
• Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2nd Edition AddisonWesley,1996.
• E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition, 2008.
• R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4th Edition,2009
• E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2nd Edition, Universities Press,2008.

4
Amity School of Engineering and Technology

Module Assessment​
• Quiz (Conceptual and Numerical Based)

• Assignment

5
Amity School of Engineering and Technology

STACK
Definition: Stack is a linear data
structure that principally works on
the Last-in First-out (LIFO). The
element that is inserted first will be
out at last and vice-versa

Examples of Stack
6
Amity School of Engineering and Technology

STACK TERMINOLOGIES
1. Top of Stack: TOP is the pointer
that shows the position of the
topmost element of the stack.
2. MaxStack: MaxStack shows the
maximum size of the stack.
(“How many elements?”)
3. Stack Operations:
PUSH()
POP()

7
Amity School of Engineering and Technology

STACK: ARRAY REPRESENTATION

8
Amity School of Engineering and Technology

ARRAY REPRESENTATION

9
Amity School of Engineering and Technology

STACK: LIST REPRESENTATION

10
Amity School of Engineering and Technology

STACK OPERATIONS
There are two operations of stack:
1) PUSH Operation
2) POP Operation

11
Amity School of Engineering and Technology

PUSH: PUSH operation is used to push (Insert) an


element onto the stack
Step 1: If TOP >= MAXSIZE , then:
PRINT: “Stack Overflow”
GOTO Step 4
[end of if structure]
Step 2: TOP = TOP + 1
Step 3: STACK [TOP] = ITEM
Step 4: EXIT

12
Amity School of Engineering and Technology

POP : POP operation is used to pop (delete) an


element from the stack
Step 1: If TOP = 0, then:

PRINT: “Stack Underflow”

Goto Step 4

[End of if structure]

Step 2: Temp=STACK [TOP]

Step 3: TOP = TOP – 1

Step 4: EXIT
13
Amity School of Engineering and Technology

STACK OPERATIONS: EXAMPLE


Let as assume a Stack of maximum size=5
TOP
50
TOP
40 40
TOP
30 30 30
TOP
20 20 20 20
TOP
10 10 10 10 10

TOP=-1 TOP=0 TOP=1 TOP=2 TOP=3 TOP=4


MaxSize=5
When TOP= MaxSize-1; then stack becomes full and
PUSH
OPERATION insertion of any new element will result in
“OVERFLOW”. 14
Amity School of Engineering and Technology

STACK OPERATIONS: EXAMPLE


Let as assume a Stack of maximum size=5

50 TOP
TOP
40 40
TOP
30 30 30
TOP
20 20 20 20
TOP
10 10 10 10 10

TOP=4 TOP=3 TOP=2 TOP=1 TOP=1 TOP=-1

When TOP= -1; then stack becomes empty and


POP
OPERATION deletion of any element will result in
“UNDERFLOW”. 15
Amity School of Engineering and Technology

Time Complexity
Push() O(1)

Pop() O(1)

Display() O(n)

16
Expression
An expression is a collection of operators and operands that represents a specific value.
Based on the operator position, expressions are divided into three categories. They are
as follows...
• Infix Expression
• Postfix Expression (Reverse Polish)
• Prefix Expression (Polish)

17
Types of Expression
Infix Expression
• Operator is used in between operands.
Operand1 Operator Operand2
• Example : a+b

Postfix Expression
• Operator is used after operands.
Operand1 Operand2 Operator
• Example : ab+

Prefix Expression
• operator is used in between operands.
Operator Operand1 Operand2
• Example : +ab
18
INFIX TO POSTFIX ALGORITHM
Let Q be an expression written in infix notation. This algorithm converts the infix expression into Postfix expression P.
Read all the symbols one by one from left to right in the given Infix Expression.
1. Push "(" onto STACK, and add ")" to the end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the stack is empty
3. If an operand is encountered add it to P
4. If a left parenthesis is encountered push it onto the stack
5. if an operator is encountered , then
(a) Repeatedly pop from STACK and add to P each operator (on top of the STACK) which has same precedence as or higher precedence than
the operator encountered
(b) Add the encountered operator to the stack
[end of IF structure]
6. if a right parenthesis is encountered, then:
(a) repeatedly pop from the stack and add to P each operator (on top of the STACK) until a left parenthesis is encountered
(b) remove the left parenthesis [do not add left parenthesis to P]
[End of IF structure]
[End of step 2 loop]
19
Infix to Postfix
Infix Expression= A*B+C

20
Quiz 2

1. A-(B*C/D^F) +G *H/I-J

2. (A-2*(B+C)/D*E)+F

3. A+B*C/D-F+A^E

21
Evaluation of Postfix
Read all the symbols one by one from left to right in the given Postfix Expression.

• If the reading symbol is operand, then push it on to the Stack.

• If the reading symbol is operator (+ , - , * , / etc.,), then perform two pop


operations and store the two popped operands in two different variables (operand1(
stack top symbol) and operand2( next-to-top symbol)). Then perform reading symbol
operation using operand2 operator operand1 and push result back on to the Stack.

22
Example: A B * C +

Reading Symbol Stack operation Stack Content


A Push A
B Push A,B
* Operand1=B (A*B)
Operand2=A
A*B
C Push (A*B), C

+ Operand 1=C (A*B)+C


Operand2=(A*B)

23
Example: Evaluate (5+6*3)+(5+6 )*3 using Postfix Notation
Infix expression: (5+6*3)+(5+6 )*3)
Current Symbol Stack Postfix Expression Priority Level
1. +, -
( (( 2. *, /
3. ^
5 (( 5 4. ++, --
+ ((+ 5
6 ((+ 56
* ((+* 56
3 ((+* 563
) ( 563*+
+ (+ 563*+
( (+( 563*+
5 (+( 563*+5
+ (+(+ 563*+5 24
Infix expression: (5+6*3)+((5+6 )*3 = 56 )

Current Symbol Stack Postfix Expression


6 (+(+ 563*+56
) (+ 563*+56+
* (+ * 563*+56+
3 (+ * 563*+56+3
) 563*+56+3*+

25
Postfix Evaluation of Expression: 5 6 3 * + 5 6 + 3 * +
Reading Symbol Stack operation Stack Content
5 Push 5
6 Push 56
3 Push 563
* Pop3,Pop 6 and push 6*3 5 18
+ Pop18,Pop 5 and Push 5+18 23
5 Push 23 5
6 Push 23 5 6
+ Pop6,Pop 5 and push 5+6 23 11
3 Push 23 11 3
* Pop3,Pop 11 and Push 11*3 23 33
+ Pop33, Pop 23 and Push 23+33 56
26
Practice Question
Evaluate the following expression using postfix notation.
1) 5*2+(16/2^3-3*2)+14/7
2) 120-7^3/49+(5*2-2^3)-40

27
Infix To Prefix Algorithm
Read all the symbols one by one from right to left in the given Infix Expression.
1.Push ‘)‘ onto Stack and ‘(’ at the end of the infix expression.
2.If the symbol is an operand, then directly add it to output expression.
3.If the reading symbol is right parenthesis ‘)', then Push it on to the Stack.
4.If the reading symbol is left parenthesis ‘(', then pop all the symbols from the stack until a
right parenthesis appears. Discard the right parenthesis and add remaining popped symbols to
the output expression in the order in which they are popped.
5.If the reading symbol is an operator (+ , - , * , / etc.,), then Check, if the operator on the top of
the stack has higher precedence than the one being read, pop the operator and add it to the
output expression. Repeat the process until a lower or equal precedence operator appears at
the top of the stack. Then push the current operator onto the stack.
6.At the end after reading the entire infix expression, reverse the output expression.
Reverse of output expression is the Prefix expression.
28
Quiz 1

1. A-(B*C/D^F) +G *H/I-J

2. (A-2*(B+C)/D*E)+F

3. A+B*C/D-F+A^E

29
EXAMPLE: A*B+C

30
Evaluation of Prefix:
Read all the symbols one by one from right to left in the given Prefix Expression.

• If the reading symbol is operand, then push it on to the Stack.

• If the reading symbol is operator (+ , - , * , / etc.,), then perform two pop operations
and store the two popped operands in two different variables (operand1( stack top
symbol) and operand2( next-to-top symbol)). Then perform reading symbol operation
using operand1 operator operand2 and push result back on to the Stack.

31
Example: +*ABC
Reading Symbol Stack operation Stack Content
C Push C

B Push CB

A Push CBA

* Pop A, Pop B, Push (A*B) C (A*B)

+ Pop (A*B), POP C, Push(A*B)+C (A*B)+C

32
Example: Evaluate (5+6*3)+(5+6 )*3 using Prefix Notation
Infix expression: ( (5+6*3)+(5+6 )*3
Current Symbol Stack Output Expression
3 ) 3
* )* 3
) )*) 3
6 )*) 36
+ )*)+ 36
5 )*)+ 365
( )* 365+
+ )+ 365+*
) )+) 365+*
3 )+) 365+*3
* )+)* 365+*3 33
Infix expression: ( (5+6*3)+(5+6 )*3
Current Symbol Stack Output Expression

6 )+)* 365+*36

+ )+)+ 365+*36*

5 )+)+ 365+*36*5

( )+ 365+*36*5+

( 365+*36*5++

Prefix Expression= Reverse of Output Expression= + + 5 * 6 3 * + 5 6 3

34
Prefix Evaluation of Expression: + + 5 * 6 3 * + 5 6 3

Reading Symbol Stack operation Stack Content


3 Push 3
6 Push 36
5 Push 365
+ Pop 5, Pop 6, Push(5+6) 3 11
* Pop11, Pop3, Push(11*3) 33
3 Push 33 3
6 Push 33 3 6
* Pop 6, Pop3, Push(6*3) 33 18
5 Push 33 18 5
+ Pop 5, Pop 18, Push(5+18) 33 23
+ Pop 23, Pop33, Push(33+23) 56
35
Practice Question
Evaluate the following expression using postfix notation.
1) 5*2+(16/2^3-3*2)+14/7
2) 120-7^3/49+(5*2-2^3)-40

36
Amity School of Engineering and Technology

Parenthesis Checker
Algorithm:
1. Whenever an opening parenthesis is encountered, push it on stack.

2. For closing parenthesis, check what is at the top of the stack, if it corresponding opening parenthesis,
remove it from the top of the stack.
3. If parenthesis at the top of the stack is not corresponding opening parenthesis, return false, as there is
no point check the string further.
4. After processing entire string, check if stack is empty or not.
4.a If the stack is empty, return true.
4.b If stack is not empty, parenthesis do not match
37
Amity School of Engineering and Technology

Example:{()[(([])[])]}
Input Operation Stack Symbol
{()[([])]} PUSH {
()[([])]} PUSH {(
)[([])]} POP {
[([])]} PUSH {[
([])]} PUSH {[(
[])]} PUSH {[([
])]} POP {[(
)]} POP {[
]} POP {
} POP Empty
Accepted
38
Amity School of Engineering and Technology

Example:{()(}
Input Input Operation Stack Symbol
{()(} { PUSH {

()(} ( PUSH {(

)(} ) POP {

(} ( PUSH {(

} } POP {(

Error Not Balanced


39
Amity School of Engineering and Technology

Recursion
Recursion is the process by which function calls
itself repeatedly, until the specified condition has
been satisfied.
To solve a problem recursively, two conditions
must be satisfied:
1. A base value should be defined for which the
function should not call itself.
2. At each function call, the argument of the
function should move close to the base
value.

40
Amity School of Engineering and Technology

Factorial of a number using recursion


Algorithm: FACTO (1, 4)
FACTO ( FAC, NUMBER) FACTO (1,3)
1. If NUMBER=0, FACTO (1,2)
then FAC=1 and Return FACTO (1,1)
2. FACTO ( FAC, NUMBER-1) FACTO(1,0)
3. FAC=NUMBER*FAC FAC=1
4. Return FACTO(1,1)=1*FACTO (1,0)=1
FACTO(1,2)=2*FACTO(1,1)=2
FAC= 3*FACTO(1,2)=6
FAC=4*FACTO(1,4)=24

41
Amity School of Engineering and Technology

Towers of Hanoi
• Mathematical puzzle
• Consists of three tower (pegs) and more than one rings;
• These rings are of different sizes and stacked upon in ascending order

• There are other variations of puzzle where the number of disks increase, but the
tower count remains the same.
42
Amity School of Engineering and Technology

Rules
The mission is to move all the disks to some another tower without
violating the sequence of arrangement.

1.Only one disk can be moved among the towers at any given time.

2.Only the "top" disk can be removed.

3.No large disk can sit over a small disk.

43
Amity School of Engineering and Technology

Algorithm for Towers of Hanoi


START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
44
Amity School of Engineering and Technology

Example of Tower of Hanoi for disk=3

Initial A->C

A->B C->B

45
Amity School of Engineering and Technology

Example of Tower of Hanoi for disk=3

A->C B->A

B->C A->C
46
Amity School of Engineering and Technology

47
ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

Neetu Narayan

1
Introduction to Data Structures
ASET

OBJECTIVES
• To understand basic definitions and
representation of linear arrays in memory.
• To understand and implement operations on
array data structure.
• To understand and implement two-dimensional
arrays and their memory representation.
• To understand the use of multi-dimensional
arrays and implement them.

2
ASET

Bubble Sort Algorithm:


Let A be a linear array with N elements. This algorithm
sorts the given list.

Step 1: Repeat for i=1 to N-1


Step 2: Repeat for j=1 to N-i
if A[j]>A[j+1], then:
M=a[j+1]
A[j+1]=A[j]
A[j]=M
[end of If structure]
[end of step 2 loop]
[end of step 1 loop]
Step 3: EXIT
3
2D Array ASET

• The syntax for creating a two-dimensional array is:

<data type> <array name>[size of Dim1][size of Dim2];


e.g., int a[6][4];
ASET

Address of an element of a 2D Array


Let A[M x N] having base address Base(A), w is the number of words
per memory cell. Then address of LOC(A[J,K]) is given by :

• By Row Major Order

• By Column major order


ASET

Examples:
Q1. Let A be an array of size 100x50 is given. Find the address of the
element A[52,13] f the array is stored using
a) Row-Major order
b) Column-Major order
where w=2.

Q2. Suppose each student in a class of 25 students is given 4 tests.


Assuming the students are numbered from 1 to 25 and the test scores can
be assigned to a 25x4 matrix array TIME.Suppose the Base(TIME)=293.
The programming language stores 2D arrays using row-major order. Find
the address of TIME[12,3]

6
ASET

Multidimensional Arrays:

7
ASET

Address of an element of Multidimensional Array


Let A[m1x m2 …..mn ] be an n-dimensional array. Then
address of LOC(A[k1x k2 …..kn ]) is given by:

a) Using Row-Major order


LOC(A[k1x k2 …..kn ])=Base(A)+w[(..((E1L2 + E2)L3 + E3)L4 +
…+ En-1)Ln + En]

b) Using Column-Major order


LOC(A[k1x k2 …..kn ])=Base(A)+w[(..((EnLn-1 + En-1)Ln-2 )+
…+ E3)L2 + E2 )L1 + E1 ]

8
ASET

Example:

Let A[-10:2, 1:20, -3:8, 4:16] be a four


dimensional array with Base(A)=493 and w
=4. Find the address of A[-5, 12, 6, 10] using
a) Row-Major Order.
b) Column Major Order

9
Sparse Matrix ASET

Matrix in which most of the elements have 0


value, then it is called a sparse matrix.
00304 Representing a sparse matrix by a 2D array leads to
00570 wastage of lots of memory as zeroes in the matrix are of
00000 no use in most of the cases.
02600 Sol: store non-zero elements
with triples- (Row, Column, value)

10
ASET

11
Character String in C ASET

• Strings are defined as an array of


characters.
• The difference between a character array
and a string is that the string is
terminated with a special character ‘\0’

Declaration of Strings
char str_name[size];

12
In built String Functions ASET

Sr.No. Function & Purpose

1 strcpy(s1, s2);
Copies string s2 into string s1.

2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.

3 strlen(s1);
Returns the length of string s1.

4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.

13
Quiz 1
Write an algorithm to find multiplication
of two matrices
Quiz 1
Matrix Multiplication Algorithm
Let A be a matrix of size MxN and B be a matrix of size NxP. This algorithm
finds multiplication of the matrices
Step1: Repeat for I=1 to M
Step2: Repeat for J=1 to P
Step3: mult[I][J]=0
Step4: Repeat for S=1 to N
Step5: mult[I][J]=mult [I][J] +A[I][S]*B[S][J]
[end of step4 loop]
[end of step 2 loop]
[end of step1 loop]
Step6: EXIT
References ASET

• https://www.building-blocks.com/content/blogs/different-types-of-data-sources
• https://www.atnyla.com/tutorial/data-structure-introduction/3/299
• https://www.programmingsimplified.com/c/source-code/c-program-linear-search
• https://www.geeksforgeeks.org/introduction-to-algorithms/

16
ASET

17
Amity School of Engineering and Technology

Data Structures using C


(CSIT 124)
Module-2
Stack and Queues

Prepared By: Neetu Narayan 1


Amity School of Engineering and Technology

Contents :​
Stack
qDefinition
qStack Representation
qArray Representation
qLinked List Representation
q Operations on Stacks- Push & Pop

2
Amity School of Engineering and Technology

Learning Objectives​
• Impart in-depth knowledge of data structure and its implementation
in computer programs.
• Make students understand the concepts of Stack linear data
structure.
• Make students understand the applications of Stack.

3
Amity School of Engineering and Technology

Recommended Reading​
Textbooks: ​
• Yashwant Kanetkar,”Data Structure using C”, BPB Publication, 5th Edition ,2011
• A.Tannenbaum,Y. Lanhgsam and A.J. Augenstein ,” Data Structures Using C And C++ “,Prentice Hall of
India,2nd Edition,2009.
• Jean-Paul Tremblay, P.G Sorenson, “An Introduction to Data Structures with applications”, Mcgraw-Hill ,2nd
Edition ,1984.
​Reference Book: ​
• Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
• Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
• Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2 nd Edition
AddisonWesley,1996.
• E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition,
2008.
• R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4 th Edition,2009
• E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2 nd Edition, Universities
Press,2008. 4
Amity School of Engineering and Technology

Module Assessment​
• Quiz (Conceptual and Numerical Based)

• Assignment

5
Amity School of Engineering and Technology

STACK
Definition: Stack is a linear data
structure that principally works on
the Last-in First-out (LIFO). The
element that is inserted first will
be out at last and vice-versa

Examples of Stack
6
Amity School of Engineering and Technology

STACK TERMINOLOGIES
1. Top of Stack: TOP is the pointer
that shows the position of the
topmost element of the stack.
2. MaxStack: MaxStack shows the
maximum size of the stack.
(“How many elements?”)
3. Stack Operations:
Ø PUSH()
Ø POP()

7
Amity School of Engineering and Technology

STACK: ARRAY REPRESENTATION

8
Amity School of Engineering and Technology

ARRAY REPRESENTATION

9
Amity School of Engineering and Technology

STACK: LIST REPRESENTATION

10
Amity School of Engineering and Technology

STACK OPERATIONS
There are two operations of stack:
1) PUSH Operation
2) POP Operation

11
Amity School of Engineering and Technology

PUSH: PUSH operation is used to push (Insert) an


element onto the stack
Step 1: If TOP >= MAXSIZE , then:
PRINT: “Stack Overflow”
GOTO Step 4
[end of if structure]
Step 2: TOP = TOP + 1
Step 3: STACK [TOP] = ITEM
Step 4: EXIT

12
Amity School of Engineering and Technology

POP : POP operation is used to pop (delete) an


element from the stack
Step 1: If TOP = 0, then:

PRINT: “Stack Underflow”

Goto Step 4

[End of if structure]

Step 2: Temp=STACK [TOP]

Step 3: TOP = TOP – 1

Step 4: EXIT
13
Amity School of Engineering and Technology

STACK OPERATIONS: EXAMPLE


Let as assume a Stack of maximum size=5
TOP
50
TOP
40 40
TOP
30 30 30
TOP
20 20 20 20
TOP
10 10 10 10 10

TOP=-1 TOP=0 TOP=1 TOP=2 TOP=3 TOP=4


MaxSize=5
When TOP= MaxSize-1; then stack becomes full
PUSH
OPERATION and insertion of any new element will result in
“OVERFLOW”. 14
Amity School of Engineering and Technology

STACK OPERATIONS: EXAMPLE


Let as assume a Stack of maximum size=5
50 TOP
TOP
40 40
TOP
30 30 30
TOP
20 20 20 20
TOP
10 10 10 10 10

TOP=4 TOP=3 TOP=2 TOP=1 TOP=1 TOP=-1

When TOP= -1; then stack becomes empty and


POP OPERATION
deletion of any element will result in
“UNDERFLOW”. 15
Amity School of Engineering and Technology

Time Complexity
Push() O(1)

Pop() O(1)

Display() O(n)

16
Expression
An expression is a collection of operators and operands that represents a
specific value.
Based on the operator position, expressions are divided into three categories.
They are as follows...
• Infix Expression
• Postfix Expression (Reverse Polish)
• Prefix Expression (Polish)

17
Types of Expression
Infix Expression
• Operator is used in between operands.
Operand1 Operator Operand2
• Example : a+b

Postfix Expression
• Operator is used after operands.
Operand1 Operand2 Operator
• Example : ab+

Prefix Expression
• operator is used in between operands.
Operator Operand1 Operand2
• Example : +ab
18
INFIX TO POSTFIX ALGORITHM
Let Q be an expression written in infix notation. This algorithm converts the infix expression into Postfix expression P.
Read all the symbols one by one from left to right in the given Infix Expression.
1. Push "(" onto STACK, and add ")" to the end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the stack is empty
3. If an operand is encountered add it to P
4. If a left parenthesis is encountered push it onto the stack
5. if an operator is encountered , then
(a) Repeatedly pop from STACK and add to P each operator (on top of the STACK) which has same precedence as or higher
precedence than the operator encountered
(b) Add the encountered operator to the stack
[end of IF structure]
6. if a right parenthesis is encountered, then:
(a) repeatedly pop from the stack and add to P each operator (on top of the STACK) until a left parenthesis is encountered
(b) remove the left parenthesis [do not add left parenthesis to P]
[End of IF structure]
[End of step 2 loop]
19
Infix to Postfix
Infix Expression= A*B+C

20
Quiz 2

1. A-(B*C/D^F) +G *H/I-J

2. (A-2*(B+C)/D*E)+F

3. A+B*C/D-F+A^E

21
Evaluation of Postfix
Read all the symbols one by one from left to right in the given Postfix
Expression.

• If the reading symbol is operand, then push it on to the Stack.

• If the reading symbol is operator (+ , - , * , / etc.,), then perform two


pop operations and store the two popped operands in two different
variables (operand1( stack top symbol) and operand2( next-to-top
symbol)). Then perform reading symbol operation using operand2
operator operand1 and push result back on to the Stack.
22
Example: A B * C +

Reading Symbol Stack operation Stack Content


A Push A
B Push A,B
* Operand1=B (A*B)
Operand2=A
A*B
C Push (A*B), C

+ Operand 1=C (A*B)+C


Operand2=(A*B)

23
Example: Evaluate (5+6*3)+(5+6 )*3 using Postfix Notation
Infix expression: (5+6*3)+(5+6 )*3)
Priority
Current Symbol Stack Postfix Expression Level
1. +, -
( (( 2. *, /
5 (( 5 3. ^
4. ++, --
+ ((+ 5
6 ((+ 56
* ((+* 56
3 ((+* 563
) ( 563*+
+ (+ 563*+
( (+( 563*+
5 (+( 563*+5
+ (+(+ 563*+5 24
Infix expression: (5+6*3)+((5+6 )*3 = 56 )

Current Symbol Stack Postfix Expression


6 (+(+ 563*+56
) (+ 563*+56+
* (+ * 563*+56+
3 (+ * 563*+56+3
) 563*+56+3*+

25
Postfix Evaluation of Expression: 5 6 3 * + 5 6 + 3 *
+
Reading Symbol Stack operation Stack Content
5 Push 5
6 Push 56
3 Push 563
* Pop3,Pop 6 and push 6*3 5 18
+ Pop18,Pop 5 and Push 5+18 23
5 Push 23 5
6 Push 23 5 6
+ Pop6,Pop 5 and push 5+6 23 11
3 Push 23 11 3
* Pop3,Pop 11 and Push 11*3 23 33
+ Pop33, Pop 23 and Push 23+33 56
26
Practice Question
Evaluate the following expression using postfix notation.
1) 5*2+(16/2^3-3*2)+14/7
2) 120-7^3/49+(5*2-2^3)-40

27
Infix To Prefix Algorithm
Read all the symbols one by one from right to left in the given Infix Expression.
1.Push ‘)‘ onto Stack and ‘(’ at the end of the infix expression.
2.If the symbol is an operand, then directly add it to output expression.
3.If the reading symbol is right parenthesis ‘)', then Push it on to the Stack.
4.If the reading symbol is left parenthesis ‘(', then pop all the symbols from the stack
until a right parenthesis appears. Discard the right parenthesis and add remaining
popped symbols to the output expression in the order in which they are popped.
5.If the reading symbol is an operator (+ , - , * , / etc.,), then Check, if the operator on
the top of the stack has higher precedence than the one being read, pop the
operator and add it to the output expression. Repeat the process until a lower or
equal precedence operator appears at the top of the stack. Then push the current
operator onto the stack.
6.At the end after reading the entire infix expression, reverse the output expression.
Reverse of output expression is the Prefix expression.
28
Quiz 1

1. A-(B*C/D^F) +G *H/I-J

2. (A-2*(B+C)/D*E)+F

3. A+B*C/D-F+A^E

29
EXAMPLE: A*B+C

30
Evaluation of Prefix:
Read all the symbols one by one from right to left in the given Prefix
Expression.

• If the reading symbol is operand, then push it on to the Stack.

• If the reading symbol is operator (+ , - , * , / etc.,), then perform two pop


operations and store the two popped operands in two different variables (ope
( stack top symbol) and operand2( next-to-top symbol)). Then perform
reading symbol operation using operand1 operator operand2 and push
result back on to the Stack.
31
Example: +*ABC
Reading Symbol Stack operation Stack Content
C Push C

B Push CB

A Push CBA

* Pop A, Pop B, Push (A*B) C (A*B)

+ Pop (A*B), POP C, Push(A*B)+C (A*B)+C

32
Example: Evaluate (5+6*3)+(5+6 )*3 using Prefix Notation
Infix expression: ( (5+6*3)+(5+6 )*3
Current Symbol Stack Output Expression
3 ) 3
* )* 3
) )*) 3
6 )*) 36
+ )*)+ 36
5 )*)+ 365
( )* 365+
+ )+ 365+*
) )+) 365+*
3 )+) 365+*3
* )+)* 365+*3 33
Infix expression: ( (5+6*3)+(5+6 )*3
Current Symbol Stack Output Expression

6 )+)* 365+*36

+ )+)+ 365+*36*

5 )+)+ 365+*36*5

( )+ 365+*36*5+

( 365+*36*5++

Prefix Expression= Reverse of Output Expression= + + 5 * 6 3 * + 5 6 3

34
Prefix Evaluation of Expression: + + 5 * 6 3 * + 5 6
3
Reading Symbol Stack operation Stack Content
3 Push 3
6 Push 36
5 Push 365
+ Pop 5, Pop 6, Push(5+6) 3 11
* Pop11, Pop3, Push(11*3) 33
3 Push 33 3
6 Push 33 3 6
* Pop 6, Pop3, Push(6*3) 33 18
5 Push 33 18 5
+ Pop 5, Pop 18, Push(5+18) 33 23
+ Pop 23, Pop33, Push(33+23) 56
35
Practice Question
Evaluate the following expression using postfix notation.
1) 5*2+(16/2^3-3*2)+14/7
2) 120-7^3/49+(5*2-2^3)-40

36
Amity School of Engineering and Technology

Parenthesis Checker
Algorithm:
1. Whenever an opening parenthesis is encountered, push it on stack.

2. For closing parenthesis, check what is at the top of the stack, if it corresponding opening
parenthesis, remove it from the top of the stack.
3. If parenthesis at the top of the stack is not corresponding opening parenthesis, return false,
as there is no point check the string further.
4. After processing entire string, check if stack is empty or not.
4.a If the stack is empty, return true.
4.b If stack is not empty, parenthesis do not match
37
Amity School of Engineering and Technology

Example:{()[(([])[])]}
Input Operation Stack Symbol
{()[([])]} PUSH {
()[([])]} PUSH {(
)[([])]} POP {
[([])]} PUSH {[
([])]} PUSH {[(
[])]} PUSH {[([
])]} POP {[(
)]} POP {[
]} POP {
} POP Empty
Accepted
38
Amity School of Engineering and Technology

Example:{()(}
Input Input Operation Stack Symbol
{()(} { PUSH {

()(} ( PUSH {(

)(} ) POP {

(} ( PUSH {(

} } POP {(

Error Not Balanced


39
Amity School of Engineering and Technology

Recursion
Recursion is the process by which function
calls itself repeatedly, until the specified
condition has been satisfied.
To solve a problem recursively, two
conditions must be satisfied:
1. A base value should be defined for
which the function should not call
itself.
2. At each function call, the argument of
the function should move close to the
base value.

40
Amity School of Engineering and Technology

Factorial of a number using recursion


Algorithm: FACTO (1, 4)
FACTO ( FAC, NUMBER) FACTO (1,3)
1. If NUMBER=0, FACTO (1,2)
then FAC=1 and Return FACTO (1,1)
2. FACTO ( FAC, NUMBER-1) FACTO(1,0)
3. FAC=NUMBER*FAC FAC=1
4. Return FACTO(1,1)=1*FACTO
(1,0)=1
FACTO(1,2)=2*FACTO(1,1)=2
FAC= 3*FACTO(1,2)=6
FAC=4*FACTO(1,4)=24

41
Amity School of Engineering and Technology

Towers of Hanoi
• Mathematical puzzle
• Consists of three tower (pegs) and more than one rings;
• These rings are of different sizes and stacked upon in ascending order

• There are other variations of puzzle where the number of disks increase,
but the tower count remains the same.
42
Amity School of Engineering and Technology

Rules
The mission is to move all the disks to some another tower
without violating the sequence of arrangement.

1.Only one disk can be moved among the towers at any given
time.

2.Only the "top" disk can be removed.

3.No large disk can sit over a small disk.

43
Amity School of Engineering and Technology

Algorithm for Towers of Hanoi


START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
44
Amity School of Engineering and Technology

Example of Tower of Hanoi for disk=3

Initial A->C

A->B C->B

45
Amity School of Engineering and Technology

Example of Tower of Hanoi for disk=3

A->C B->A

B->C A->C
46
Amity School of Engineering and Technology

47
Data structures using C
Module 2
STACKS AND QUEUES
Prepared By
Ms. Neetu Narayan

48
QUEUE:
QUEUE: Array implementation, Linked List implementation, Circular Queue,
Applications of queue: Priority queue, Double ended queue

49
• Queue is a linear data structure in which the insertion and deletion
operations are performed at two different ends.
• In a queue data structure, the insertion operation is performed at a
position which is known as 'rear' and the deletion operation is
performed at a position which is known as 'front'.
• Queue follows FIFO (First In First Out) principle.
• In a queue data structure, the insertion operation is performed using
a function called "enQueue()" and deletion operation is performed
using a function called "deQueue()".
Operations on Queue
•enQueue(value) - (To insert an element into the
queue)
•deQueue() - (To delete an element from the
queue)
•display() - (To display the elements of the queue)

Implementation of Queue
ØUsing Array
ØUsing Linked List
Queue using Array
• Step 1: Create a one dimensional array with above defined SIZE
(int queue[SIZE])

• Step 2: Define two integer variables 'front' and 'rear' and initialize
both with '-1'. (int front = -1, rear = -1)

#define SIZE 5

int queue[SIZE],

Int front=-1,rear=-1;
EnQueue(value) - Inserting value
Step 1: Check whether queue is FULL. (rear == SIZE-1)
Step 2: If it is FULL, then display "Queue is FULL!!! and terminate
Step 3: If it is EMPTY, then place the first item in position front =
rear = 0
Step 4 : if NOT FULL,NOT EMPTY , then increment rear value by
void enqueue(value)
{ one (rear++) and set queue[rear] = value.
if(rear == SIZE-1)
printf("Queue is Full“);
else if(front == -1 && rear ==-1)
{ front = 0;
rear = 0;
queue[rear] = value;
}
else
{ rear++;
queue[rear] = value;
}
}
Example of Insertion in Queue
dequeue() – Removing value
Step 1: Check whether queue is EMPTY. (front == rear=-1)
Step 2: If it is EMPTY, then display "Queue is EMPTY!!" and terminate.
Step 3: If it is NOT EMPTY, then check if both front and rear are
equal (front == rear), then set both front and rear to '-1'
(front = rear = -1).
else increment the front value by one (front ++). Then
display queue[front] as deleted element.
Void dequeue()
{ if(front == rear)
printf(“Queue is Empty!!! “);
else {
printf(“Deleted : %d “, queue[front]);
if(front == rear)
front = rear = -1;
else
front++;
}
}
Example of Deletion in Queue
display() - Displays the elements of a Queue
1: Check whether queue is EMPTY. (front == rear)
2a: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate.
2b: If it is NOT EMPTY, then define an integer variable 'i' and set
'i = front+1'
3: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat
the same until 'i' value is equal to rear (i <= rear)

void display()
{
if(front ==-1 && rear= = -1)
printf(“Queue is Empty!!! “);
else
{ int i;
printf(“Queue elements are:");
for(i=front; i<=rear; i++)
printf(“%d”, queue[i]);
}
}
Linked List implementation

In linked list implementation of a queue,


• the last inserted node is always pointed by 'rear'
• the first node is always pointed by 'front'.
Circular Queue, Applications of queue: Priority queue, Double
ended queue
Why Circular Queue
• In a normal Queue Data Structure, we can insert elements until
queue becomes full. But once if queue becomes full, we can not
insert the next element until all the elements are deleted from the
queue.

• rear' is still at last position. In above situation, even though we have


empty positions in the queue we can not make use of them to insert
• Circular Queue is a linear data structure in which the
operations are performed based on FIFO (First In First
Out) principle and the last position is connected back to
the first position to make a circle.
• Peek () returns a[front]
• Enqueue(x)
• Dequeue()
Insertion in Circular Queue
Step 1: IF FRONT=0 and REAR=MAX-1 OR • Enqueue (x)
FRONT = REAR+1 Then {
Write “Overflow”
if((rear+1)%SIZE==FRONT) //full
return
Goto Step 4 else if(isempty())
{
[END OF IF] front=0;
Step 2: IF FRONT = REAR=-1 then rear=0;
a[rear]=x;
SET FRONT=REAR=0 }
ELSE IF REAR=MAX-1 and FRONT!=0 else
{
SET REAR=0 rear = (rear+1)%SIZE;
ELSE a[rear]=x;
}
SET REAR=REAR+1
• [END OF IF] If Current Rear position=7
New position = (7+1)%8 = 0
Step 3: SET QUEUE[REAR]=X
Step 4: EXIT
e.g.

63
e.g.

64
e.g.

65
e.g.

66
Deletion in Circular Queue
Step 1: If FRONT = -1 then dequeue ()
Write ("Circular Queue Underflow“) {
GOTO Step 4 Step 2: if(isempty())
SET Step 2: SET return
VAL=QUEUE[FRONT] else if(front = rear) // Single element in
Step 3: If FRONT = REAR then Queue
SET FRONT=REAR=-1 { front=-1;
ELSE rear=-1;
IF FRONT=MAX-1 }
SET FRONT=0 Else
ELSE {
SET FRONT=FRONT+1 Front = (front +1)%SIZE
[END OF IF] }
[END OF IF] }
Step 4: EXIT
e.g.

68
e.g.

69
e.g.

70
QUEUE: Array implementation, Linked List implementation,
Circular Queue, Applications of queue: Priority queue,
Double ended queue
Priority Queue
• In priority queue, each element is assigned a priority.
• Priority of an element determines the order in which the
elements will be processed.
• Rules:
1. An element with higher priority will processed before an
element with a lower priority.
2. Two elements with the same priority are processed on a
First Come First Serve basis.
Types of Priority Queue
1. Ascending Priority Queue
• In this type of priority queue, elements can be inserted into any
order but only the smallest element can be removed.

2. Descending Priority Queue


• In this type of priority queue, elements can be inserted into any
order but only the largest element can be removed.
Array Representation of Priority Queue
Insertion Operation:
• While inserting elements in priority queue we will add it at the
appropriate position depending on its priority
• It is inserted in such a way that the elements are always ordered
either in Ascending or descending sequence
Array Representation of Priority Queue
15 10
A[0] A[1] A[2] A[3] A[4]

Front Rear

20 15 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
2 1 1 1
5
A[0] 5
A[1] 3
A[2] 0
A[3] A[4]

Front Rear
Array Representation of Priority Queue
Deletion Operation:
• While deletion, the element at the front is always
deleted.
Array Representation of Priority
Queue 20 15 13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear

15 13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
Double ended Queue (Deque)
Double Ended Queue is also a Queue data structure in which the
insertion and deletion operations are performed at both the ends
(front and rear).

Deque represented in TWO ways,

• Input Restricted Double Ended Queue


• Output Restricted Double Ended Queue
Double ended Queue (Deque)
• It is also known as “Head-Tail Linked List”
• It has two pointers LEFT and RIGHT, which point to either end
of the deque.
• Dequeue can be implemented using Circular Array or a Circular
doubly linked list where Deque [n-1] is followed by Dequeue[0]
• Input Restricted Double Ended Queue

the insertion operation is performed at only one end and

deletion operation is performed at both the ends.


• Output Restricted Double Ended Queue

the deletion operation is performed at only one end.


81
ASET

Data Structure Using C


Module-III (stacks and queues using linked list)

Dept. of CSE
B.Tech, 3th Sem
Ms. Neetu Narayan

82
Agenda…. ASET

Stack and queue


implementation using
linked list

83
Linked list ASET

implementation of
• Instead of using array, we can also use linked list to implement stack.


stack
Linked list allocates the memory dynamically.

• LIFO(last in first out)

• In linked list implementation of stack, the nodes are maintained non-contiguously in the
memory.

• Each node contains a pointer to its immediate successor node in the stack.

• Stack is said to be overflown if the space left in the memory heap is not enough to
create a node.

• The top most node in the stack always contains null in its address field.

84
ASET

85
Adding a node to the stack (Push ASET
operation)
• Adding a node to the stack is referred to as push operation.
• Time Complexity : O(1)
• In order to push an element onto the stack, the following steps are
involved.
1. Create a node first and allocate memory to it.
2. If the list is empty then the item is to be pushed as the start node of the list.
This includes assigning value to the data part of the node and assign null to the address
part of the node.
3. If there are some nodes in the list already, then we have to add the new element in the
beginning of the list.
For this purpose, assign the address of the starting element to the address field of the
new node and make the new node, the starting node of the list.

86
ASET

push(value) - Inserting an element into the Stack


Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL)
Step 3 - If it is Empty, then set newNode → next = NULL.
Step 4 - If it is Not Empty, then set newNode → next = top.
Step 5 - Finally, set top = newNode.

87
push(value) - Inserting an element into the Stack ASET

void push ()
{ else //some nodes are already
int val; present in the stack
struct node *ptr =(struct node*)malloc(sizeof(str {
uct node)); ptr->val = val;
if(ptr == NULL) ptr->next = head;
{ head=ptr;
printf("not able to push the element");
} }
else printf("Item pushed");
{
printf("Enter the value"); }
scanf("%d",&val); }
if(head==NULL) //No node is present in
the stack
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}

88
Deleting a node from the stack (POP ASET
operation)
• Deleting a node from the top of stack is referred to as pop operation.
• Deleting a node from the linked list implementation of stack is different from
that in the array implementation.
• In order to pop an element from the stack, we need to follow the following
steps :
• Check for the underflow condition: The underflow condition occurs when
we try to pop from an already empty stack.
The stack will be empty if the head pointer of the list points to null.
• Adjust the head pointer accordingly: In stack, the elements are popped
only from one end, therefore, the value stored in the head pointer must be
deleted and the node must be freed.
The next node of the head node now becomes the head node.
• Time Complexity : o(1)

89
pop() - Deleting an Element from a Stack
ASET

Step 1 - Check whether stack is Empty (top == NULL).


Step 2 - If it is Empty, then display "Stack is Empty!!!
Deletion is not possible!!!" and terminate the function
Step 3 - If it is Not Empty, then define a Node pointer ‘ptr'
and set it to 'top'.
Step 4 - Then set 'top = top → next'.
Step 5 - Finally, delete ‘ptr'. (free(ptr)).

90
ASET

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
91
Display the nodes (Traversing) ASET

Displaying all the nodes of a stack needs traversing all the nodes of the linked
list organized in the form of stack
1. Copy the head pointer into a temporary pointer.
2. Move the temporary pointer through all the nodes of the list and print
the value field attached to every node.
Time Complexity : o(n)
void display() else
{ {
int i; printf("Printing Stack elements \n");
struct node *ptr; while(ptr!=NULL)
ptr=head; {
if(ptr == NULL) printf("%d\n",ptr->val);
{ ptr = ptr->next;
printf("Stack is empty\n"); }
} }
}

92
ASET

Linked List implementation of


Queue

93
Linked List implementation of
ASET
Queue
Drawback of array implementation
• Memory wastage : The space of the array, which is used to store queue elements,
can never be reused to store the elements of that queue because the elements can
only be inserted at front end and the value of front might be so high so that, all the
space before that, can never be filled.

• Deciding the array size-


Size is requires to be declared in advance.
Due to the fact that, the queue can be extended at runtime depending upon the
problem, the extension in the array size is a time taking process and almost
impossible to be performed at runtime since a lot of reallocations take place.
Due to this reason, we can declare the array large enough so that we can store queue
elements as enough as possible but the main problem with this declaration is that,
most of the array slots (nearly half) can never be reused.
It will again lead to memory wastage.

94
Linked List
ASET
implementation of Queue
• The storage requirement of linked representation of a queue with n
elements is o(n) while the time requirement for operations is o(1).
• In a linked queue, each node of the queue consists of two parts i.e.
data part and the link part.
• Each element of the queue points to its immediate next element in the
memory.
• In the linked queue, there are two pointers maintained in the memory
i.e. front pointer and rear pointer.
• The front pointer contains the address of the starting element of the
queue while the rear pointer contains the address of the last element
of the queue.
• Insertion and deletions are performed at rear and front end
respectively.
• If front and rear both are NULL, it indicates that the queue is empty.

95
Operation on Linked Queue ASET

Insert operation
The insert operation append the queue by adding an element to the end of the
queue. The new element will be the last element of the queue.

Step 1: allocate the memory for the new node ptr by using the following statement.

Ptr = (struct node *) malloc (sizeof(struct node));

96
ASET

There can be the two scenario of inserting this new node ptr into the linked queue.

In the first scenario, we insert element into an empty queue. In this case, the
condition front = NULL becomes true.
Now, the new element will be added as the only element of the queue and the next
pointer of front and rear pointer both, will point to NULL

ptr -> data = item;


if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}

97
ASET

In the second case, the queue contains more than one element.
The condition front = NULL becomes false.
In this scenario, we need to update the end pointer rear so that the next pointer of
rear will point to the new node ptr.
Since, this is a linked queue, hence we also need to make the rear pointer point to
the newly added node ptr. We also need to make the next pointer of rear point to
NULL.

rear -> next = ptr;


rear = ptr;
rear->next = NULL;

98
ASET

Algorithm

Step 1: Allocate the space for the new


node PTR
Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT =
NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
Step 4: END

99
ASET

Deletion
Deletion operation removes the element that is first inserted among all the queue elements.
Firstly, we need to check either the list is empty or not.
The condition front == NULL becomes true if the list is empty, in this case , we simply write
underflow on the console and make exit.

Otherwise, we will delete the element that is pointed by the pointer front.
For this purpose, copy the node pointed by the front pointer into the pointer ptr. Now, shift the
front pointer, point to its next node and free the node pointed by the node ptr.

ptr = front;
front = front -> next;
free(ptr);

100
ASET

Algorithm (delete)

Step 1: IF FRONT = NULL


Write " Underflow "
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END

101
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
CSIT124:Data Structure Using C

1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
Introduction to Module-IV ASET

At the end of this module, student should be able


to :
• Understand the definition of Tree
• Describe the basic terminology of Tree
– Root node, sub tree, leaf, path, ancestor node,
degree, level number
• Differentiate between different types of Tree
– General tree, forests, binary tree, binary search tree,
expression tree, tournament tree
Introduction to Module-IV ASET

At the end of this module, student should be able


to :
• Understand the basic terminology of Binary
Trees and its representation
– Definition of Binary Tree
– Different Types of Binary Tree
– Properties of Binary Tree
– Sequential representation of Binary Tree
– Linked representation of Binary Tree
– Tree traversals: pre-order, in-order, post-order
Introduction to Module-IV ASET

At the end of this module, student should be able


to :
• Understand the basic terminology and
operations of Binary Search Trees
– Introduction to binary search tree
– Traversals in binary search tree
– Various operations in binary search tree
• Insertion of a node
• Deletion of a node
• Searching of a node
Introduction to Module-IV ASET

At the end of this module, student should be able


to :
• Define AVL Tree, balance factor in AVL Tree,
Operations on AVL Tree, AVL Tree rotations
with example
• Define Threaded Binary Tree, One-way and two-
way representation, Traversals, Comparison
between Threaded Binary Tree vs Binary Tree,
Advantages and Disadvantages of Threaded
Binary Tree
• Define of B-Tree, its operations with example
ASET

Let’s start Module-IV

8
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
CSIT124:Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Define Tree
• Understand the basic terminology of tree
• Differentiate between various types of tree
Tree ASET

• Non- linear Data Structure


• A Tree is a recursive data structure containing the set of
one or more data nodes where one node is designated as
the root of the tree while the remaining nodes are called as
the children of the root.
• The nodes other than the root node are partitioned into the
non-empty sets where each one of them is to be called sub-
tree.
• Nodes of a tree either maintain a parent-child relationship
between them or they are sister nodes.
• In a general tree, A node can have any number of children
nodes but it can have only a single parent.

6
Tree ASET

• The following image shows a tree, where the node A is the


root node of the tree while the other nodes can be seen as
the children of A.

7
Basic terminology
• Root Node :-
The topmost node in the tree hierarchy.
The root node is the one which doesn't have any
parent.

• Sub Tree :-
If the root node is not null, the tree T1, T2 and
T3 is called sub-trees of the root node.

• Leaf Node :-
The node of tree, which doesn't have any child
node, is called leaf node.
It is the bottom most node of the tree.
There can be any number of leaf nodes present
in a general tree.
Also called external nodes.

• Path :-
The sequence of consecutive edges is called
path.
In the tree shown in the above image, path to
the node E is A→ B → E.
Basic terminology
• Ancestor node :-
An ancestor of a node is any predecessor
node on a path from root to that node.
The root node doesn't have any ancestors.
In the tree shown, the node F have the
ancestors, B and A.
• Degree :-
Degree of a node is equal to number of
children, a node have.
In the tree shown in the above image, the
degree of node B is 2.
Degree of a leaf node is always 0 while in a
complete binary tree, degree of each node
is equal to 2.
• Level Number :-
Each node of the tree is assigned a level
number in such a way that each node is
present at one level higher than its parent.
Root node of the tree is always present at
level 0.
Types of Trees ASET

General
tree

Tournament
Forest
Tree

Trees

Expression
Binary Tree
Tree

Binary
search Tree

10
General Tree ASET

• Stores the elements in a hierarchical order in which the


top-level element is always present at level 0 as the root
element.

• All the nodes except the root node are present at number
of levels.

• The nodes which are present on the same level are called
siblings while the nodes which are present on the different
levels exhibit the parent-child relationship among them.

• A node may contain any number of sub-trees.

• The tree in which each node contain 3 sub-tree, is called


ternary tree.
11
Forests ASET

• Set of disjoint trees

• Obtained by deleting the root node and the edges which connects
root node to the first level node.

12
Binary Tree ASET

• Binary tree is a data structure in which each node can have at


most 2 children.

• The node present at the top-most level is called the root node.

• A node with the 0 children is called leaf node.

• Binary Trees are used in the applications like expression


evaluation and many more.

13
Binary Search Tree ASET

• Binary search tree is an ordered binary tree.

• All the elements in the left sub-tree are less than the root while
elements present in the right sub-tree are greater than or equal
to the root node element.

• Binary search trees are used in most of the applications of


computer science domain like searching, sorting, etc.

14
Expression Tree ASET

• Expression trees are used to evaluate the simple arithmetic


expressions.

• Expression tree is basically a binary tree where internal nodes are


represented by operators while the leaf nodes are represented by
operands.

• Expression trees are widely used to solve algebraic expressions


like (a+b)*(a-b).

15
Tournament Tree ASET

• Tournament tree are used to record the winner of the match in


each round being played between two players.

• Tournament tree can also be called as selection tree or winner


tree.

• External nodes represent the players among which a match is


being played while the internal nodes represent the winner of the
match played.

• At the top-most level, the winner of the tournament is present as


the root node of the tree.

16
Summary ASET

In this session, we have discussed about


• Definition of Tree
• Basic Terminology of Tree
– Root node, sub tree, leaf, path, ancestor node,
degree, level number
• Different Types of Tree
– General tree, forests, binary tree, binary search tree,
expression tree, tournament tree
Quick Revision ASET
Quick Revision ASET
Quick Revision ASET
ASET

Thank You..☺

21
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Define binary tree
• Differentiate between various types of binary
tree
• Understand the properties of binary tree
• Describe sequential and linked representation of
binary tree
Binary Tree ASET

• Binary Tree is a special type of generic tree in which, each node can have
at most two children.

• Binary tree is generally partitioned into three disjoint subsets.

– Root of the node

– left sub-tree which is also a binary tree.

– Right binary sub-tree

6
Types of Binary Tree ASET

• Strictly Binary Tree


– In Strictly Binary Tree, every non-leaf node contain non-empty left and right
sub-trees.

– In other words, the degree of every non-leaf node will always be 2.

– A strictly binary tree with n leaves, will have (2n - 1) nodes.

7
Types of Binary Tree ASET

• Complete Binary Tree


– A Binary Tree is said to be a complete binary tree if all levels have
maximum number of nodes, except the last level.

– A complete binary tree is a binary tree that contains 2^l nodes at each
level between level 0 and h-1. (where l=level)

– The maximum number of nodes in the last level of a complete binary


tree with height h range from1 to 2h-1.

– All the nodes are towards the left if it is not full.

8
Types of Binary Tree ASET

9
Types of Binary Tree ASET

2 3 For any node k,


Number of left child is 2k
4 5 6 7 right child is 2k+1
Parent is floor (k/2)
8 9 10 11 12 13 14 15

10
Types of Binary Tree ASET

Skewed Tree
– A skewed binary tree is a type of binary tree in which all the nodes have only
either one child or no child.
– 1. Left Skewed Binary Tree:
• These are those skewed binary trees in which all the nodes are having a left
child or no child at all.
• It is a left side dominated tree. All the right children remain as null.
– 2. Right Skewed Binary Tree:
• These are those skewed binary trees in which all the nodes are having a right
child or no child at all.
• It is a right side dominated tree. All the left children remain as null.

11
Types of Binary Tree ASET

Extended Binary Tree


• In a binary tree, if each subtree is replaced by a special node then the
resulting tree is extended tree or 2-tree
• Special nodes ----- External nodes
• Original nodes -----Internal nodes 0

Path length of a node= Number of 1 1


edges traversed from that node to the
root node = Level number of that node
2 2 2 2
Internal Path length of Binary tree
Sum of path length of all internal 3 3 3 3 3 3
nodes I= 0+1+1+2+2+2= 8

External path length of a binary tree


Sum of path length of all external
nodes
E= 2+3+3+3+3+3+3 =20
12
Some Interesting ASET

Properties of Binary trees

13
Some Interesting ASET

Properties of Binary trees


• In a non-empty binary tree, if n=number of nodes, e= number of edges,
then e=n-1
Every node has exactly one parent except root node
N-1 nodes have exactly one parent
There is only one edge between a parent and child.
_______________________________________
(By mathematical induction)
If n=1, e=0
for k nodes and e edges,
e=k-1

let k’ = k+1 and e’=e+1


e’-1=k’-1-1
e’=k’-1
Tree t Tree t’

14
Some Interesting ASET

Properties of Binary trees

15
Some Interesting ASET

Properties of Binary trees


• In an extended binary tree, if E is external path length, I is the internal path length
and n is the number of internal nodes, then E=I+2n
Suppose we have root node and
2 children, n=1, I=0, E= 2

A
level p
A n=6
level p Tree T’ I= 0+1+1+2+2+2= 8
E= 2+3+3+3+3+3+3=20
Internal Nodes: k-1 ------(1)
Tree T
Internal Path length I’=I-p
External Path Length:
Internal node: k
E’= E-2(p+1)+p
External Node length: E
E’=I’+2(k-1) [we assume that
Internal Node length: I
(1)is true]
E-2(p+1)+p= I-p +2(k-1)
E=I+2k 16
Binary Tree representation ASET

Sequential Representation
– This is the simplest memory allocation technique to store the
tree elements

– An inefficient technique since it requires a lot of space to store


the tree elements.

– In this representation, a 1-d array is used to store the tree


elements.

– Size of the array will be equal to the number of nodes present


in the tree.

17
ASET

Note:
1. The root node of the tree
will be present at the
1st index of the array.
2. If a node is indexed k, it will
be stored in kth place in the
array

18
ASET

19
ASET

Note:
1. Root is stored at
index 1
2. For any node k, left
child is stored at 2k,
right child at 2k+1 and
parent is at floor(k/2)

20
ASET

Note: Issues with sequential representation

For 25 nodes 31

21
Binary Tree representation ASET

• Linked Representation
– The binary tree is stored in the memory, in the form of a
linked list where the number of nodes are stored at non-
contiguous memory locations and linked together by inheriting
parent child relationship like a tree.

– Every node contains three parts :


• Pointer to the left node,
• Data element
• Pointer to the right node.

– Each binary tree has a root pointer which points to the root
node of the binary tree.
– In an empty binary tree, the root pointer will point to null.

22
ASET

Figure A: Tree, as the collection of nodes (where


each node contains three parts : left pointer, data
element and right pointer.)

Figure B: Memory Allocation of Binary Tree


Using Linked List

Note:
1. Left pointer stores the address of the left child while the
right pointer stores the address of the right child.
2. The leaf node contains null in its left and right pointers.
3. There is a special pointer maintained in the memory which
points to the root node of the tree. 23
ASET

Note: if we have n nodes in a


binary tree then total number of
null links are n+1

24
Quick Revision ASET

If two trees have same structure and but different node content, then they are called ___
(A) Synonyms trees
(B) Joint trees
(C) Equivalent trees
(D) Similar trees

If two trees have same structure and node content, then they are called ____
(A) Synonyms trees
(B) Joint trees
(C) Equivalent trees
(D) Similar trees

What is/are the disadvantages of implementing tree using normal arrays?


(A) difficulty in knowing children nodes of a node
(B) difficult in finding the parent of a node
(C) have to know the maximum number of nodes possible before creation of trees
(D) difficult to implement
Quick Revision ASET

Advantages of linked list representation of binary trees over arrays?


(A) dynamic size
(B) ease of insertion/deletion
(C) ease in randomly accessing a node
(D) both dynamic size and ease in insertion/deletion

The number of edges from the root to the node is called __________ of the tree.
(A) Height
(B) Depth
(C) Length
(D) Width

What is a complete binary tree?


(A) Each node has exactly zero or two children
(B) A binary tree, which is completely filled, with the possible exception of the bottom
level, which is filled from right to left
(C) A binary tree, which is completely filled, with the possible exception of the bottom
level, which is filled from left to right
(D) A tree In which all nodes have degree 2
ASET

Thank You..☺

27
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Understand the concept of binary tree traversal
• Illustrate different types of binary tree traversals
with an example
– Pre-order traversal
– In-order traversal
– Post-order traversal
– Level-order traversal
Binary Tree Traversal ASET

• Visiting the root node N

• Traversing its left sub-tree L

• Traversing its right sub-tree R

NLR LNR LRN

Post-order
In-order

Pre-order

6
Binary Tree Traversal ASET

• Pre-order Traversal
– Traverse the root first then traverse into the left sub-tree and right
sub-tree respectively.
– This procedure will be applied to each sub-tree of the tree recursively.
• In-order Traversal
– Traverse the left sub-tree first, and then traverse the root and the
right sub-tree respectively.
– This procedure will be applied to each sub-tree of the tree recursively.
• Post-order Traversal
– Traverse the left sub-tree and then traverse the right sub-tree and
root respectively.
– This procedure will be applied to each sub-tree of the tree recursively
• Level order traversal
– Nodes are traversed level by level

7
Pre-Order Traversal ASET

Traverse the root first then


traverse into the left sub-
tree and right sub-tree
respectively.

8
Pre-order traversal ASET

• Example 2

9
Algorithm for Pre-order ASET

traversal
Step 1: Repeat Steps 2 to 4 while TREE !=
NULL
Step 2: Write TREE -> DATA
Step 3: PREORDER(TREE -> LEFT)
Step 4: PREORDER(TREE -> RIGHT)
[END OF LOOP]
Step 5: END
void preorder(struct treenode *tree)
{
if(tree != NULL)
{
printf("%d",tree→ root);
pre-order(tree→ left);
pre-order(tree→ right);
}
}
10
In-order Traversal ASET

Traverse the left sub-tree first, and


then traverse the root and the right
sub-tree respectively.

11
ASET

• Example 2

12
Algorithm for In-order ASET

traversal
Step 1: Repeat Steps 2 to 4 while TREE !=
NULL
Step 2: INORDER(TREE -> LEFT)
Step 3: Write TREE -> DATA
Step 4: INORDER(TREE -> RIGHT)
[END OF LOOP]
Step 5: END
void in-order(struct treenode *tree)
{
if(tree != NULL)
{
in-order(tree→ left);
printf("%d",tree→ root);
in-order(tree→ right);
}
}
13
Post order traversal ASET

Traverse the left sub-


tree and then traverse
the right sub-tree and
root respectively.

14
ASET

15
Algorithm for Post-order ASET

traversal
Step 1: Repeat Steps 2 to 4 while TREE !=
NULL
Step 2: POSTORDER(TREE -> LEFT)
Step 3: POSTORDER(TREE -> RIGHT)
Step 4: Write TREE -> DATA
[END OF LOOP]
Step 5: END
void post-order(struct treenode *tree)
{
if(tree != NULL)
{
post-order(tree→ left);
post-order(tree→ right);
printf("%d",tree→ root);
}
}
16
Level order traversal ASET

Level 0

Level 1

Level 2

Level 3

D E T Y K S H J

Note: Nodes are visited from left to


right
We can implement it using a queue
17
Level order traversal ASET

using Queue
1. Insert a root node in the queue
2. Delete a node from the queue and visit it
3. Insert left child of visited node in the queue
4. Insert right child of the visited node n the queue
5. Repeat steps 2,3 and 4 till queue becomes empty

Queue D K
YE TS

Level order
traversal
D E T

REPEAT TILL WE GET - D E T Y K S H J

18
ASET

1. Insert a root node in the


queue
2. Delete a node from the
queue and visit it
3. Insert left child of visited
node in the queue
4. Insert right child of the
visited node n the queue
5. Repeat steps 2,3 and 4 till
queue becomes empty

19
Height of a binary tree ASET

Height of a binary tree= 1+greater (hL,hR)

We need to find the height of all the sub trees till


we reach root

Base case: Hight of empty tree is 0

20
Function for finding height ASET
of tree

21
Quick Revision ASET

1. For the tree below, write the pre-order traversal.

a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9

2. Consider the following data. The pre order traversal of a binary tree is A, B, E, C, D.
The in order traversal of the same binary tree is B, E, A, D, C. The level order sequence
for the binary tree is _________
a) A, C, D, B, E
b) A, B, C, D, E
c) A, B, C, E, D
d) D, B, E, A, C
Quick Revision ASET

3. Consider the following data and specify which one is Preorder Traversal Sequence,
Inorder and Postorder sequences.
S1: N, M, P, O, Q
S2: N, P, Q, O, M
S3: M, N, O, P, Q
a) S1 is preorder, S2 is inorder and S3 is postorder
b) S1 is inorder, S2 is preorder and S3 is postorder
c) S1 is inorder, S2 is postorder and S3 is preorder
d) S1 is postorder, S2 is inorder and S3 is preorder

4. For the tree below, write the level-order traversal.

a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 11, 9, 6, 5, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
Quick Revision ASET

5. In a binary search tree, which of the following traversals would print the numbers in
the ascending order?
a) Level-order traversal
b) Pre-order traversal
c) Post-order traversal
d) In-order traversal

6. Find the postorder traversal of the binary tree shown below.

a) P Q R S T U V W X
b) W R S Q P V T U X
c) S W T Q X U V R P
d) S T W U X V Q R P
ASET

Thank You..☺

25
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Understand the concept of binary search tree
• Illustrate the various traversals in binary search
tree
• Understand operations in binary search tree
– Insertion of a node
– Deletion of a node
– Searching of a node
Binary Search Tree ASET

Binary Search tree can be defined as a class of binary trees, in which the
nodes are arranged in a specific order.
This is also called ordered binary tree.

Binary tree is a tree which is either empty or has following properties:

1.In a binary search tree, the value of all the nodes in the left sub-tree is
less than the value of the root.
2.Similarly, value of all the nodes in the right sub-tree is greater than or
equal to the value of the root.
3.This rule will be recursively applied to all the left and right sub-trees of
the root.
4.Left and right sub-trees are also binary tree

6
Example ASET

7
Traversal in a binary ASET

search tree

Preorder: 70 40 35 30 37 50 55 80 75 89 82 93
(Traverse the root first then traverse into
the left sub-tree and right sub-tree
respectively.)
8
Traversal in a binary ASET

search tree

Postorder: 30 37 35 55 50 40 75 82 93 89 80 70
(Traverse the left sub tree first then
traverse into the right sub-tree and then
root.)
9
Traversal in a binary ASET

search tree

Inorder: 30 35 37 40 50 55 70 75 80 82 89 93
(Traverse the left sub tree first then root
and then right sub-tree respectively.)

10
Traversal in a binary ASET

search tree

level-order: 70 40 80 35 50 75 89 30 37 55 82 93
(Traverse each level from left to right)

11
Searching in a Binary ASET

Search Tree
Let x is the key to be searched.
Search 37 in the given tree
Start at the root node and move down the tree
Search 77 in the given tree
• If x is equal to the key in the current node
Search is successful

• If x is less than the key in the current node


Move to left child
• If x is less than the key in the current node

Move to left child

If we reach a Null left child or Null right child


Search is unsuccessful
12
Advantages of using binary ASET

search tree
1. Searching become very efficient in a binary search tree since, we get a
hint at each step, about which sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare
to arrays and linked lists.
3. In searching process, it removes half sub-tree at every step.
4. Searching for an element in a binary search tree takes o(log2n) time. In
worst case, the time it takes to search an element is 0(n).
5. It also speed up the insertion and deletion operations as compare to that
in array and linked list.

13
Iterative function for ASET

binary search tree


p= search(root,x);
if (p==NULL) struct node *search(struct node *p, int x)
printf(“key not found\n”); {
else while(p!=NULL)
printf(“key found\n”); {
if (x< p→info)
p=p→ lchild;
else if (x> p→info)
p=p→ rchild;
else
return p;
}
return NULL;
}

14
14
Recursive function for searching ASET
in a binary search tree
p= search(root,x);
if (p==NULL) struct node *search(struct node *p, int x)
printf(“key not found\n”); {
else if(p==NULL)
printf(“key found\n”); return NULL;
if (x< p→info)
return search(p=p→ lchild,x);
if (x> p→info)
return search(p=p→ rchild,x);
return p;
}

15
Nodes with Minimum and ASET

Maximum Key
• Nodes with smallest key
– Last node in the leftmost path starting from root
• Node with largest Key
– Last node in the rightmost path starting from root

16
Example ASET

17
Iterative function for Nodes with
ASET
Minimum and Maximum Key

18
Recursive function for Nodes with
ASET
Minimum and Maximum Key

19
Insertion in a binary search ASET

tree
1. Allocate the memory for tree.
2. Set the data part to the value and set the left and right pointer of
tree, point to NULL.
3. If the item to be inserted, will be the first element of the tree,
then the left and right of this node will point to NULL.
4. Else, check if the item is less than the root element of the tree, if
this is true, then recursively perform this operation with the left of
the root.
5. If this is false, then perform this operation recursively with the
right sub-tree of the root.

20
ASET

Insert (TREE, ITEM)

Step 1:
IF TREE = NULL
Allocate memory for TREE
SET TREE -> DATA = ITEM
SET TREE -> LEFT = TREE -> RIGHT = NULL
ELSE
IF ITEM < TREE -> DATA
Insert(TREE -> LEFT, ITEM)
ELSE
Insert(TREE -> RIGHT, ITEM)
[END OF IF]
[END OF IF]
Step 2:
END

21
ASET

22
Deletion in a binary search ASET

tree
• The node to be deleted is a leaf node
– Replace the leaf node with the NULL and simple free the
allocated space.

23
ASET

• The node to be deleted has only one child.


– replace the node with its child and delete the child
node, which now contains the value which is to be
deleted. Simply replace it with the NULL and free the
allocated space.

24
ASET
• The node to be deleted has two children.
– the node which is to be deleted, is replaced with its in-order successor
or predecessor recursively until the node value (to be deleted) is placed
on the leaf of the tree.
– After the procedure, replace the node with NULL and free the allocated
space.
– Example
• the node 50 is to be deleted which is the root node of the tree. The in-order traversal of
the tree given below.
6, 25, 30, 50, 52, 60, 70, 75.
• replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree,
which will simply be deleted.

25
ASET

Delete (TREE, ITEM)


Step 1: IF TREE = NULL
Write "item not found in the tree" ELSE IF ITEM < TREE -> DATA
Delete(TREE->LEFT, ITEM)
ELSE IF ITEM > TREE -> DATA
Delete(TREE -> RIGHT, ITEM)
ELSE IF TREE -> LEFT AND TREE -> RIGHT
SET TEMP = findLargestNode(TREE -> LEFT)
SET TREE -> DATA = TEMP -> DATA
Delete(TREE -> LEFT, TEMP -> DATA)
ELSE
SET TEMP = TREE
IF TREE -> LEFT = NULL AND TREE -> RIGHT = NULL
SET TREE = NULL
ELSE IF TREE -> LEFT != NULL
SET TREE = TREE -> LEFT
ELSE
SET TREE = TREE -> RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
Step 2: END 26
ASET

Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50

• Insert 43 into the tree as the root of the tree.


• Read the next element, if it is lesser than the root node
element, insert it as the root of the left sub-tree.
• Otherwise, insert it as the root of the right of the right sub-
tree.

27
ASET

28
ASET

29
Quick Revision ASET

1. Which of the following is false about a binary search tree?


a) The left child is always lesser than its parent
b) The right child is always greater than its parent
c) The left and right sub-trees should also be binary search trees
d) In order sequence gives decreasing order of elements

2. What is the specialty about the in-order traversal of a binary search tree?
a) It traverses in a non increasing order
b) It traverses in an increasing order
c) It traverses in a random fashion
d) It traverses based on priority of the node

3. Is this a binary search tree?

a) True
b) False
Quick Revision ASET

1. Which of the following is false about a binary search tree?


a) The left child is always lesser than its parent
b) The right child is always greater than its parent
c) The left and right sub-trees should also be binary search trees
d) In order sequence gives decreasing order of elements

2. What is the specialty about the in-order traversal of a binary search tree?
a) It traverses in a non increasing order
b) It traverses in an increasing order
c) It traverses in a random fashion
d) It traverses based on priority of the node

3. Is this a binary search tree?

a) True
b) False
Quick Revision ASET

4. The height of a BST is given as h. Consider the height of the tree as the no. of
edges in the longest path from root to the leaf. The maximum no. of nodes possible
in the tree is?
a) 2h-1 -1
b) 2h+1 -1
c) 2h +1
d) 2h-1 +1

5. A binary search tree is generated by inserting in order the following integers:


50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
The number of the node in the left sub-tree and right sub-tree of the root, respectively, is

a) (4, 7)
b) (7, 4)
c) (8, 3)
d) (3, 8)
ASET

Thank You..☺

33
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Define AVL Tree
• Understands and calculate the balance factor in
AVL Tree
• Illustrate AVL Tree rotations and various
operations with example
AVL Tree ASET

• AVL Tree given invented by GM Adelson - Velsky and EM


Landis in 1962. The tree is named AVL in honor of its
inventors.

• AVL Tree can be defined as height balanced binary search


tree in which each node is associated with a balance factor

• Balance factor is calculated by subtracting the height of its


right sub-tree from that of its left sub-tree.

Balance Factor (k) = height (left(k)) - height (right(k))

• Tree is said to be balanced if balance factor of each node is


in between -1 to 1, otherwise, the tree will be unbalanced
and need to be balanced.

6
ASET

The balance factor associated with each


node is in between -1 and +1. therefore, it
is an example of AVL tree.

7
General Discussion on
ASET
Balance Factor
Balance Factor (k) = height (left(k)) - height (right(k))

Balance Remark
factor
1 The left sub-tree is one level
higher than the right sub-tree.

0 The left sub-tree and right sub-


tree contain equal height.

-1 the left sub-tree is one level lower


than the right sub-tree.

8
ASET

Complexity
Algorithm Average case Worst case
Space o(n) o(n)
Search o(log n) o(log n)
Insert o(log n) o(log n)
Delete o(log n) o(log n)

Note:
• AVL tree controls the height of the binary search tree by not letting
it to be skewed.
• The time taken for all operations in a binary search tree of height h
is O(h). However, it can be extended to O(n) if the BST becomes
skewed (i.e. worst case).
• By limiting this height to log n, AVL tree imposes an upper bound on
each operation to be O(log n) where n is the number of nodes.
9
Operations on AVL tree ASET

Note:
• AVL tree is also a binary search tree therefore, all
the operations are performed in the same way as
they are performed in a binary search tree.
• Searching and traversing do not lead to the
violation in property of AVL tree.
• Insertion and deletion are the operations which can
violate this property and therefore, they need to be
revisited.
• Every AVL Tree is a binary search tree but every
Binary Search Tree need not be AVL tree.

10
Insertion In AVL Tree ASET

• Insertion in AVL tree is performed in the same way as it is performed in a


binary search tree.
• The new node is added into AVL tree as the leaf node. However, it may
lead to violation in the AVL tree property and therefore the tree may need
balancing.
• The tree can be balanced by applying rotations.
• Rotation is required only if, the balance factor of any node is disturbed upon
inserting the new node, otherwise the rotation is not required.
• Depending upon the type of insertion, the Rotations are categorized into
four categories.
– LL Rotation
– RR Rotation
– LR Rotation
– RL Rotation

11
AVL Tree Rotations ASET
• 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.
There are four rotations and they are classified into two types.

12
Single Left Rotation (LL Rotation) ASET

• In LL Rotation, every node moves one position to left from the current
position. To understand LL Rotation, let us consider the following
insertion operation in AVL Tree.

13
Single Right Rotation (RR Rotation) ASET

• In RR Rotation, every node moves one position to right from the


current position. To understand RR Rotation, let us consider the
following insertion operation in AVL Tree.

14
Left Right Rotation (LR Rotation) ASET

• The LR Rotation is a sequence of single left rotation followed by a


single right rotation. In LR Rotation, at first, every node moves one
position to the left and one position to right from the current position. To
understand LR Rotation, let us consider the following insertion
operation in AVL Tree.

15
Right Left Rotation (RL Rotation) ASET

• The RL Rotation is sequence of single right rotation followed by single


left rotation. In RL Rotation, at first every node moves one position to
right and one position to left from the current position. To understand
RL Rotation, let us consider the following insertion operation in AVL
Tree.

16
Operations on an AVL Tree
ASET

• The following operations are performed on


AVL tree.
– Search
– Insertion
– Deletion

17
Search Operation in AVL Tree
ASET

18
Insertion Operation in AVL Tree
ASET

19
Example ASET

• Construct an AVL Tree by inserting numbers from 1 to 8.

20
Example ASET

• Construct an AVL Tree by inserting numbers from 1 to 8.

21
Example ASET
• Construct an AVL Tree by inserting numbers from 1 to 8.

22
Example ASET
• Construct an AVL Tree by inserting numbers from 1 to 8.

23
Deletion in AVL Tree ASET

• Deleting a node from an AVL tree is similar to that in a


binary search tree.
• Deletion may disturb the balance factor of an AVL tree and
therefore the tree needs to be rebalanced in order to
maintain the property of AVL.
• For this purpose, we need to perform rotations.
• The two types of rotations are
– L rotation and R rotation.
• If the node which is to be deleted is present in the left sub-
tree of the critical node, then L rotation needs to be applied
else if, the node which is to be deleted is present in the
right sub-tree of the critical node, the R rotation will be
applied.

24
R0 rotation (Node B has
ASET
balance factor 0 )
• If the node B has 0 balance factor, and the balance factor of node A disturbed
upon deleting the node X, then the tree will be rebalanced by rotating tree
using R0 rotation.
• The critical node A is moved to its right and the node B becomes the root of
the tree with T1 as its left sub-tree. The sub-trees T2 and T3 becomes the left
and right sub-tree of the node A.

25
ASET

Example: Delete the node 30 from the AVL tree


shown in the following image.

26
ASET

• In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image.
• The node B(10) becomes the root, while the node A is moved to its
right. The right child of node B will now become the left child of node A.

27
R1 Rotation (Node B has ASET

balance factor 1)
• R1 Rotation is to be performed if the balance factor of Node B is 1.
• In R1 rotation, the critical node A is moved to its right having sub-trees
T2 and T3 as its left and right child respectively.
• T1 is to be placed as the left sub-tree of the node B.

28
ASET

Example: Delete Node 55 from the AVL tree shown in the following
image.

29
ASET

• Deleting 55 from the AVL Tree disturbs the balance factor of the node 50
i.e. node A which becomes the critical node.
• This is the condition of R1 rotation in which, the node A will be moved to
its right (shown in the image below).
• The right of B is now become the left of A (i.e. 45).

30
R-1 Rotation (Node B has balance
ASET
factor -1)
• R-1 rotation is to be performed if the node B has balance factor -1.
• This case is treated in the same way as LR rotation.
• In this case, the node C, which is the right child of node B, becomes the
root node of the tree with B and A as its left and right child respectively.
• The sub-trees T1, T2 becomes the left and right sub-trees of B whereas,
T3, T4 become the left and right sub-trees of A.

31
ASET

Example: Delete the node 60 from the AVL tree shown


in the following image.

32
ASET

• The node B has balance factor -1. Deleting the node 60, disturbs the
balance factor of the node 50 therefore, it needs to be R-1 rotated. The
node C i.e. 45 becomes the root of the tree with the node B(40) and A(50)
as its left and right child.

33
Quick Revision ASET

1. The balance factor of a node in a binary tree is defined as _____


a) addition of heights of left and right subtrees
b) height of right subtree minus height of left subtree
c) height of left subtree minus height of right subtree
d) height of right subtree minus one

2. Figure below is a balanced binary tree. If a node inserted as child of the node R,
how many nodes will become unbalanced?
a) 2
b) 1
c) 3
d) 0

3. A binary tree is balanced if the difference between left and right subtree of every
node is not more than ____
a) 1
b) 3
c) 2
d) 0
Quick Revision ASET

4. The figure shown below is a balanced binary tree. If node P is deleted, which of the
following nodes will get unbalanced?
a) U
b) M
c) H
d) A

5. What will be the height of a balanced full binary tree with 8 leaves?
a) 8
b) 5
c) 6
d) 4
ASET

Thank You…☺

36
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Define Threaded Binary Tree
• Understands one-way and two-way
representation of Threaded Binary Tree
• Illustrate the traversals of Threaded Binary Tree
• Differentiate between Threaded Binary Tree vs
Binary Tree
• Understand the advantages and disadvantages
of Threaded Binary Tree
Threaded Binary Tree ASET

• Definition: A binary search tree in which each node


uses an otherwise-empty left child link to refer to the
node's in-order predecessor and an empty right child link
to refer to its in-Order Successor.

6
Threaded Binary Tree ASET

7
Threaded Binary Tree ASET

• In above binary tree, there are 8 null pointers & actual 6 pointers.
• In all there are 14 pointers.
• We can generalize it that for any binary tree with n nodes there
will be (n+1) null pointers and 2n total pointers.
• The objective here to make effective use of these null pointers.
• A. J. perils & C. Thornton jointly proposed idea to make effective
use of these null pointers.
• According to this idea we are going to replace all the null pointers
by the appropriate pointer values called threads.
• And binary tree with such pointers are called threaded tree.
• In the memory representation of a threaded binary tree, it is
necessary to distinguish between a normal pointer and a thread.

8
Threaded Binary Tree: One way
ASET

• We will use the right thread only in this case.

• To implement threads we need to use in-order successor of


the tree

9
Threaded Binary Tree: One way
ASET

10
Threaded Binary Tree: Two way
ASET

• Again two-way threading has left pointer of the


first node and right pointer of the last node will
contain the null value.

• The header nodes is called two-way threading


with header node threaded binary tree.

11
Threaded Binary Tree: Two way
ASET

Inorder Traversal of The tree: D, B, E, A, F, C, G

12
Threaded Binary Tree: Two way
ASET

Dangling can be solved as follows:

• Introduce a header node.

• The left and right pointer of the header node are


treated as normal links and are initialized to point to
header node itself.

13
Threaded Binary Tree: Two way
ASET

Inorder Traversal of The tree: D, B, E, A, F, C, G

14
Threaded Binary Tree: Two way
ASET

Structure of Thread BT
• Node structure
– For the purpose of our evaluation algorithm, we assume each node
has five fields:

15
Threaded Binary Tree: Two way
ASET

16
Threaded Binary Tree Traversal
ASET

• We start at the leftmost node in the tree, print it,


and follow its right thread

• If we follow a thread to the right, we output the


node and continue to its right.

• If we follow a link to the right, we go to the


leftmost node, print it, and continue.

17
Threaded Binary Tree Traversal
ASET

18
Threaded Binary Tree Traversal
ASET

19
Threaded Binary Tree Traversal
ASET

20
Threaded Binary Tree Traversal
ASET

21
Threaded Binary Tree Traversal
ASET

22
Threaded Binary Tree Traversal
ASET

23
Threaded Binary Tree Traversal
ASET

24
Threaded Binary Tree Traversal
ASET

25
Comparison of Threaded BT
ASET

26
Threaded BT
ASET

27
Quick Revision ASET

1. What is a threaded binary tree traversal?


a) a binary tree traversal using stacks
b) a binary tree traversal using queues
c) a binary tree traversal using stacks and queues
d) a binary tree traversal without using stacks and queues

2. What are the disadvantages of normal binary tree traversals?


a) there are many pointers which are null and thus useless
b) there is no traversal which is efficient
c) complexity in implementing
d) improper traversals

3. In general, the node content in a threaded binary tree is ________


a) leftchild_pointer, left_tag, data, right_tag, rightchild_pointer
b) leftchild_pointer, left_tag
c) leftchild_pointer, left_tag, right_tag, rightchild_pointer
d) leftchild_pointer, left_tag, data
Quick Revision ASET

4. What are null nodes filled with in a threaded binary tree?


a) inorder predecessor for left node and inorder successor for right node information
b) right node with inorder predecessor and left node with inorder successor information
c) they remain null
d) some other values randomly

5. What is inefficient with the below threaded binary tree picture?

a) it has dangling pointers


b) nothing inefficient
c) incorrect threaded tree
d) space is being used more
ASET

Thank You…☺

30
ASET

Amity School of Engineering &


Technology
Programme: B.Tech, Semester- III
Data Structure Using C

Dr. Deepak Sharma


Assistant Professor
ASET,CSE
1
ASET

Module-IV
Trees
Syllabus ASET

• Basic Terminology, Binary Trees and their


representation, expression evaluation,
Complete Binary trees, extended binary
trees, Traversing binary trees, Searching,
Insertion and Deletion in binary search
trees, General trees, AVL trees, Threaded
trees, B trees
ASET

• Books & Reference links:


– E. Balagurusamy, “Problem Solving through C language”, TMH
publication, Fourth Edition, 2008.
– R.S Salaria ,“Data Structures & Algorithms using C”,Khanna
Publication,4th Edition,2009
– E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C
“,2nd Edition,Universities Press,2008.
– Data Structures With C (Sie) (Sos). (n.d.). India: McGraw-Hill
Education (India) Pvt Limited.
– https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763
AF2E1C572F
– https://www.youtube.com/watch?v=RBSGKlAvoiM

4
Student Learning Outcomes: ASET

At the end of this presentation, Student should be


able to :
• Understand the concept of B-Tree
• Illustrate the operations on B-Tree
– Insertion
– Deletion
• Illustrate the construction of B-Tree with an
example
B - Tree ASET

• 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.

6
B - Tree ASET

• 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.

7
B - Tree ASET

• 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

8
Operations on a B-Tree ASET

• The following operations are


performed on a B-Tree.
– Search
– Insertion
– Deletion

9
Search Operation in B-Tree
ASET

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. 10
Insertion Operation in B-Tree
ASET

• 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.
11
Example ASET

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

12
Example ASET

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

13
Example ASET

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

14
Example ASET

15
Example ASET

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

16
Quick Revision ASET

1. B-tree of order n is a order-n multiway tree in which each non-root node contains
__________
a) at most (n – 1)/2 keys
b) exact (n – 1)/2 keys
c) at least 2n keys
d) at least (n – 1)/2 keys

2. B-tree and AVL tree have the same worst case time complexity for insertion and
deletion.
a) True
b) False
3. Statement 1: When a node is split during insertion, the middle key is promoted to the
parent as well as retained in right half-node.
Statement 2: When a key is deleted from the leaf, it is also deleted from the non-leaf
nodes of the tree.
1. Statement 1 is true but statement 2 is false
2. Statement 2 is true but statement 1 is false
3. Both the statements are true
4. Both the statements are false
ASET

Thank You…☺

18
ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

1
ASET

Module-V
Searching & Sorting Techniques
Contents to be Covered
ASET

Searching and Sorting Techniques

➢ Insertion Sort
➢ Bubble Sort
➢ Selection Sort
Learning Objectives ASET

➢ Impart in-depth knowledge of various sorting algorithms.

➢ Implement insertion sort, bubble sort and selection sort algorithms

for complex problems.


Insertion Sort ASET
ASET
ASET
ASET
Insertion Sort algorithm ASET

for(i=1; i<n; i++)


{
key = a[i];
j = i-1;
while(j>=0 && key < a[j])
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
ASET

Sort this using Insertion Sort


8 12 5 4 6 9 3
ASET

Sort this using Insertion Sort


8 12 5 4 6 9 3
8 12 5 4 6 9 3
8 12 5 4 6 9 3

8 12 4 6 9 3

8 12 4 6 9 3
ASET

Sort this using Insertion Sort


5 8 12 4 6 9 3
4 5 8 12 6 9 3
4 5 6 8 12 9 3

4 5 6 8 9 12 3

3 4 5 6 8 9 12
Bubble Sort algorithm
ASET

• Bubble sort is a simple sorting algorithm.


• This sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is compared and the
elements are swapped if they are not in order.
• This algorithm is not suitable for large data sets as its
average and worst case complexity are of Ο(n2) where n is
the number of items.
• How Bubble Sort Works?
• We take an unsorted array for our example. Bubble sort
takes Ο(n2) time so we're keeping it short and precise.

• Bubble sort starts with very first two elements, comparing


them to check which one is greater.
ASET
ASET
ASET
Bubble Sort Algorithm ASET

for(i=0; i<n; i++)


{
for(j=0; j<n-i-1; j++)
{
if( a[j] > a[j+1])
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
Lets Practice ASET
ASET

Sort this using Bubble Sort


8 12 5 4 6 9 3
ASET

Sort this using Bubble Sort


8 12 5 4 6 9 3
8 5 4 6 9 3 12
5 4 6 8 3 9 12

4 5 6 3 8 9 12

4 5 3 6 8 9 12
ASET

Sort this using Bubble Sort


4 3 5 6 8 9 12

3 4 5 6 8 9 12

3 4 5 6 8 9 12
Selection Sort ASET
ASET
ASET
ASET
Work for you…. ASET

– You have been given a pack of cards (Only


Spades) numbered 1 to 13 (Ace, 2 to 10,
Jack Queen, King) in random order. Write a
program to sort these cards. Use Bubble sort
and print each line of output after every
iteration.
– For the same above problem, Write the
program using Insertion sort and print each
line of output after every iteration.
– Compare the number of steps for each sorting
technique
Web Resources ASET

• https://www.cs.usfca.edu/~galles/visualization/Search.html
(Final) for linear search and binary search
• https://blog.penjee.com/binary-vs-linear-search-animated-gifs/
(for linear search and binary search comparisons)
• https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
• http://www.cs.armstrong.edu/liang/animation/web/BubbleSort.
html (Bubble Sort)
• http://www.ee.ryerson.ca/~courses/coe428/sorting/bubblesort.
html (bUBBLE SORT)
• https://courses.cs.vt.edu/csonline/Algorithms/Lessons/Insertio
nCardSort/insertioncardsort.swf (Insertion sort final)
• http://www.ee.ryerson.ca/~courses/coe428/sorting/insertionso
rt.html (INSERTION SORT ADDITIONAL)
ASET

Thank you

29
ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

1
ASET

Module-V
Searching & Sorting Techniques
Contents to be Covered
ASET

Searching and Sorting Techniques

➢ Quick Sort
➢ Merge Sort
➢ Heap Sort
Learning Objectives ASET

➢ Impart in-depth knowledge of various sorting algorithms.

➢ Implement quick sort, merge sort and heap sort algorithms for

complex problems.
Quick Sort ASET

• Efficient sorting algorithm


• Example of Divide and Conquer algorithm
• Two phases
– Partition phase
• Divides the work into half
– Sort phase
• Conquers the halves!

5
Quicksort ASET

• Partition
– Choose a pivot
– Find the position for the pivot so that
• all elements to the left are less / equal
• all elements to the right are equal / greater

< =pivot pivot => pivot

6
Quicksort ASET

• Conquer
– Apply the same algorithm to each half
< pivot > pivot

<= p’ p’ => p’ pivot < =p” p” => p”


ASET
46, 33, 26, 47, 17, 19, 35, 43, 42, 18, 47, 23, 12, 39, 49, 14, 32, 19, 27, 35

8
ASET
46, 33, 26, 47, 17, 19, 35, 43, 42, 18, 47, 23, 12, 39, 49, 14, 32, 19, 27, 35

9
ASET
46, 33, 26, 47, 17, 19, 35, 43, 42, 18, 47, 23, 12, 39, 49, 14, 32, 19, 27, 35

10
ASET
46, 33, 26, 47, 17, 19, 35, 43, 42, 18, 47, 23, 12, 39, 49, 14, 32, 19, 27, 35

11
ASET

12
ASET

13
ASET

14
ASET

15
ASET

16
ASET

17
ASET

18
ASET

19
ASET

20
ASET

21
ASET

22
ASET

23
ASET

24
Quicksort ASET

• Implementation
quicksort( void *a, int low, int high )
{
int pivot;
/* Termination condition! */
if ( high > low )
{
pivot = partition( a, low, high ); Divide
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high ); Conquer
}
}
Quicksort - Partition
ASET
int partition( int *a, int low, int high ) {
int left, right;
int pivot_item;
pivot_item = a[low];
pivot = left = low;
right = high;
while ( left < right ) {
/* Move left while item < pivot */
while( a[left] <= pivot_item ) left++;
/* Move right while item > pivot */
while( a[right] > pivot_item ) right--;
if ( left < right ) {
SWAP(a,left,right); left++; right--;
}
}
/* right is final position for the pivot */
a[low] = a[right];
a[right] = pivot_item;
return right;
} 26
Quicksort - Analysis ASET

• Partition
– Check every item once O(n)
• Conquer
– Divide data in half O(log2n)
• Total
– Product O(n log n)
• Same as Heapsort
– quicksort is generally faster
• Fewer comparisons

27
Quicksort vs Heap Sort ASET

 Quicksort
◦ Generally faster
◦ Sometimes O(n2)
 Better pivot selection reduces probability
◦ Use when you want average good performance
 Commercial applications, Information systems
 Heap Sort
◦ Generally slower
◦ Guaranteed O(n log n)

28
Merge Sort - Definition ASET

• Definition: A sort algorithm that splits


the items to be sorted into two groups,
recursively sorts each group, and merge
them into a final sorted sequence. Run
time is O (n log n).

29
Merge Sort – Divide-and- ASET

Conquer
 Divide-and-conquer is a general algorithm
design paradigm:
◦ Divide: divide the input data S in two disjoint
subsets S1 and S2
◦ Conquer: solve the sub-problems associated
with S1 and S2 recursively
◦ Combine: combine the solutions for S1 and
S2 into a solution for S
 The base case for the recursion are sub-
problems of size 0 or 1

30
Merge Sort Tree ASET

• An execution of merge-sort is depicted by a


binary tree
– each node represents a recursive call of merge-
sort and stores
• unsorted sequence before the execution
• sorted sequence at the end of the execution
– the root is the initial call
– the leaves are calls on subsequences of size 0 or
1

31
Merge Sort - example ASET

Partition

7294|3861

32
Merge Sort - example ASET

Recursive call, partition

7294|3861

72|94

33
Merge Sort - example ASET

Recursive call, partition

7294|3861

72|94

7|2

34
Merge Sort - example ASET

Recursive call, base case

7294|3861

72|94

7|2

7→7

35
Merge Sort - example ASET

Recursive call, base case

7294|3861

72|94

7|2

7→7 2→2

36
Merge Sort - example ASET

Merge

7294|3861

72|94

7|2 → 2 7

7→7 2→2

37
Merge Sort - example ASET

Recursive call, base case, …, base case, merge

7294|3861

72|94

7|2 → 2 7 9|4 → 4 9

7→7 2→2 9→9 4→4

38
Merge Sort - example ASET

Merge

7294|3861

72|94 → 2 4 7 9

7|2 → 2 7 9|4 → 4 9

7→7 2→2 9→9 4→4

39
Merge Sort - example ASET

Recursive call, …, merge, merge

7294|3861

72|94 → 2 4 7 9 3 8 6 1→1 3 8 6

7|2 → 2 7 9|4 → 4 9 38→38 61→16

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

40
Merge Sort - example ASET

Merge

7294|3861 → 12346789

72|94 → 2 4 7 9 3 8 6 1→1 3 8 6

7|2 → 2 7 9|4 → 4 9 38→38 61→16

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

41
Merge Sort - analysis ASET

• The height h of the merge-sort tree is log n + 1 = O(log n)


– at each recursive call we divide in half the
sequence
• The overall amount or work done at the nodes of depth i is O(n)
– we partition and merge 2i sequences of size
n/2i
– we make 2i+1 recursive calls
• Thus, the total running time of merge-sort is O(n log n)

42
Merge Sort – sample ASET

code
void mergesort(int low, int
high)
{
if (low<high)
{
int middle=(low+high)/2;
mergesort(low, middle);
mergesort(middle+1,
high);
merge(low, middle,
high); 43
ASET

Merge Sort – sample code


(cont.)
void merge(int low, int middle, int high)
{
int i, j, k;
// copy both halves of a to auxiliary array b
for (i=low; i<=high; i++)
b[i]=a[i];
i=low; j=middle+1; k=low;
// copy back next-greatest element at each time
while (i<=middle && j<=high)
if (b[i]<=b[j]) a[k++]=b[i++];
else a[k++]=b[j++];
// copy back remaining elements of first half
(if any)
while (i<=middle)
a[k++]=b[i++];
}

44
Heap data structure ASET

• Binary tree

• Balanced

• Left-justified or Complete

• (Max) Heap property: no node has a


value greater than the value in its
parent
45
Balanced binary trees ASET

• Recall:
– The depth of a node is its distance from the root
– The depth of a tree is the depth of the deepest
node
• A binary tree of depth n is balanced if all the
nodes at depths 0 through n-2 have two
children
n-2
n-1
n
Balanced Balanced Not balanced

46
Left-justified binary trees ASET

• A balanced binary tree of depth n is left-


justified if:
– it has 2n nodes at depth n (the tree is “full”), or
– it has 2k nodes at depth k, for all k < n, and all
the leaves at depth n are as far left as possible

Left-justified Not left-justified

47
Building up to heap sort ASET

• How to build a heap

• How to maintain a heap

• How to use a heap to sort data

48
The heap property ASET

• A node has the heap property if the value in the


node is as large as or larger than the values in
its children
12 12 12

8 3 8 12 8 14
Blue node has Blue node has Blue node does not
heap property heap property have heap property
• All leaf nodes automatically have the heap
property
• A binary tree is a heap if all nodes in it have
the heap property 49
shiftUp ASET

• Given a node that does not have the heap property,


you can give it the heap property by exchanging its
value with the value of the larger child

12 14

8 14 8 12
Blue node does not Blue node has
have heap property heap property

• This is sometimes called shifting up

50
Constructing a heap I ASET

• A tree consisting of a single node is


automatically a heap
• We construct a heap by adding nodes one at
a time:
– Add the node just to the right of the rightmost node
in the deepest level
– If the deepest level is full, start a new level
• Examples:
Add a new Add a new
node here node here

51
Constructing a heap II ASET

• Each time we add a node, we may destroy the


heap property of its parent node
• To fix this, we sift up
• But each time we sift up, the value of the
topmost node in the sift may increase, and this
may destroy the heap property of its parent node
• We repeat the sifting up process, moving up in
the tree, until either
– We reach nodes whose values don’t need to be
swapped (because the parent is still larger than both
children), or
– We reach the root

52
Constructing a heap III ASET

8 8 10 10

10 8 8 5

1 2 3

10 10 12

8 5 12 5 10 5

12 8 8
4

53
Other children are not affectedASET

12 12 14

10 5 14 5 12 5

8 14 8 10 8 10

• The node containing 8 is not affected because its parent gets larger,
not smaller
• The node containing 5 is not affected because its parent gets larger,
not smaller
• The node containing 8 is still not affected because, although its parent
got smaller, its parent is still greater than it was originally

54
A sample heap ASET

• Here’s a sample binary tree after it has been


heapified
25

22 17

19 22 14 15

18 14 21 3 9 11

• Notice that heapified does not mean sorted


• Heapifying does not change the shape of the binary
tree; this binary tree is balanced and left-justified
because it started out that way 55
Removing the root (animated) ASET

• Notice that the largest number is now in the root


• Suppose we discard the root:
11

22 17

19 22 14 15

18 14 21 3 9 11

• How can we fix the binary tree so it is once again


balanced and left-justified?
• Solution: remove the rightmost leaf at the deepest
level and use it for the new root 56
The reHeap method I ASET

• Our tree is balanced and left-justified, but no longer a


heap
• However, only the root lacks the heap property
11

22 17

19 22 14 15

18 14 21 3 9

• We can shiftDown() the root


• After doing this, one and only one of its children may
have lost the heap property
57
The reHeap method II ASET

• Now the left child of the root (still the number 11)
lacks the heap property
22

11 17

19 22 14 15

18 14 21 3 9

• We can siftDown() this node


• After doing this, one and only one of its children may
have lost the heap property
58
The reHeap method III ASET

• Now the right child of the left child of the root (still the
number 11) lacks the heap property:
22

22 17

19 11 14 15

18 14 21 3 9

• We can shiftDown() this node


• After doing this, one and only one of its children may
have lost the heap property —but it doesn’t, because
it’s a leaf 59
The reHeap method IV ASET

• Our tree is once again a heap, because every node


in it has the heap property
22

22 17

19 21 14 15

18 14 11 3 9

• Once again, the largest (or a largest) value is in the root


• We can repeat this process until the tree becomes empty
• This produces a sequence of values in order largest to
60
smallest
Sorting ASET

• What do heaps have to do with sorting an


array?
• Here’s the neat part:
– Because the binary tree is balanced and left
justified, it can be represented as an array
– All our operations on binary trees can be
represented as operations on arrays
– To sort:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
} 61
Key properties ASET

• Determining location of root and “last


node” take constant time

• Remove n elements, re-heap each time

62
Analysis ASET

• To reheap the root node, we have to follow


one path from the root to a leaf node (and
we might stop before we reach a leaf)
• The binary tree is perfectly balanced
• Therefore, this path is O(log n) long
– And we only do O(1) operations at each node
– Therefore, reheaping takes O(log n) times
• Since we reheap inside a while loop that
we do n times, the total time for the while
loop is n*O(log n), or O(n log n)
63
Analysis ASET

• Construct the heap O(n log n)

• Remove and re-heap O(log n)


– Do this n times O(n log n)

• Total time O(n log n) + O(n log n)


64
ASET

Thank you

65
ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

1
ASET

Module-V
Searching & Sorting Techniques
Contents to be Covered
ASET

Searching and Sorting Techniques

➢ Partition exchange sort


➢ Shell sort
➢ Sorting on different keys
➢ External sorting
Learning Objectives ASET

➢ Impart in-depth knowledge of various sorting algorithms, sorting

on different keys and external sorting.

➢ Implement shell sort algorithm for complex problems.


Partition Exchange Sort ASET

• Quicksort (sometimes called partition-


exchange sort) is an efficient sorting
algorithm, serving as a systematic method
for placing the elements of a random
access file or an array in order.
• (Refer to Quick Sort Algorithm)

5
Shell Sort ASET

• Shell sort is a highly efficient sorting algorithm and is


based on insertion sort algorithm.
• This algorithm avoids large shifts as in case of insertion
sort, if the smaller value is to the far right and has to be
moved to the far left.
• This algorithm uses insertion sort on a widely spread
elements, first to sort them and then sorts the less widely
spaced elements. This spacing is termed as interval.
This interval is calculated based on Knuth's formula as −
Knuth's Formula
h = h * 3 + 1 where − h is interval with initial value

6
Shell Sort ASET

• This algorithm is quite efficient for medium-sized data


sets as its average and worst-case complexity of this
algorithm depends on the gap sequence the best known
is Ο(n), where n is the number of items.
• And the worst case space complexity is O(n).

7
How shell sort works? ASET

• Let us consider the following example to have an idea of


how shell sort works.

• We take the same array we have used in our previous


examples. For our example and ease of understanding,
we take the interval of 4.

• Make a virtual sub-list of all values located at the interval


of 4 positions. Here these values are {35, 14}, {33, 19},
{42, 27} and {10, 44}

8
How shell sort works? ASET

• We compare values in each sub-list and swap them (if


necessary) in the original array. After this step, the new
array should look like this –

9
How shell sort works? ASET

• Then, we take interval of 1 and this gap generates two


sub-lists - {14, 27, 35, 42}, {19, 10, 33, 44}

• We compare and swap the values, if required, in the


original array. After this step, the array should look like
this −

10
How shell sort works? ASET

• Finally, we sort the


rest of the array using
interval of value 1.
Shell sort uses
insertion sort to sort
the array.

• Following is the step-


by-step depiction

• We see that it
required only four
swaps to sort the rest
of the array. 11
Shell sort algorithm ASET

Algorithm
Following is the algorithm for shell sort.
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller sub-list of equal
interval h
Step 3 − Sort these sub-lists using insertion sort
Step 4 − Repeat until complete list is sorted

12
Shell sort pseudo code ASET

13
Shell sort ASET

Key points of shell sort algorithm


• Shell Sort is a comparison based sorting.
• Time complexity of Shell Sort depends on gap sequence. Its best
case time complexity is O(n* logn) and worst case is O(n* log2n).
Time complexity of Shell sort is generally assumed to be near to O(n)
and less than O(n2) as determining its time complexity is still an open
problem.
• The best case in shell sort is when the array is already sorted. The
number of comparisons is less.
• It is an in-place sorting algorithm as it requires no additional scratch
space.
• Shell Sort is unstable sort as relative order of elements with equal
values may change.
• It is been observed that shell sort is 5 times faster than bubble sort
and twice faster than insertion sort its closest competitor.
• There are various increment sequences or gap sequences in shell
sort which produce various complexity between O(n) and O(n2). 14
Sort on different keys ASET

• Sorting refers to the operation or technique of arranging and


rearranging sets of data in some specific order. A collection of
records called a list where every record has one or more fields. The
fields which contain a unique value for each record is termed as
the key field.
• For example, a phone number directory can be thought of as a list
where each record has three fields - 'name' of the person, 'address'
of that person, and their 'phone numbers'. Being unique phone
number can work as a key to locate any record in the list. Sorting is
the operation performed to arrange the records of a table or list in
some order according to some specific ordering criterion. Sorting is
performed according to some key value of each record. The records
are either sorted either numerically or alphanumerically. The records
are then arranged in ascending or descending order depending on
the numerical value of the key. Here is an example, where the
sorting of a lists of marks obtained by a student in any particular
subject of a class. 15
Why Sort? ASET

• A classic problem in computer science!


• Data requested in sorted order
– e.g., find students in increasing gpa order
• Sorting is first step in bulk loading B+ tree
index.
• Sorting useful for eliminating duplicate copies
in a collection of records (Why?)
• Sort-merge join algorithm involves sorting.
• Problem: sort 1Gb of data with 1Mb of RAM.
– why not virtual memory?
ASET

2-Way Sort: Requires 3 Buffers


• Pass 1: Read a page, sort it, write it.
– only one buffer page is used
• Pass 2, 3, …, etc.:
– three buffer pages used.

INPUT 1

OUTPUT
INPUT 2

Main memory Disk


Disk
buffers
Two-Way External Merge SortASET
3,4 6,2 9,4 8,7 5,6 3,1 2 Input file
• Each pass we read + write PASS 0
each page in file. 3,4 2,6 4,9 7,8 5,6 1,3 2 1-page runs
PASS 1
• N pages in the file => the 2,3 4,7 1,3
2-page runs
4,6 8,9 5,6 2
number of passes PASS 2
=  log2 N  + 1 2,3
4,4 1,2
• So toal cost is:
4-page runs
6,7 3,5

( )
6
2 N  log 2 N  + 1
8,9
PASS 3

1,2
• Idea: Divide and 2,3
3,4
conquer: sort subfiles and 4,5
8-page runs

merge 6,6
7,8
9
General External Merge Sort ASET

* More than 3 buffer pages. How can we utilize them?


• To sort a file with N pages using B buffer pages:
– Pass 0: use B buffer pages. Produce  N / B sorted runs of
B pages each.
– Pass 2, …, etc.: merge B-1 runs.

INPUT 1

INPUT 2
... ... OUTPUT ...
INPUT B-1
Disk Disk
B Main memory buffers
Cost of External Merge Sort ASET

1 +  log B −1  N / B 
• Number of passes:
• Cost = 2N * (# of passes)
• E.g., with 5 buffer pages, to sort 108 page file:
– Pass 0: 108 / 5  = 22 sorted runs of 5 pages each
(last run is only 3 pages)
– Pass 1:  22 / 4  = 6 sorted runs of 20 pages each
(last run is only 8 pages)
– Pass 2: 2 sorted runs, 80 pages and 28 pages
– Pass 3: Sorted file of 108 pages
Number of Passes of External Sort ASET

N B=3 B=5 B=9 B=17 B=129 B=257


100 7 4 3 2 1 1
1,000 10 5 4 3 2 2
10,000 13 7 5 4 2 2
100,000 17 9 6 5 3 3
1,000,000 20 10 7 5 3 3
10,000,000 23 12 8 6 4 3
100,000,000 26 14 9 7 4 4
1,000,000,000 30 15 10 8 5 4
I/O for External Merge Sort ASET

• … longer runs often means fewer passes!


• Actually, do I/O a page at a time
• In fact, read a block of pages sequentially!
• Suggests we should make each buffer
(input/output) be a block of pages.
– But this will reduce fan-out during merge
passes!
– In practice, most files still sorted in 2-3
passes.
Number of Passes of Optimized Sort ASET

N B=1,000 B=5,000 B=10,000


100 1 1 1
1,000 1 1 1
10,000 2 2 1
100,000 3 2 2
1,000,000 3 2 2
10,000,000 4 3 3
100,000,000 5 3 3
1,000,000,000 5 4 3

* Block size = 32, initial pass produces runs of size 2B.


Double Buffering ASET

• To reduce wait time for I/O request to


complete, can prefetch into `shadow block’.
– Potentially, more passes; in practice, most files
still sorted in 2-3 passes.

INPUT 1

INPUT 1'

INPUT 2
OUTPUT
INPUT 2'
OUTPUT'

b
block size
Disk INPUT k
Disk
INPUT k'

B main memory buffers, k-way merge


Sorting Records! ASET

• Sorting has become a blood sport!


– Parallel sorting is the name of the game ...
• Datamation: Sort 1M records of size 100 bytes
– Typical DBMS: 15 minutes
– World record: 3.5 seconds
• 12-CPU SGI machine, 96 disks, 2GB of RAM
• New benchmarks proposed:
– Minute Sort: How many can you sort in 1 minute?
– Dollar Sort: How many can you sort for $1.00?
ASET

Thank you

26
ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

1
ASET

Module-V
Searching & Sorting Techniques
Contents to be Covered
ASET

Searching and Sorting Techniques

➢ Linear Search
➢ Binary Search
Learning Objectives ASET

➢ Impart in-depth knowledge of various searching algorithms.

➢ Implement linear search and binary search algorithms for complex

problems.
ASET

What is algorithm and Algorithm


design?
• An Algorithm is a Step by Step solution of
a specific mathematical or computer
related problem.

• Algorithm design is a specific method to


create a mathematical process in solving
problems.

5
ASET

Sorted Array

• Sorted array is an array where each


element is sorted in numerical,
alphabetical, or some other order, and
placed at equally spaced addresses in
computer memory.
0 1 2 3
10 20 30 40

6
ASET

Unsorted Array

• Unsorted array is an array where each


element is not sorted in numerical,
alphabetical, or some other order, and
placed at equally spaced addresses in
computer memory.
0 1 2 3
10 40 30 20

7
ASET

What is Searching?

• In computer science, searching is the process of finding


an item with specified properties from a collection of
items.
• The items may be stored as records in a database,
simple data elements in arrays, text in files, nodes in
trees, vertices and edges in graphs, or maybe be
elements in other search place.
• The definition of a search is the process of looking for
something or someone
• Example: An example of a search is a quest to find a
missing person
8
ASET

Why do we need searching?

• Searching is one of the core computer science


algorithms.
• We know that today’s computers store a lot of
information.
• To retrieve this information proficiently we need very
efficient searching algorithms.
• Types of Searching
– Linear search
– Binary search

9
ASET

Linear search

• The linear search is a sequential search, which uses a


loop to step through an array, starting with the first
element.

• It compares each element with the value being searched


for, and stops when either the value is found or the end
of the array is encountered.

• If the value being searched is not in the array, the


algorithm will unsuccessfully search to the end of the
array.
10
ASET

Linear search

• Since the array elements are stored in linear order


searching the element in the linear order make it easy
and efficient.

• The search may be successful or unsuccessfully. That


is, if the required element is found them the search is
successful other wise it is unsuccessfully.

11
ASET

Linear search

• Since the array elements are stored in linear order


searching the element in the linear order make it easy
and efficient.

• The search may be successful or unsuccessfully. That


is, if the required element is found them the search is
successful other wise it is unsuccessfully.

12
ASET

Unordered linear/ Sequential search

int unorderedlinearsearch (int A[], int n, int data)


{
for (inti=0; i<n; i++)
{
if(A[i] == data)
return i;
}
return -1;
}
13
ASET

Advantages of Linear search


• If the first number in the directory is the number you were
searching for ,then lucky you!!.
• Since you have found it on the very first page, now its not
important for you that how many pages are there in the
directory.
• The linear search is simple -It is very easy to understand
and implement
• It does not require the data in the array to be stored in any
particular order
• So it does not depends on no. on elements in the directory.
Hence constant time .

14
ASET

Disadvantages of Linear search


• It may happen that the number you are searching for is the last
number of directory or if it is not in the directory at all.
• In that case you have to search the whole directory.
• Now number of elements will matter to you. if there are 500
pages ,you have to search 500;if it has 1000 you have to search
1000.
• Your search time is proportional to number of elements in the
directory.
• It has very poor efficiency because it takes lots of comparisons
to find a particular record in big files
• The performance of the algorithm scales linearly with the size of
the input
• Linear search is slower then other searching algorithms
15
ASET

Analysis of Linear search


• In the average case, the target value is somewhere in the
array.
• In fact, since the target value can be anywhere in the array,
any element of the array is equally likely.
• So on average, the target value will be in the middle of the
array.
• So the search takes an amount of time proportional to half
the length of the array
• The worst case complexity is O(n), sometimes known an
O(n) search
• Time taken to search elements keep increasing as the
number of elements are increased.
16
ASET

Analysis of Linear search


• How long will our search take?

In the best case, the target value is in the first element of the
array.

So the search takes some tiny, and constant, amount of time.

In the worst case, the target value is in the last element of


the array.

So the search takes an amount of time proportional to the


length of the array. 17
ASET

Binary search
• The general term for a smart search through sorted data is
a binary search.
Step 1: The initial search region is the whole array.
Step 2: Look at the data value in the middle of the search
region.
Step 3: If you’ve found your target, stop.
Step 4: If your target is less than the middle data value, the
new search region is the lower half of the data.
Step 5: If your target is greater than the middle data value,
the new search region is the higher half of the data.
Step 6: Continue from Step 2.
18
ASET

Binary search

19
ASET

Binary search

Step2: Calculate middle = (low + high) / 2.


= (0 + 8) / 2 = 4.

If 37 == array[middle] →return middle


Else if 37 < array[middle] →high = middle -1
Else if 37 > array[middle] →low = middle +1

20
ASET

Binary search

Repeat Step 2: Calculate middle = (low + high) / 2.


= (0 + 3) / 2 = 1.

If 37 == array[middle] →return middle


Else if 37 < array[middle] →high = middle -1
Else if 37 > array[middle] →low = middle +1

21
ASET

Binary search

Repeat Step 2: Calculate middle = (low + high) / 2.


= (2 + 3) / 2 = 2.

If 37 == array[middle] →return middle


Else if 37 < array[middle] →high = middle -1
Else if 37 > array[middle] →low = middle +1

22
ASET

Binary search Routine

23
ASET

Binary Search Performance


• Successful Search
– Best Case –1 comparison
– Worst Case –log2N comparisons
• Unsuccessful Search
– Best Case = ----
– Worst Case = log2N comparisons
• Since the portion of an array to search is cut into half after
every comparison, we compute how many times the array
can be divided into halves.
• After K comparisons, there will be N/2K elements in the list.
We solve for K when N/2K= 1, deriving K = log2N.

24
ASET

Comparing N and log2N performances


Array Size Linear – N Binary – log2N
10 10 4
50 50 6
100 100 7
500 500 9
1000 1000 10
2000 2000 11
3000 3000 12
4000 4000 12
5000 5000 13
6000 6000 13
7000 7000 13
8000 8000 13
9000 9000 14
10000 10000 14 25
ASET

Important differences
• Input data needs to be sorted in Binary Search and not in
Linear Search

• Linear search does the sequential access whereas Binary


search access data randomly.

• Time complexity of linear search -O(n) , Binary search has


time complexity O(log n).

• Linear search performs equality comparisons and Binary


search performs ordering comparisons
26
ASET

Thank you

27
ASET

Data Structures Using C


Course Code: CSIT124
B. Tech. (IT/CSE)

1
ASET

Module-V
Searching & Sorting Techniques
Contents to be Covered
ASET

Searching and Sorting Techniques

➢ Hashing
➢ Hash Function
➢ Collision Resolution Techniques
Learning Objectives ASET

➢ Impart in-depth knowledge of hashing, hash function and various

collision resolution techniques.


Review ASET

• Sorting Algorithms
– Elementary
• Insertion Sort
• Selection Sort
• Bubble Sort
– Efficient
• Shell Sort
• Heap Sort
• Quick Sort
• Merge Sort
• Radix Sort
Searching so far ASET

• We have encountered searching algorithms


several times already in the course;
– Linked List searches O(n)
– Pick a Number
• The efficiency of searches has varied,
depending on how effectively the data has been
arranged.
• This week we look at an alternative approach to
searching – where the data could be found in
constant time O(1)
Searching in Constant Time ASET

• In order the find data in constant time, we need


to know where to look for it.
• Given a ‘key’, which could be in any form
(alphanumeric), we need to return an index for
some table (or array).
• A function which converts a key to a address is
known as a Hash function.
– If that address turns out to be a unique address it is a
perfect hash function.
Hashing Example ASET

• Take a student id number (IDNum);


– 478603
• A possible hash function could be;
– H(IDNum) = IDNum % 1000
– Which would return – what?
– This number could then be the array index
number.
Hashing ASET

• If only Hashing was that simple…!


• There is a problem with the function, the hash
function will return a total of 1000 possible
different indexes;
– What happens when there are more than 1000
students?
• When a hash function returns the same index for
more than one key, there is a collision.
• A hash table, needs to contain at least as many
positions as the number of elements to be
hashed.
Hashing Example 2 ASET

• Suppose we need to convert a variable name


into a data location.
– int ken = 31;
• We need a hash function that could return a
unique address for each variable name;
– H(“ken”)
• Consider how many different variable names
there could be?
• How large should the hash table be?
Hashing Example cont. ASET

• Suppose set the function H() to sum the


values of each letter in the variable name;
– k=11,e=5,n=14;
– H(“ken”) = 30.
• Therefore we could store the ken data in
index 30.
• We can use this bad hashing function to
highlight some problems that hashing
functions should address;
Hashing Problems ASET

• If we have a program with 4 variables;


– name H(“name”) = 33
– age H(“age”) = 13
– gender H(“gender”) = 53
– mean H(“mean”) = 33
1) The data is spread out throughout the table – with
many unused wasted cells.
2) There is a collision between name and mean.
• These two problems have to be solved by a
simple, efficient algorithm.
Good Hash Functions ASET

• A good hash function should:


- be easy and quick to compute
- achieve an even distribution of the key values that
actually occur across the index range supported by the
table
• Typically a hash function will take a key value and:
- chop it up into pieces, and
- mix the pieces together in some fashion, and
- compute an index that will be uniformly distributed
across the available range.
• Note: hash functions are NOT random in any sense.
Approaches ASET

• Truncation
– Ignore a part of the key value and use the remainder
as the table index
• e.g., 21296876 maps to 976
• Folding:
– Partition the key, then combine the parts in some
simple manner
• e.g., 21296876 maps to 212 + 968 + 76 = 1256 and then mod to 256
• Modular Arithmetic:
– Convert the key to an integer, and then mod that
integer by the size of the table
• e.g., 21296876 maps to 876
Truncation Caution ASET

• It is a good idea if the entire key has some


impact on the hash function, simply
truncating a key may lead to many keys
returning the same result when hashed.
– Consider truncating the last 3 letters of the
following keys;
• hash, mash, bash, trash.
Hash Function ASET

int strHash(string toHash, const int TableSize) {


int hashValue = 0;
for (unsigned int Pos = 0; Pos < toHash.length(); Pos++) {
hashValue = hashValue + int(toHash.at(Pos));
}
return (hashValue % TableSize);
}

Given the key ‘ken’ and a table size of 1000, what


would be returned?
Improving the hash function ASET

• The hash function given on the previous slide would


return the same result if we put either of the following
keys in; sham or mash
– The hash function didn’t take position into account.
• This can easily be remedied with the following change;
hashValue = 4*hashValue + int(toHash.at(Pos));
• This is known as Collision Reduction, or rather
reducing the chance of collision.
Resolving Collisions ASET

• Even with a sophisticated hashing function it is


likely that collisions will still occur, so we need a
strategy to deal with collisions.
• We first find out about a collision if we try to
insert data into a position which is already filled.
– In this case we can simply insert the data into a
different available position, leaving a record so the
data can be retrieved.
Linear Probing ASET

• Linear probing deals with collisions by


inserting a new value into the next
available space after the space returned
by the hash function;
• If H(key) is occupied store data in
H(key)+1.
Linear Probing, problem ASET

Consider the following hash table

a b c d

If c is duplicated, the new value is placed in the successive cell.

a b c c d

a b c c d c

This leads to clustering, which contradicts one of our key objectives.


Quadratic Probing ASET

• Quadratic Probing is designed to combat the clustering


effect of linear probing.
• Rather than inserting the data into the next available cell,
data is inserted into a cell based on the following
sequence;
– k
– k+1
– k+4
– k+9
– k+16
• While this solves the problem of clustering, it produces a
problem that the hash function may not try every slot in
the table (if table size is a prime, then approximately half
of the cells will be tested).
General Increment Probing ASET

• A general Increment Probing approach will try


each cell in a sequence based on a formula;
– k
– k+s(1)
– k+s(2)
– k+s(3)
– k+s(4)
• Here care must be taken that the formula
doesn’t return to the first cell too quickly.
• What happens if s(i) = i2 ?
Key dependent Probing ASET

• Another probing strategy could calculate


the formula based on some part of the
original key – perhaps just adding the
value of the first segment of the key.
• However this could produce inefficient
code.
Deletion ASET

• Given this approach to collision resolution,


care needs to be taken when deleting data
from a cell.
– Why?
• The tombstone method marks a deleted
cell as available for insertion, but marks it
has having had data in it.
Alternative to Probing ASET

• An alternative strategy to probing is to use


Separate Chaining.
– Here more than one piece of data can be
associated to the same cell in the hash table
– The cell can contain a pointer to a linked list
of data insertions.
– This is sometimes known as a bucket.
ASET

Thank you

26
Amity School of Engineering Technology

Amity School of Engineering & Technology


MODULE VI
Graphs and its Applications
Topic: Operations on Graphs
B.Tech.(CSE)
Data Structures Using C (CSIT 124)

1
Syllabus of Module VI Amity School of Engineering Technology

• Introduction:
– Graph Theory Terminology,
– Sequential Representation of Graph (Adjacency and Path Matrix),
– Warshall’s Algorithm,
– Linked Representation of Graph,
– Different Operations on Graphs,
– Traversing A Graph(DFS, BFS),
• Spanning Trees -Introduction
– Representation of Spanning tree,
– Constructing A Spanning Tree(Prim’s Algorithm, Kruskal’s Algorithm).
2
Learning outcomes Amity School of Engineering Technology

• Students will be able to know about


– Operations on Graphs
• Union
• Intersection
• Sum of two Graphs
• Ring Sum
• Product
• Complement
3
What is a graph? Amity School of Engineering Technology

• A data structure that consists of a set of


nodes (vertices) and a set of edges that
relate the nodes to each other
• The set of edges describes relationships
among the vertices
Amity School of Engineering Technology

Formal definition of graphs

• A graph G is defined as follows:


G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Amity School of Engineering Technology

Directed vs. undirected graphs


• When the edges in a graph have no
direction, the graph is called undirected
Amity School of Engineering Technology

Directed vs. undirected graphs (cont.)


• When the edges in a graph have a
direction, the graph is called directed (or
digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!
Trees vs graphs Amity School of Engineering Technology

• Trees are special cases of graphs!!


Graph terminology Amity School of Engineering Technology

• Adjacent nodes: two nodes are adjacent


if they are connected by an edge
5 is adjacent to 7
7 is adjacent from 5

• Path: a sequence of vertices that


connect two nodes in a graph
• Complete graph: a graph in which every
vertex is directly connected to every
other vertex
Graph terminology Amity School of Engineering Technology

• What is the number of edges in a


complete directed graph with N
vertices?
N * (N-1)

2
O( N )
Graph terminology Amity School of Engineering Technology

• What is the number of edges in a complete


undirected graph with N vertices?
N * (N-1) / 2
2
O( N )
Graph terminology Amity School of Engineering Technology

• Weighted graph: a graph in which each edge


carries a value
Graph implementation Amity School of Engineering Technology

• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to
represent the edges
Amity School of Engineering Technology
Array-based implementation
Graph implementation Amity School of Engineering Technology

• Linked-list implementation
– A 1D array is used to represent the vertices
– A list is used for each vertex v which contains
the vertices which are adjacent from v
(adjacency list)
Linked-list implementation
Amity School of Engineering Technology
Amity School of Engineering Technology

Adjacency matrix vs. adjacency list


representation
• Adjacency matrix
– Good for dense graphs --|E|~O(|V|2)
– Memory requirements: O(|V| + |E| ) = O(|V|2 )
– Connectivity between two vertices can be
tested quickly
• Adjacency list
– Good for sparse graphs -- |E|~O(|V|)
– Memory requirements: O(|V| + |E|)=O(|V|)
– Vertices adjacent to another vertex can be
found quickly
Amity School of Engineering Technology

Thank You!
All pairs shortest path Amity School of Engineering Technology

• The problem: find the shortest path between every pair of


vertices of a graph

• The graph: may contain negative edges but no negative


cycles

• A representation: a weight matrix where


W(i,j)=0 if i=j.
W(i,j)= if there is no edge between i and j.
W(i,j)=“weight of edge”
Amity School of Engineering Technology

The weight matrix and the graph


1
3 v1 v2
1 2 3 4 5 9
5
1 0 1  1 5 1 2 3
v5
2 9 0 3 2  3 2
v4 v3
3   0 4 
4
4   2 0 3
5 3    0
The subproblems Amity School of Engineering Technology

• How can we define the shortest distance di,j in terms of


“smaller” problems?

• One way is to restrict the paths to only include vertices


from a restricted subset.

• Initially, the subset is empty.

• Then, it is incrementally increased until it includes all


the vertices.
The subproblems Amity School of Engineering Technology

• Let D(k)[i,j]=weight of a shortest path from vi to vj using


only vertices from {v1,v2,…,vk} as intermediate vertices in
the path

– D(0)=W
– D(n)=D which is the goal matrix

• How do we compute D(k) from D(k-1) ?


Example Amity School of Engineering Technology

1 2 3
1 0 4 5
W= D0 =
2 2 0 
1 5
3  -3 0
4 2 3
1 2 3
2 -3 1 0 0 0
P= 2 0 0 0
3 0 0 0

Floyd’s Algorithm 5
Amity School of Engineering Technology

1 5 1 2 3
4 3 1 0 4 5
2
D0 = 2 2 0 
-3 D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] )
2
3  -3 0
= min (, 7)
1 2 3 =7
1 0 4 5
D1 =
2 2 0 7
D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] )
3  -3 0 = min (-3,)
1 2 3 = -3
1 0 0 0
P= 2 0 0 1
3 0 0 0
1 2 3
D1 = 1 0 4 5 Amity School of Engineering Technology

2 2 0 7
3  -3 0

1 2 3
1 0 4 5 D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )
2
D = = min (5, 4+7)
1 5 2 2 0 7
=5
3 -1 -3 0
4 2 3
-3
2 1 2 3
1 0 0 0 D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )
P= 2 0 0 1 = min (, -3+2)
3 2 0 0 = -1

Floyd’s Algorithm 7
D2 = 1 2 3
1 0 4 5 Amity School of Engineering Technology
2 2 0 7
1 5 3 -1 -3 0
4 2 3
1 2 3
-3
2 1 0 2 5 D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] )
D3 =
2 2 0 7 = min (4, 5+(-3))
3 -1 -3 0 =2

1 2 3
1 0 3 0 D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] )
= min (2, 7+ (-1))
P= 2 0 0 1 =2
3 2 0 0

Floyd’s Algorithm 8
Amity School of Engineering Technology

Floyd's Algorithm: Using 2 D


matrices
Floyd
1. D  W // initialize D array to W [ ]
2. P  0 // initialize P array to [0]
3. for k  1 to n
// Computing D’ from D
4. do for i  1 to n
5. do for j  1 to n
6. if (D[ i, j ] > D[ i, k ] + D[ k, j ] )
7. then D’[ i, j ]  D[ i, k ] + D[ k, j ]
8. P[ i, j ]  k;
9. else D’[ i, j ]  D[ i, j ]
10. Move D’ to D.
Amity School of Engineering Technology

Can we use only one D matrix?

• D[i,j] depends only on elements in the kth column and


row of the distance matrix.
• We will show that the kth row and the kth column of the
distance matrix are unchanged when Dk is computed
• This means D can be calculated in-place

Floyd’s Algorithm 10
Amity School of Engineering Technology

The main diagonal values


• Before we show that kth row and column of D remain
unchanged we show that the main diagonal remains 0

• D(k)[ j,j ] = min{ D(k-1)[ j,j ] , D(k-1)[ j,k ] + D(k-1)[ k,j ] }


= min{ 0, D(k-1)[ j,k ] + D(k-1)[ k,j ] }
=0
• Based on which assumption?
The kth column Amity School of Engineering Technology

• kth column of Dk is equal to the kth column of Dk-1

• Intuitively true - a path from i to k will not become


shorter by adding k to the allowed subset of
intermediate vertices

• For all i, D(k)[i,k] =


= min{ D(k-1)[i,k], D(k-1)[i,k]+ D(k-1)[k,k] }
= min { D(k-1)[i,k], D(k-1)[i,k]+0 }
= D(k-1)[i,k]
Floyd’s Algorithm 12
Example Amity School of Engineering Technology

Floyd’s Algorithm 13
Amity School of Engineering Technology

The final distance matrix and P

The values in parenthesis are the non zero P values.

Floyd’s Algorithm 14
The call tree for Path(1, 4) Amity School of Engineering Technology

Path(1, 4)

Path(1, 6) Path(6, 4)
Print
P(1, 6)=0 v6

Path(6, 3) Print Path(3, 4)


v3
P(6, 3)=0 P(3, 4)=0
The intermediate nodes on the shortest path from 1 to 4 are v6, v3.
The shortest path is v1, v6, v3, v4.
Floyd’s Algorithm 15
Amity School of Engineering Technology

Thank You!
Amity School of Engineering Technology

Amity School of Engineering & Technology


MODULE VI
Graphs and its Applications
Topic: Operations on Graphs
B.Tech.(CSE)
Data Structures Using C (CSIT 124)

1
Syllabus of Module VI Amity School of Engineering Technology

• Introduction:
– Graph Theory Terminology,
– Sequential Representation of Graph (Adjacency and Path Matrix),
– Warshall’s Algorithm,
– Linked Representation of Graph,
– Different Operations on Graphs,
– Traversing A Graph(DFS, BFS),
• Spanning Trees -Introduction
– Representation of Spanning tree,
– Constructing A Spanning Tree(Prim’s Algorithm, Kruskal’s Algorithm).
2
What is a graph? Amity School of Engineering Technology

• A data structure that consists of a set of


nodes (vertices) and a set of edges that
relate the nodes to each other
• The set of edges describes relationships
among the vertices
Amity School of Engineering Technology

Formal definition of graphs

• A graph G is defined as follows:


G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Amity School of Engineering Technology

Directed vs. undirected graphs


• When the edges in a graph have no
direction, the graph is called undirected
Directed vs. undirected graphs (cont.) Amity School of Engineering Technology

• When the edges in a graph have a


direction, the graph is called directed (or
digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)


Trees vs graphs Amity School of Engineering Technology

• Trees are special cases of graphs!!


Amity School of Engineering Technology
Graph terminology
• Adjacent nodes: two nodes are adjacent
if they are connected by an edge
5 is adjacent to 7
7 is adjacent from 5

• Path: a sequence of vertices that


connect two nodes in a graph
• Complete graph: a graph in which every
vertex is directly connected to every
other vertex
Amity School of Engineering Technology
Graph terminology (cont.)
• What is the number of edges in a
complete directed graph with N
vertices?
N * (N-1)
2
O( N )
Amity School of Engineering Technology
Graph terminology (cont.)
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
O( N )
Amity School of Engineering Technology

Graph terminology (cont.)


• Weighted graph: a graph in which each edge
carries a value
Graph implementation Amity School of Engineering Technology

• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to
represent the edges
Amity School of Engineering Technology
Array-based implementation
Graph implementation (cont.) Amity School of Engineering Technology

• Linked-list implementation
– A 1D array is used to represent the vertices
– A list is used for each vertex v which contains
the vertices which are adjacent from v
(adjacency list)
Linked-list implementation
Amity School of Engineering Technology
Adjacency matrix vs. adjacency list
Amity School of Engineering Technology
representation

• Adjacency matrix
– Good for dense graphs --|E|~O(|V|2)
– Memory requirements: O(|V| + |E| ) = O(|V|2 )
– Connectivity between two vertices can be
tested quickly
• Adjacency list
– Good for sparse graphs -- |E|~O(|V|)
– Memory requirements: O(|V| + |E|)=O(|V|)
– Vertices adjacent to another vertex can be
found quickly
Graph specification based on Amity School of Engineering Technology

adjacency matrix representation


const int NULL_EDGE = 0;
private:
template<class VertexType> int numVertices;
class GraphType { int maxVertices;
public: VertexType* vertices;
GraphType(int); int **edges;
~GraphType(); bool* marks;
void MakeEmpty(); };
bool IsEmpty() const;
bool IsFull() const;
void AddVertex(VertexType);
void AddEdge(VertexType, VertexType, int);
int WeightIs(VertexType, VertexType);
void GetToVertices(VertexType, QueType<VertexType>&);
void ClearMarks();
void MarkVertex(VertexType);
bool IsMarked(VertexType) const; (continues)
template<class VertexType> Amity School of Engineering Technology
GraphType<VertexType>::GraphType(int maxV)
{
numVertices = 0;
maxVertices = maxV;
vertices = new VertexType[maxV];
edges = new int[maxV];
for(int i = 0; i < maxV; i++)
edges[i] = new int[maxV];
marks = new bool[maxV];
}

template<class VertexType>
GraphType<VertexType>::~GraphType()
{
delete [] vertices;
for(int i = 0; i < maxVertices; i++)
delete [] edges[i];
delete [] edges;
delete [] marks; (continues)
}
Amity School
void GraphType<VertexType>::AddVertex(VertexType vertex)of Engineering Technology
{
vertices[numVertices] = vertex;
for(int index = 0; index < numVertices; index++) {
edges[numVertices][index] = NULL_EDGE;
edges[index][numVertices] = NULL_EDGE;
}

numVertices++;
}

template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType fromVertex,
VertexType toVertex, int weight)
{
int row;
int column;
row = IndexIs(vertices, fromVertex);
col = IndexIs(vertices, toVertex);
edges[row][col] = weight;
} (continues)
Amity School of Engineering Technology

template<class VertexType>
int GraphType<VertexType>::WeightIs(VertexType fromVertex,
VertexType toVertex)
{
int row;
int column;

row = IndexIs(vertices, fromVertex);


col = IndexIs(vertices, toVertex);
return edges[row][col];
}
Amity School of Engineering Technology

Graph searching

• Problem: find a path between two nodes


of the graph (e.g., Austin and
Washington)
• Methods: Depth-First-Search (DFS) or
Breadth-First-Search (BFS)
Depth-First-Search (DFS) Amity School of Engineering Technology

• What is the idea behind DFS?


– Travel as far as you can down a path
– Back up as little as possible when you reach a "dead end"
(i.e., next vertex has been "marked" or there is no next
vertex)
• DFS can be implemented efficiently using a
stack
Depth-First-Search (DFS) (cont.) Amity School of Engineering Technology

Set found to false


stack.Push(startVertex)
DO
stack.Pop(vertex)
IF vertex == endVertex
Set found to true
ELSE
Push all adjacent vertices onto stack
WHILE !stack.IsEmpty() AND !found

IF(!found)
Write "Path does not exist"
Amity School of Engineering Technology

start end

(initialization)
Amity School of Engineering Technology
Amity School of Engineering Technology
template <class ItemType> Amity School of Engineering Technology

void DepthFirstSearch(GraphType<VertexType> graph, VertexType


startVertex, VertexType endVertex)
{
StackType<VertexType> stack;
QueType<VertexType> vertexQ;

bool found = false;


VertexType vertex;
VertexType item;

graph.ClearMarks();
stack.Push(startVertex);
do {
stack.Pop(vertex);
if(vertex == endVertex)
found = true;
(continues)
else { Amity School of Engineering Technology

if(!graph.IsMarked(vertex)) {
graph.MarkVertex(vertex);
graph.GetToVertices(vertex, vertexQ);

while(!vertexQ.IsEmpty()) {
vertexQ.Dequeue(item);
if(!graph.IsMarked(item))
stack.Push(item);
}
}
} while(!stack.IsEmpty() && !found);

if(!found)
cout << "Path not found" << endl;
}
(continues)
Amity School of Engineering Technology

template<class VertexType>
void GraphType<VertexType>::GetToVertices(VertexType vertex,
QueTye<VertexType>& adjvertexQ)
{
int fromIndex;
int toIndex;

fromIndex = IndexIs(vertices, vertex);


for(toIndex = 0; toIndex < numVertices; toIndex++)
if(edges[fromIndex][toIndex] != NULL_EDGE)
adjvertexQ.Enqueue(vertices[toIndex]);
}
Amity School of Engineering Technology

Breadth-First-Searching (BFS)

• What is the idea behind BFS?


– Look at all possible paths at the same
depth before you go at a deeper level
– Back up as far as possible when you reach
a "dead end" (i.e., next vertex has been
"marked" or there is no next vertex)
Amity School of Engineering Technology
Breadth-First-Searching (BFS)
• (cont.)efficiently using a
BFS can be implemented
queue IF(!found)
Write "Path does not exist"
Set found to false
queue.Enqueue(startVertex)
DO
queue.Dequeue(vertex)
IF vertex == endVertex
Set found to true
ELSE
Enqueue all adjacent vertices onto queue
WHILE !queue.IsEmpty() AND !found

• Should we mark a vertex when it is enqueued


or when it is dequeued ?
start end
Amity School of Engineering Technology

(initialization)
Amity School of Engineering Technology

next:
Amity School of Engineering Technology
Amity School of Engineering Technology
template<class VertexType>
void BreadthFirtsSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex);
{
QueType<VertexType> queue;
QueType<VertexType> vertexQ;//

bool found = false;


VertexType vertex;
VertexType item;

graph.ClearMarks();
queue.Enqueue(startVertex);
do {
queue.Dequeue(vertex);
if(vertex == endVertex)
found = true;

(continues)
else { Amity School of Engineering Technology
if(!graph.IsMarked(vertex)) {
graph.MarkVertex(vertex);
graph.GetToVertices(vertex, vertexQ);

while(!vertxQ.IsEmpty()) {
vertexQ.Dequeue(item);
if(!graph.IsMarked(item))
queue.Enqueue(item);
}
}
}
} while (!queue.IsEmpty() && !found);

if(!found)
cout << "Path not found" << endl;
}
Amity School of Engineering Technology
Single-source shortest-path
problem
• There are multiple paths from a source
vertex to a destination vertex
• Shortest path: the path whose total
weight (i.e., sum of edge weights) is
minimum
• Examples:
– Austin->Houston->Atlanta->Washington:
1560 miles
– Austin->Dallas->Denver->Atlanta-
>Washington: 2980 miles
Amity School of Engineering Technology

Single-source shortest-path
problem (cont.)
• Common algorithms: Dijkstra's
algorithm, Bellman-Ford algorithm
• BFS can be used to solve the shortest
graph problem when the graph is
weightless or all the weights are the
same
(mark vertices before Enqueue)
Amity School of Engineering Technology

Exercises
• 24-37
Learning outcomes Amity School of Engineering Technology

• Students will be able to know about


– Operations on Graphs
• Union
• Intersection
• Sum of two Graphs
• Ring Sum
• Product
• Complement
40
Operations on Graphs Amity School of Engineering Technology

1. Union If two graph G1 & G2 are given then 𝐺1 ∪ 𝐺2


Will be 𝑉 𝐺1 ∪ 𝐺2 = 𝑉 𝐺1 ∪ 𝑉(𝐺2 )
& 𝐸 𝐺1 ∪ 𝐺2 = 𝐸 𝐺1 ∪ 𝐸(𝐺2 )
e1
A B B e5

e2 e4
∪ e4 E

C D D e6
e3
Amity School of Engineering Technology

Operations on Graph
2. Intersection If two graph G1 & G2 are given then 𝐺1 ∩ 𝐺2
Will be 𝑉 𝐺1 ∩ 𝐺2 = 𝑉 𝐺1 ∩ 𝑉(𝐺2 )
& 𝐸 𝐺1 ∩ 𝐺2 = 𝐸 𝐺1 ∩ 𝐸(𝐺2 )
e1
A B B e5

e2 e4
∩ e4 E

C D D e6
e3
Amity School of Engineering Technology

Operations on Graph
3. Sum of two graph If two graph G1 & G2 are given then 𝐺1 + 𝐺2
Will be 𝑉 𝐺1 + 𝐺2 = 𝑉 𝐺1 + 𝑉(𝐺2 )
& 𝐸 𝐺1 + 𝐺2 = Edges which are in G1 & G2 and edges
obtained by joining each vertex of G1 to each G2

A B

+
e2 e4

C
D
e3

D
Amity School of Engineering Technology

Operations on Graph
4. Ring Sum If two graph G1 & G2 are given then 𝐺1 ⊕ 𝐺2
Will be 𝑉 𝐺1 ⊕ 𝐺2 = 𝑉 𝐺1 ∪ 𝑉(𝐺2 )
& 𝐸 𝐺1 ⊕ 𝐺2 = 𝐸 𝐺1 ∪ 𝐸 𝐺2 − 𝐸 𝐺1 ∩ 𝐸(𝐺2 )
edges either in G1 or G2 but not in both

e1
A B B e5

e2 e4
∪ e4 E

C D D e6
e3
Amity School of Engineering Technology

Operations on Graph
5. Product If two graph G1 & G2 are given then 𝐺1 × 𝐺2
Will be 𝑉 𝐺1 × 𝐺2 = 𝑉 𝐺1 × 𝑉(𝐺2 )
& 𝐸 𝐺1 × 𝐺2 = 𝐸 𝐺1 × 𝐸 𝐺2 ∪ 𝐸 𝐺2 × 𝐸(𝐺1 )

𝑉 𝐺1 × 𝐺2 = { 𝐴, 0 , 𝐴, 1 , 𝐵, 0 , 𝐵, 1 , 𝐶, 0 , 𝐶, 1 }
A 𝐸 𝐺1 × 𝐺2 = { 𝐴, e4 , 𝐵, e4 , 𝐶, e4 , 0, e2 , 0, e3 ,
0 1, e2 , 1, e3 }
e2 A, e4
A, 0
×
A, 1
e4
B 0, e2 1, e2
B, e4
e3 1 B, 0 B, 1

0, e3 1, e3
C C, e4
C, 0 C, 1
Amity School of Engineering Technology

Operations on Graph
6. Complement 𝐺 → 𝐺 ′
𝐺 ′ is said to be complement when the vertex pair which are
adjacent in 𝐺 ′ are not adjacent in G.
A A

0 1 0 1

B C B C

2 2

𝐺 𝐺′
Amity School of Engineering Technology

Thank You!
Amity School of Engineering Technology

Amity School of Engineering & Technology


MODULE VI
Graphs and its Applications
Topic: Graph Traversal
B.Tech.(CSE)
Data Structures Using C (CSIT 124)

1
Learning outcomes Amity School of Engineering Technology

• Students will be able to know about


– Graph Traversal
– Depth First Search (DFS)
– Breadth First Search (BFS)

2
Graph Traversal Amity School of Engineering Technology

Graph Terminology
-Graph Representation
-Warshall Algorithms
-Different Operations on Graphs
-Graph Traversal
-Minimumspanning tree
- Dijkstra shortest Path Algorithm

3
Graph Traversal Amity School of Engineering Technology

Graph traversal is technique used for searching a vertex in a graph. The


graph traversal is also used to decide the order of vertices to be visit in
the search process.

DFS (Depth First Search)


BFS (Breadth First Search)

4
Amity School of Engineering Technology

DFS (Depth First Search)


DFS traversal of a graph, produces a spanning tree as final
result.
Spanning Tree is a graph without any loops.
We use Stack data structure with maximum size of total
number of vertices in the graph.
Back tracking is coming back to the vertex from which we
came to current vertex.

5
Amity School of Engineering Technology

DFS (Depth First Search) Work-Through

Visited Array Stack


F C A √
A B √
B C √
D
H D √ B
A
E √
G E H
F √ G
G √ C
F
E
H √ D
The order nodes are visited:
No
F
Back
Visit
Nodes
Only
Stack
Visit
No unvisited
isnodes
Dto
Consider
G
C D
isAD
is
Node –BCnodes
adjacent
unvisited
nodes
and
empty.
unvisited H
Band
has
adjacent
is are
nodesisadjacent
to
beenC;visited,
adjacent cannot
adjacent
adjacent
to E,
adjacent SotoA.
to
Depth-first
adjacent to
D,
to to
visit
to F.
D.
G.
H.D.D
decide
decide
G
Decide
traversal
B, A,is
H,
DC EG H A BF Backtrack.
continue
to
G,visit
has
to E
C
E.already
Decide
visit
done. to ➔
B nextbacktrack,
first F next.
(Rule:
been
visit
next.
Backtrack A i.e.,
visit
visited.
(pop the pop to
adjacent
Decide
stack).
stack
visit H.and
nodes restore previous
in alphabetical order)state 6
Amity School of Engineering Technology

Algorithm for DFS


• Step 1: Define a Stack of size total number of vertices
• Step 2: Select any vertex as starting point. Visit that vertex and push it on
to the Stack.
• Step 3: Visit any one of the adjacent vertex of the vertex which is at top of
the stack which is not visited and push it on to the stack.
• Step 4: Repeat step 3 until there are no new vertex to be visit from the
vertex on top of the stack.
• Step 5: When there is no new vertex to be visit then use back
tracking and pop one vertex from the stack.
• Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty. When stack
becomes Empty, then produce final spanning tree by removing unused
edges from the graph
7
Amity School of Engineering Technology

Pseudo code for DFS


DFS(input: Graph G)
{
Stack S; Integer x, y;
while (G has an unvisited node x)
{
visit(x);
push(x,S);
while (S is not empty)
{
t := top(S);
if (t has an unvisited neighbor y)
visit(y);
push(y,S);
else
pop(S);
}
}
} 8
Amity School of Engineering Technology

Breadth First Search Traversal


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

9
Amity School of Engineering Technology

Example 1. Choose any random vertex


as start point.
2. Insert that vertex in a Queue
r s t u 3. Insert adjacent neighbour in


1 
0 
2 
3
a Queue and Deque
4. Next vertex in queue is
selected again step 3 is
carried out. Continue till all
vertex been visited

 1  
5. We have visited all vertex
2 2 3 and also queue is empty.
6. The path we followed is the
v w x y
final Spanning tree.

Q[8] s w r t x v u y
Amity School of Engineering Technology

Algorithm for BFS


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

11
Amity School of Engineering Technology

Pseudo code for DFS


BFS(input: graph G)
{
Queue Q; Integer x, z, y;
while (G has an unvisited node x)
{
visit(x); Enqueue(x,Q);
while (Q is not empty)
{
z := Dequeue(Q);
for all (unvisited neighbor y of z)
{
visit(y);
Enqueue(y,Q);
}
}
}
}

12
Amity School of Engineering Technology

Complexity Analysis
• Queuing time is O(V) and scanning all edges requires O(E)
• Overhead for initialization is O (V)
• So, total running time is O(V+E)

13
Amity School of Engineering Technology

Thank You
Amity School of Engineering Technology

Amity School of Engineering & Technology


MODULE VI
Graphs and its Applications
Topic: Minimum Spanning Tree
B.Tech.(CSE)
Data Structures Using C (CSIT 124)

1
Learning outcomes Amity School of Engineering Technology

• Students will be able to know about


– Minimum Spanning Tree (MST)
– Kruskal’s algorithm
– Prim’s algorithms
– Applications of Minimum Spanning Tree

2
Amity School of Engineering Technology

Minimum spanning tree


A spanning tree is a subset of Graph G, which has all the
vertices covered with minimum possible number of edges.
A complete undirected graph can have maximum nn-2 number of
spanning trees, where n is number of nodes.

3
Amity School of Engineering Technology

General properties of spanning tree


A connected graph G can have more than one spanning tree.
All possible spanning trees of graph G, have same number of
edges and vertices.
Spanning tree does not have any cycle (loops)
Removing one edge from spanning tree will make the graph
disconnected i.e. spanning tree is minimally connected.
Adding one edge to a spanning tree will create a cycle or loop
i.e. spanning tree is maximally acyclic.

4
Amity School of Engineering Technology

Mathematical properties of spanning tree


Spanning tree has n-1 edges, where n is number of nodes (vertices)
From a complete graph, by removing maximum e-n+1 edges, we can
construct a spanning tree.
A complete graph can have maximum nn-2 number of spanning trees.
Spanning trees are subset of a connected Graph G and
disconnected Graphs do not have spanning tree.

Application of Spanning Tree


Spanning tree is basically used to find minimum paths to connect all
nodes in a graph.
• Civil Network Planning
• Computer Network Routing Protocol
5
• Cluster Analysis
Amity School of Engineering Technology

Minimum Spanning Tree (MST)


In a weighted graph, a minimum spanning tree is a spanning tree that
has minimum weight than all other spanning trees of the same graph.

MST Algorithm
Kruskal’s Algorithm
Prim’s Algorithm
6
Amity School of Engineering Technology

Example
A cable company want to connect five villages to their network which
currently extends to the market town of Avonford. What is the minimum
length of cable needed? 5
BrinleighB 5 Cornwell
C

We model the situation as a 33


network, then the problem is 44
8
8 66
to find the minimum connector
for the network 88
Avonford
A Fingley
F Donster
D
77

5
5
44
22

7
Edan
E
Amity School of Engineering Technology

Kruskal’s Algorithm List the edges in order of size:

B 5 Select the next shortest edge


C
which does not create a cycle
3
4 Select the shortest edge in the network
8 6
ED 2 ED 2
8 AB 3 AB 3
A F D AE 4 AE 4 or CD 4
7 CD 4
CD 4
5 BC 5 EF 5 BC 5 forms the cycle
4 EF 5
2 CF 6
AF 7
BF 8
E CF 8

The solution is Total weight of tree: 18 8


Amity School of Engineering Technology

Kruskal’s Algorithm
1. Remove all loops & Parallel Edges from the given graph
2. Sort all the edges in non-decreasing order of their weight.
3. Pick the smallest edge. Check if it forms a cycle with the
spanning tree formed so far. If cycle is not formed, include
this edge. Else, discard it.
4. Repeat step#2 until there are (V-1) edges in the spanning
tree

9
Amity School of Engineering Technology

Prim’s Algorithm Visited Select any vertex


B 5 Vertex
C A
A Select the shortest edge
3
4 B connected to that vertex
8 6
E Select the shortest edge
8 D connected to any vertex
A F D C already visited.
7
F
5 AB 3
4 AE 4 BC or CB 5 but
2
forms cycle so
ED 2 discard
E DC 4
EF 5
The solution is Total weight of tree: 18 10
Amity School of Engineering Technology

Prim’s Algorithm
1. Create a set mstSet that keeps track of vertices already included in
MST.
2. Assign a key value to all vertices in the input graph. Initialize
all key values as INFINITE. Assign key value as 0 for the first
vertex so that it is picked first.
3. While mstSet doesn’t include all vertices
Pick a vertex u which is not there in mstSet and has minimum key value.

Include u to mstSet

Update key value of all adjacent vertices of u. To update the key values, iterate through all
adjacent vertices. For every adjacent vertex v, if weight of edge u-v is less than the previous
key value of v, update the key value as weight of u-v

11
Amity School of Engineering Technology

Minimum Spanning Tree Algorithms

Kruskal’s algorithm Prim’s algorithm

1. Select the shortest edge in 1. Select any vertex


a network
2. Select the shortest edge
2. Select the next shortest connected to that vertex
edge which does not
create a cycle 3. Select the shortest edge
connected to any vertex
3. Repeat step 2 until all already connected
vertices have been
connected 4. Repeat step 3 until all
vertices have been
connected
Amity School of Engineering Technology

Thank You !

You might also like