0% found this document useful (0 votes)
30 views71 pages

Data Structures Syllabus for CSE BCS304

The document outlines the syllabus for the Data Structures and Applications course (BCS304) at Sir M Visvesvaraya Institute of Technology, detailing five modules covering topics such as arrays, stacks, queues, linked lists, trees, graphs, and hashing. It includes course objectives and outcomes, emphasizing the understanding and application of various data structures. Additionally, it lists required textbooks and reference materials for the course.

Uploaded by

jtr2826
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views71 pages

Data Structures Syllabus for CSE BCS304

The document outlines the syllabus for the Data Structures and Applications course (BCS304) at Sir M Visvesvaraya Institute of Technology, detailing five modules covering topics such as arrays, stacks, queues, linked lists, trees, graphs, and hashing. It includes course objectives and outcomes, emphasizing the understanding and application of various data structures. Additionally, it lists required textbooks and reference materials for the course.

Uploaded by

jtr2826
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SIR M VISVESVARAYA INSTITUTE OF TECHNOLOGY

Krishnadevarayanagar, Hunasamaranahalli, Yelahanka, International


Airport Road, Bengaluru - 562 157

Data Structures and Applications


BCS304
Syllabus
Module – 1
INTRODUCTION TO DATA STRUCTURES: Data Structures, Classifications (Primitive & Non-Primitive),
Data structure Operations, Review of pointers and dynamic Memory Allocation,
ARRAYS and STRUCTURES: Arrays, Dynamic Allocated Arrays, Structures and Unions, Polynomials,
Sparse Matrices, representation of Multidimensional Arrays, Strings
STACKS: Stacks, Stacks Using Dynamic Arrays, Evaluation and conversion of Expressions
Text Book: Chapter-1:1.2 Chapter-2: 2.1 to 2.7 Chapter-3: 3.1,3.2,3.6
Reference Book 1: 1.1 to 1.4 8 Hours

Module – 2
QUEUES: Queues, Circular Queues, Using Dynamic Arrays, Multiple Stacks and queues.
LINKED LISTS : Singly Linked, Lists and Chains, Representing Chains in C, Linked Stacks and Queues,
Polynomials
Text Book: Chapter-3: 3.3, 3.4, 3.7 Chapter-4: 4.1 to 4.4

Department of CSE 2
Syllabus
Module – 3
LINKED LISTS : Additional List Operations, Sparse Matrices, Doubly Linked List.
TREES: Introduction, Binary Trees, Binary Tree Traversals, Threaded Binary Trees.
Text Book: Chapter-4: 4.5,4.7,4.8 Chapter-5: 5.1 to 5.3, 5.5 8 Hours

Module – 4
TREES(Cont..): Binary Search trees, Selection Trees, Forests, Representation of Disjoint sets, Counting
Binary Trees,
GRAPHS: The Graph Abstract Data Types, Elementary Graph Operations
Text Book: Chapter-5: 5.7 to 5.11 Chapter-6: 6.1, 6.2

Module – 5
HASHING: Introduction, Static Hashing, Dynamic Hashing
PRIORITY QUEUES: Single and double ended Priority Queues, Leftist Trees
INTRODUCTION TO EFFICIENT BINARY SEARCH TREES: Optimal Binary Search Trees
Text Book: Chapter 8: 8.1 to 8.3 Chapter 9: 9.1, 9.2 Chapter 10: 10.1
Department of CSE 3
Textbook and References

Textbooks:
1. Ellis Horowitz and Sartaj Sahni, Fundamentals of Data Structures in C, 2nd Ed, Universities Press, 2014.
Reference Books:
1. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1st Ed, McGraw Hill,2014.
2. Gilberg & Forouzan, Data Structures: A Pseudo-code approach with C, 2nd Ed, Cengage Learning,2014.
3. Reema Thareja, Data Structures using C, 3rd Ed, Oxford press, 2012.
4. Jean-Paul Tremblay & Paul G. Sorenson, An Introduction to Data Structures with Applications, 2nd Ed,
McGraw Hill, 2013
5. A M Tenenbaum, Data Structures using C, PHI, 1989
6. Robert Kruse, Data Structures and Program Design in C, 2nd Ed, PHI, 1996.

Department of CSE 4
COURSE OBJECTIVE
After studying this course, students will be able to:
Course Learning Objective
1. To explain fundamentals of data structures and their applications.

2. To illustrate representation of Different data structures such as Stack, Queues,


