Data Structures Syllabus for CSE BCS304
Data Structures Syllabus for CSE BCS304
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.
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.
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,
Department of CSE 7
Data Structures
Data: Collection of raw facts.
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
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
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
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
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
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.
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
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
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
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
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
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
• 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 - *
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
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
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.
Department of CSE 69
Infix Expressions to Postfix Expression
Department of CSE 70
Thank You
Department of CSE 71