LinkedLists, Trees and Graphs.

3. To Design and Develop Solutions to problems using Linear Data Structures.

4. To discuss applications of Nonlinear Data Structures in problem solving.

5. To introduce advanced Data structure concepts such as Hashing and Optimal


BinarySearch Trees

Department of CSE 5
COURSE OUTCOMES
After studying this course, students will be able to:
Course Outcomes
1. Explain different data structures and their applications.

2. Apply Arrays, Stacks and Queue data structures to solve the given problems.

3. Use the concept of linked list in problem solving.

4. Develop solutions using trees and graphs to model the real-world problem.

5. Explain the advanced Data Structures concepts such as Hashing Techniques and
Optimal Binary Search Trees.

Department of CSE 6
Module – 1
INTRODUCTION TO DATA STRUCTURES: Data Structures,
Classifications (Primitive & Non-Primitive), Data structure
Operations, Review of pointers and dynamic Memory
Allocation,

ARRAYS and STRUCTURES: Arrays, Dynamic Allocated Arrays,


Structures and Unions, Polynomials, Sparse Matrices,
representation of Multidimensional Arrays, Strings

STACKS: Stacks, Stacks Using Dynamic Arrays, Evaluation and


conversion of Expressions

Department of CSE 7
Data Structures
 Data: Collection of raw facts.

What is Data Structure ?

1. Data structure is representation of the logical relationship existing


between individual elements of data.
2. A data structure is a way to store and organize data in order to facilitate
the access and modifications
3. Data Structure are the method of representing of logical relationships
between individual data elements related to the solution of a given
problem

Department of CSE 8
Classifications (Primitive & Non Primitive)

Department of CSE 9
Classifications (Primitive & Non Primitive)

Department of CSE 10
Selection of Data Structure
The choice of particular data structure depends on two consideration:
• It must be rich enough in structure to represent the relationship between data
elements
• The structure should be simple enough that one can effectively process the data
when necessary

Types of Data Structure


Linear: In Linear data structure, values are Non-Linear: The data values in this
arrange in linear fashion. structure are not arranged in order.
• Array: Fixed-size • Hash tables: Unordered lists which use a
• Linked-list: Variable-size ‘hash function’ to insert and search
• Stack: Add to top and remove from top • Tree: Data is organized in branches.
• Queue: Add to back and remove from front • Graph: A more general branching structure,
• Priority queue: Add anywhere, remove the highest with less strict connection conditions than for
priority a tree
Department of CSE 11
Pointers
In How Many Ways Can You Access Variables?
You can access a variable in two ways.
• Direct Access: You can directly use the variable name to access the variable.
• Indirect Access: You use a pointer to access that variable.
Variables are allocated at addresses in computer memory (address depends on computer / operating system)
Name of the variable is a reference to that memory address
A pointer variable contains a representation of an address of another variable
A pointer is a reference to another variable (memory location) in a program.
A pointer is a variable pointing to the address of another variable.
It is declared along with an asterisk symbol (*).
Syntax : datatype *var1
Pointers are one of the core components of the C programming language.
A pointer can also be used to store the memory address functions, or even other pointers.
What is a Pointer in C?
A pointer is defined as a derived data type that can store the address of other C variables or a memory
location. We can access and manipulate the data stored in that memory location using pointers.
Department of CSE 12
Pointers - Memory Management Functions
Dynamic Memory Allocation : Allow the program to allocate some variables (notably arrays), during the
program, based on variables in program (dynamically (at runtime))
Memory Management Functions :
calloc - routine used to allocate arrays of memory
malloc - routine used to allocate a single block of memory
realloc - routine used to extend the amount of space allocated previously
free - routine used to tell program a piece of memory no longer needed
Note: memory allocated dynamically does not go away at the end of functions, you MUST explicitly free it up
Array Allocation with calloc:
prototype: void * calloc(size_t num, size_t esize)
size_t : is a special type used to indicate sizes, generally an unsigned int num is the number of
elements to be allocated in the array
esize : is the size of the elements to be allocated generally use sizeof and type to get correct value an
amount of memory of size num*esize allocated on heap calloc returns the address of the
first byte of this memory generally we cast the result to the appropriate type if not enough
memory is available, calloc returns NULL
Department of CSE 13
Pointers - Memory Management Functions
Array Allocation with calloc: prototype: void * calloc(size_t num, size_t esize)
Releasing Memory (free)
float *nums;
int N; prototype: void free(void *ptr)
int I; • Memory at location pointed to by ptr is released (so we could
use it again in the future)
printf(“Read how many numbers:”);
• The space for array of size N allocated, when function ends,
scanf(“%d”,&N); variable nums goes away, but the space nums points at (the
nums = (float *) calloc(N,sizeof(float)); array of size N) does not (allocated on the heap) -
/* nums is now an array of floats of size furthermore, we have no way to figure out where it is)
N */ • Problem called memory leakage
for (I = 0; I < N; I++) {
printf(“Please enter number %d:“,I+1);
float *nums;
scanf(“%f”,&(nums[I])); int N;
} printf(“Read how many numbers:”);
scanf(“%d”,&N);
/* Calculate average, etc. */ nums = (float *) calloc(N, sizeof(float));
/* use array nums */
/* when done with nums: */
free(nums);
/* would be an error to say it again - free(nums) */

Department of CSE 14
Pointers - Memory Management Functions
Array Allocation with calloc: prototype: void * calloc(size_t num, size_t esize)
Array Allocation with malloc
prototype: void * malloc(size_t esize)
• Similar to calloc, except we use it to allocate a single block of the given size esize as with calloc,
memory is allocated from heap
• NULL returned if not enough memory available.
• Memory must be released using free once the user is done
• Can perform the same function as calloc if we simply multiply the two arguments of calloc together
malloc(N * sizeof(float)) is same as calloc(N,sizeof(float))
Increasing Memory Size with realloc :
prototype: void * realloc(void * ptr, size_t esize)
• ptr is a pointer to a piece of memory previously dynamically allocated.
• esize is new size to allocate (no effect if esize is smaller than the size of the memory block ptr points to
already) program allocates memory of size esize,
• then it copies the contents of the memory at ptr to the first part of the new piece of memory,
• Finally, the old piece of memory is freed up
Department of CSE 15
Pointers - Memory Management Functions
Dynamically Allocating 2D Arrays
• Can not simply dynamically allocate 2D (or higher) array
• Idea - allocate an array of pointers (first dimension), make each (Memory content) pointer point to a 1D
array of the appropriate size
• Can treat result as 2D array

float **A;
/* A is an array (pointer) of float pointers */
int I;
A = (float **) calloc(5,sizeof(float *));
/* A is a 1D array (size 5) of float pointers */
for (I = 0; I < 5; I++)
A[I] = (float *) calloc(4,sizeof(float));
/* Each element of array points to an array of 4 float
variables */
/* A[I][J] is the Jth entry in the array that the Ith
member of A points to */
Department of CSE 16
Pointers - Memory Management Functions
Dynamically Allocating 2D Arrays

Department of CSE 17
Introduction to Structures
• With array, we can only declare one data type per array.
• For different data type, we need another array declaration.
• It is single type aggregate data type.
• Struct overcomes this problem by declaring composite data
types which can consist different types.
• A structure is a collection of related data items stored in one place
and can be referenced by more than one names.
• These data items are different basic data types. So, the number of
bytes required to store them may also vary.
• A structure type is a user-defined composite type.
• It is composed of fields or members which can be different types.

Department of CSE 18
Introduction to Structures

• In order to use a structure, we must first declare a structure template.


• The variables in a structure are called elements or members.
• In C, you must explicitly use the struct keyword to declare a structure.
• You have the option of declaring variables when the structure type is
defined by placing one or more comma-separated variable names
between the closing brace and the semicolon.
• Structure variables can be initialized. The initialization for each variable
must be enclosed in braces.
• Both structure types and variables follow the same scope as normal
variables, as do all identifiers.

Department of CSE 19
Introduction to Structures
• If you define a structure within a function, then you can only use it within
that function.
• Likewise if you define a structure outside of any function then it can be
used in any place throughout your program.
• Example, to store and process a student’s record with the elements
chIdNum (identification number), chName, chGender and nAge, we
can declare the following structure,
struct student {
char chIdNum[5];
char chName[10];
tag
char chGender;
int nAge;
};
Department of CSE 20
Introduction to Structures
• Here, struct is a keyword that tells the compiler that a Tag
structure template is being declared and student is a tag that
identifies its data structure.
• Tag is not a variable; it is a label for the structure’s template.
• Note that there is a semicolon after the closing curly brace.
• A structure tag simply a label for the structure’s template but
you name the structure tag using the same rules for naming
variables.
• The previous sample template for the structure can be
illustrated as follow (note the different data sizes),

Department of CSE 21
Self-Referential STRUCTURES
• A Self-Referential Structure is one in which one or more of its components is a
pointer to itself.
• Self-referential structures usually require dynamic storage management
routines (malloc and free) to explicitly allocate and release memory.

Department of CSE 22
Implementation of polynomial using arrays

𝑷𝒐𝒍𝒚𝑨 = 𝟏𝟎𝒙𝟒 + 𝟓𝒙𝟐 +3x+5=0


𝑷𝒐𝒍𝒚𝑩 = 𝟑𝒙𝟑 + 𝟗𝒙𝟐 +15=0
𝑷𝒐𝒍𝒚𝑫 = 𝟏𝟎𝒙𝟒 + 𝟑𝒙𝟑 + 𝟏𝟒𝒙𝟐 + 𝟑𝒙+20=0

StartA FinishA StartD FinishD

Index 0 1 2 3 4 5 6 7 10 11 12 13
Coeff 10 5 3 5 3 9 15 10 3 14 3 20
Exp 4 2 1 0 3 2 0 4 3 2 1 0

StartB FinishB
Implementation of polynomial using arrays
𝑷𝒐𝒍𝒚𝑨 = 𝟏𝟎𝒙𝟒 + 𝟓𝒙𝟐 +3x+5=0
𝑷𝒐𝒍𝒚𝑩 = 𝟑𝒙𝟑 + 𝟗𝒙𝟐 +15=0
𝑷𝒐𝒍𝒚𝑫 = 𝟏𝟎𝒙𝟒 + 𝟑𝒙𝟑 + 𝟏𝟒𝒙𝟐 + 𝟑𝒙+20=0

StartA FinishA StartD FinishD

Index 0 1 2 3 4 5 6 7 10 11 12 13
Coeff 10 5 3 5 3 9 15 10 3 14 3 20
Exp 4 2 1 0 3 2 0 4 3 2 1 0

StartB FinishB
Implementation of polynomial using arrays
𝑷𝒐𝒍𝒚𝑨 = 𝟏𝟎𝒙𝟒 + 𝟓𝒙𝟐 +3x+5=0
𝑷𝒐𝒍𝒚𝑩 = 𝟑𝒙𝟑 + 𝟗𝒙𝟐 +15=0
𝑷𝒐𝒍𝒚𝑫 = 𝟏𝟎𝒙𝟒 + 𝟑𝒙𝟑 + 𝟏𝟒𝒙𝟐 + 𝟑𝒙+20=0

StartA FinishA StartD FinishD

Index 0 1 2 3 4 5 6 7 10 11 12 13
Coeff 10 5 3 5 3 9 15 10 3 14 3 20
Exp 4 2 1 0 3 2 0 4 3 2 1 0

StartB FinishB
Implementation of polynomial using arrays

StartD FinishD

Index 7 10 11 12 13

Coeff 10 3 14 3 20

Exp 4 3 2 1 0
What is Sparse Matrix?
A matrix which contains many zero entries or very few non-zero entries is called a
Sparse matrix.

Important Note: A sparse matrix can be represented in 1-Dimension,2-Dimension and


3- Dimensional array.

Sparse Matrix Representation


• An element within a matrix can characterize by using the triple <row,col,value> This means that,
an array of triples is used to represent a sparse matrix.
• Organize the triples so that the row indices are in ascending order.
• The operations should terminate, so we must know the number of rows and columns, and the
number of nonzero elements in the matrix.

Department of CSE 27
Implementation of Sparse Matrix using arrays
Implementation of Sparse Matrix using arrays

Department of CSE 29
Implementation of Sparse Matrix using arrays

Department of CSE 30
Implementation of Sparse Matrix using arrays

Department of CSE 31
Implementation of Transpose of Sparse Matrix

Department of CSE 32
General - String taxonomy

Length-controlled Strings: stores the Delimited Strings: adds a


number of characters in the string delimiter at the end of string

Department of CSE 33
Strings in C
C uses variable-length, delimited strings.
• String is not an explicit type, instead strings are maintained as arrays of characters
• Representing strings in C
• stored in arrays of characters
• array can be of any length
• end of string is indicated by a delimiter, the zero or null character ‘\0‘
Storing strings:
• A character, in single quotes:
char s1= `h`; //Takes only one byte of storage.
• On the other hand, the character string:
char s2[2]=“H“; //Takes two bytes of storage.
• An empty string
char s3[]= “”; //Takes only one byte of Storage storage.

Department of CSE 34
Declaring Strings: Strings in C
• Case (a) has the ceiling of 8-characters plus a delimiter
• However, case (b) allows length to be defined before usage and Memory for strings must
be allocated before the string can be used.

Initializing Strings:
• char str[9]= “Good Day”;
• char str[9]= {„G„,„o„,„o„,„d„,‟ „,„D„,„a„,„y„,„\0„};
• char month[]= “January”;
• char *pStr = “Good Day”;
Department of CSE 35
Strings in C
String Function

Department of CSE 36
Implementation of Pattern Search in String using arrays
String A A B A A C A A D A A B A A B A

Pattern A A D A

A A B A A C A A D A A B A A B A

Start EndP
A A B A A C A A D A A B A A B A

A A B A A C A A D A A B A A B A
Start EndP

Start EndP A A B A A C A A D A A B A A B A

A A B A A C A A D A A B A A B A
Start EndP

Start EndP Match Found Returns the index 6

A A B A A C A A D A A B A A B A

Start EndP
Implementation of Pattern Search in String using arrays
String A A B A A C A A D A A B A A B A

Pattern A A D A
Implementation of Pattern Search in String using arrays
Another Technique of searching a pattern

Pattern to Search B A B B A C

String
A B A B A A B A C C A B A B B A C
Using Failure Function : It allows us not to waste prior comparisons done when we have
already matched particular portion of the pattern until a particular
location and had some but not all the letters match.
Index 0 1 2 3 4 5
Failure Function :
P[j] B A B B A C
f(j) 0 0 1 1 2 0

f(2) is 1, because the prefix “A" is a suffix of “BAB"


f(4) is 2, because the prefix “A" is a suffix of “BABA"

if the failure function is positive, then we do NOT need to check the beginning letters of the
pattern because we know they match already!
Implementation of Pattern Search in String using arrays
Another Technique of searching a pattern
Implementation of Pattern Search in String using arrays
Another Technique of searching a pattern

Pattern to Search B A B B A C

String
A B A B A A B A C C A B A B B A C

A B A B A A B A C C A B A B B A C
B A B B A C
B A B B A C
B A B B A C
B A B B A C
B A B B A C
B A B B A C
B A B B A C
B A B B A C
B A B B A C
Implementation of Pattern Search in String using arrays
Another Technique of searching a pattern
Implementation of Pattern Search in String using arrays
Another Technique of searching a pattern
Implementation of STACKS using arrays
• A STACK is an ordered list in which insertions and deletions are
made at one end called the top.
• If we add the elements A, B, C, D, E to the stack, in that order, then E
is the first element we delete from the stack
• A stack is also known as a Last-In-First-Out (LIFO) list.

Department of CSE 44
Implementation of STACKS using arrays
Implementation of STACK Using Arrays

E
Last element that
was pushed is the
first to be
D popped.
C
B
A

Department of CSE 45
Implementation of STACKS using arrays
• The STACK Abstract Data Type (ADT)

Department of CSE 46
Implementation of STACKS using arrays
Implementation of STACK Using Arrays

Department of CSE 47
Implementation of STACKS
Implementation of STACK Using Dynamic Arrays

Department of CSE 48
Prefix, Postfix, Infix Notation

Department of CSE 49
Expressions Notation
Infix Notation
To add A, B, we write
A+B

Postfix Notation
To multiply A, B, we write
A*B
The operators ('+' and '*') go in between the
operands ('A' and 'B') This is Infix notation. Another alternative is to put the operators
after the operands as in
Prefix Notation and
AB+

Instead of saying "A plus B", we could say "add A,B " AB*
and write This is Postfix notation.
+AB
"Multiply A,B" would be written
*AB
This is Prefix notation.
Department of CSE 50
Expressions Evaluation
Infix Notation
Evaluate 2+3*5.
+ First: (2+3)*5 = 5*5 = 25
* First: 2+(3*5) = 2+15 = 17
Infix notation requires Parentheses. Postfix Notation
Prefix Notation
235*+=
+2*35=
=235*+
=+2*35
= 2 15 + = 17
= + 2 15 = 17
23+5*=
*+235=
=23+5*
=*+235
= 5 5 * = 25
= * 5 5 = 25
No parentheses needed here either!
No parentheses needed!

Infix is the only notation that requires parentheses in order to change the order in which the
operations are done.
Department of CSE 51
Fully Parenthesized Expression
A FPE has exactly one set of Parentheses enclosing
Infix to Prefix Conversion
each operator and its operands.
Which is fully parenthesized? Move each operator to the left of its operands &
(A+B)*C remove the parentheses:
( ( A + B) * ( C + D ) )
( ( A + B) * C )
( ( A + B) * ( C ) )
( ( + A B) * ( C + D ) )
Infix to Postfix Conversion
( * ( + A B) ( C + D ) )

( * ( + A B) ( + C D ) )

*+A B +C D
Order of operands does not change!

Department of CSE 52
Evaluation of Expressions
Evaluating postfix expressions
• The standard way of writing expressions is known as infix notation
• binary operator in-between its two operands
• Infix notation is not the one used by compilers to evaluate expressions
• Instead compilers typically use a parenthesis-free notation referred to as postfix
Postfix:
no parentheses,
no precedence

Department of CSE 53
Postfix Evaluation of Expressions

string: 6 2/3-4 2*+#

we make a single left-to-right scan of it


add the string with the operator

6 2 / 3 - 4 2 * + #
2 2
notnot
an an operator,
operator,
is not
an put
an
isisan
an
put
into
operator,
not
into
isan
an
an the
operator,
operator,
end
the
pop put
twointo
operator,
operator,
ofoperator,
operator,string,
pop
pop two the
elements
pop
put
pop
two two
into
elements
the of
elements
the
stack
elements of
and getof
ofthe
the
not put into the
stack thestack
stack stack
stack
stack the
stack
answer
stack stack
1 4*2
2
3
4
0
6/2-3+4*2
6/2-3
6/2
6
the answer is top
STACK

6/2-3 + 4*2 now, top must +1


-2
-1
Department of CSE 54
Postfix Evaluation of Expressions
Evaluating postfix expressions is much simpler than the evaluation of infix
expressions:
• There are no parentheses to consider.
• To evaluate an expression we make a single left-to-right scan of it.
• We can evaluate an expression easily by using a stack

Figure shows this processing when the input is nine


character string 6 2/3-4 2*+#

Department of CSE 55
Postfix Evaluation of Expressions
• Representation
• We now consider the representation of both the stack and the expression

Get Token

Department of CSE 56
Postfix Evaluation of Expressions

Department of CSE 57
Infix to Postfix Conversion
• We use a stack
• When ( is found push the symbol on stack (lowest precedence)
• When an operand is read, append to postfix expression
• When an operator is read, if input symbol precedence is lower than the precedence
of symbol on top of stack
• Pop symbol and append it to postfix expression,
• until the top symbol on the stack has an element of lower precedence
• Then push it (operator )
• When ) is found,
• pop elements from stack and append to postfix expression
• until we find the (
• When an operator is read, if input symbol precedence is higher than the precedence
of symbol on top of stack , then push the symbol on to the stack
• When we reach the end of input, pop symbol and append the postfix expression
until the stack is empty
Department of CSE 58
Infix to Postfix Conversion Example 1
• 3+4*5/6 • 3+4*5/6 • 3+4*5/6 • 3+4*5/6 • 3+4*5/6
• Stack: • Stack: • Stack: + • Stack: +
• Output: • Output: 3 • Output: 3 • Output: 3 4

• 3+4*5/6 • 3+4*5/6 • 3+4*5/6 • 3+4*5/6


• Stack: + * • Stack: + • Stack: + / • Stack: + /
• Output: 3 4 • Output: 3 4 5 * • Output: 3 4 5 * • Output: 3 4 5 * 6

• 3+4*5/6 • 3+4*5/6
• Stack: + • Stack:
• Output: 3 4 5 * 6 / • Output: 3 4 5 * 6 / +
• Done!
Department of CSE 59
Infix to Postfix Conversion Example 1
(300+23)*(43-21)/(84+7)
• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)
• Stack: • Stack: ( • Stack: (
• Output: • Output: • Output: 300
• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)
• Stack: ( + • Stack: ( + • Stack: (
• Output: 300 • Output: 300 23 • Output: 300 23 +
• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)
• Stack: * • Stack: * ( • Stack: * (
• Output: 300 23 + • Output: 300 23 + • Output: 300 23 + 43
• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)
• Stack: * ( - • Stack: * ( - • Stack: * (
• Output: 300 23 + 43 • Output: 300 23 + 43 21 • Output: 300 23 + 43 21 -
Department of CSE 60
Infix to Postfix Conversion Example 2
(300+23)*(43-21)/(84+7)
• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)
• Stack: * • Stack: • Stack: /
• Output: 300 23 + 43 21 - • Output: 300 23 + 43 21 - * • Output: 300 23 + 43 21 - *

• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)


• Stack: / ( • Stack: / ( • Stack: / ( +
• Output: • Output: • Output:
300 23 + 43 21 - * 300 23 + 43 21 - * 84 300 23 + 43 21 - * 84
• (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7) • (300+23)*(43-21)/(84+7)
• Stack: / ( + • Stack: / ( • Stack: /
• Output: • Output: • Output:
300 23 + 43 21 - * 84 7 300 23 + 43 21 - * 84 7 + 300 23 + 43 21 - * 84 7 +

Department of CSE 61
Infix to Postfix Conversion Example 2

• (300+23)*(43-21)/(84+7)
• Stack:
• Output: 300 23 + 43 21 - * 84 7 + /

• Done!
Example 3
(4+8)*(6-5)/((3-2)*(2+2)) Output: 4 8 + 6 5 - * 3 2 – 2 2 + * /

Department of CSE 62
Infix Expressions to Postfix Expression
We can describe an algorithm for producing a postfix expression from an infix
one as follows
(1) Fully parenthesize expression
a/b-c+d*e-a*c
((((a / b) - c) + (d * e)) - (a * c))
(2) All operators replace their corresponding right parentheses

((((a / b) - c) + (d * e)) - (a * c))


* *
two passes
/ - + -
(3)Delete all parentheses
ab/c-de*+ac*-
The order of operands is the same in infix and postfix

Department of CSE 63
Evaluation of Expressions
Understanding the meaning of these or any other expressions and statement
assume a = 4, b = c = 2, d = e = 3 in the statement,
finding out the value of x = a/b – c + d*e - a*c

Interpretation 1: ((4/2)-2)+(3*3)-(4*2) = 0+8+9 = 1


Interpretation 2: (4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.66666…

we would have written differently by using parentheses to change the order of


evaluation:
x = ((a/(b - c+d))*(e - a)*c
How to generate the machine instructions corresponding to a given expression?
precedence rule + associative rule
Department of CSE 64
Precedence hierarchy and associative for C

Department of CSE 65
Infix Expressions to Postfix Expression
Algorithm to convert from infix to postfix
• Assumptions:
• operators: (, ), +, -, *, /, %
• operands: single digit integer or variable of one character
1. Operands are taken out immediately
2. Operators are taken out of the stack as long as their in-stack precedence (ISP)
is higher than or equal to the incoming precedence (ICP) of the new operator
3. „(„ has low ISP, and high ICP
op ( ) + - * / % eos
ISP 0 19 12 12 13 13 13 0
ICP 20 19 12 12 13 13 13 0
precedence stack[MAX_ STACK_ SIZE];
/* ISP and ICP arrays -- index is value of precedence lparen, rparen, plus, minus, times,
divide, mod, eos */
static int ISP [ ] = {0, 19, 12, 12, 13, 13, 13, 0};
static int ICP [ ] = {20, 19, 12, 12, 13, 13, 13, 0};

Department of CSE 66
Infix Expressions to Postfix Expression

a * ( b + c ) / d #
operand, operator
operand,
print
operator operator
out operand,
print print
out operator
operand,
operator eosout
out print

pushthe
pop into
stack
the stack
and printout
2
+
operator
the isp of“)”,
“(
“/ “pop
“+“ 13
0 and
is 12and
andprint
the
theicp
out
icpofof
until
“*“(
“*““““(”
isis
is13
13
20
1
(

output 0
*/
top
a b c + * d / stack
now, top must +1
-1

Department of CSE 67
Infix Expressions to Postfix Expression

+ - * / ( #
input per
I(x)
1 1 3 3
stack per
S(x)
2 2 4 4 0 -1

Department of CSE 68
Infix Expressions to Postfix Expression
Example [Simple expression]: Simple expression a+b*c, which yields abc*+ in postfix.

Example [Parenthesized expression]: The expression a*(b+c)*d, which yields abc+*d*


in postfix

Department of CSE 69
Infix Expressions to Postfix Expression

Department of CSE 70
Thank You

Department of CSE 71

You might also like