Data Structues Unit - I
Data Structues Unit - I
Course Objectives:
Exploring basic data structures such as stacks and queues.
Introduces a variety of data structures such as hash tables, search trees, tries, heaps, graphs.
Introduces sorting and pattern matching algorithms
Course Outcomes:
Ability to select the data structures that efficiently model the information in a problem.
Ability to assess efficiency trade-offs among different data structure implementations or combinations.
Implement and know the application of algorithms for sorting and pattern matching.
Design programs using a variety of data structures, including hash tables, binary and general tree structures, search trees,
tries, heaps, graphs, and AVL-trees.
UNIT - I
Introduction to Data Structures, abstract data types, Linear list – singly linked list implementation, insertion, deletion and searching
operations on linear list, Stacks-Operations, array and linked representations of stacks, stack applications, Queues-operations, array and linked
representations.
UNIT - II
Dictionaries: linear list representation, skip list representation, operations - insertion, deletion and searching.
Hash Table Representation: hash functions, collision resolution-separate chaining, open addressing-linear probing, quadratic probing,
double hashing, and rehashing, extendible hashing.
UNIT - III
Search Trees: Binary Search Trees, Definition, Implementation, Operations- Searching, Insertion and Deletion, AVL Trees, Definition,
Height of an AVL Tree, Operations – Insertion, Deletion and Searching, Red –Black, Splay Trees.
II
UNIT - IV
Graphs: Graph Implementation Methods. Graph Traversal Methods.
Sorting: Heap Sort, External Sorting- Model for external sorting, Merge Sort.
UNIT - V
Pattern Matching and Tries: Pattern matching algorithms-Brute force, the Boyer –Moore algorithm, the Knuth-Morris-Pratt algorithm,
Standard Tries, Compressed Tries, and Suffix tries.
TEXTBOOKS:
1. Fundamentals of Data Structures in C, 2nd Edition, E. Horowitz, S. Sahni and Susan Anderson Freed, Universities Press.
2. Data Structures using C – A. S. Tanenbaum, Y. Langsam, and M.J. Augenstein, PHI/Pearson Education.
REFERENCE BOOKS:
1. Data Structures: A Pseudocode Approach with C, 2nd Edition, R. F. Gilberg and B.A. Forouzan, Cengage Learning.
III
LESSON PLAN
1
collision resolution-
separate chaining,
open addressing-
linear probing,
quadratic probing,
double hashing, and
rehashing, extendible
hashing.
Week 4 II Dictionaries: linear Data Structures: A
list representation, Pseudocode Approach
skip list with C, 2nd Edition, R. F.
Gilberg and B.A.
representation,
Forouzan, Cengage
operations - insertion, Learning.
deletion and
searching.
Hash Table
Representation:
hash functions,
collision resolution-
separate chaining,
open addressing-
linear probing,
quadratic probing,
double hashing, and
rehashing, extendible
hashing.
Week 5 III Search Trees: Data Structures using C
Binary Search Trees, – A. S. Tanenbaum, Y.
Definition, Langsam, and M.J.
Augenstein,
Implementation,
PHI/Pearson Education
Operations-
Searching, Insertion
and Deletion, AVL
Trees, Definition,
Height of an AVL
Tree, Operations –
Insertion, Deletion
and Searching, Red –
Black, Splay Trees
Week 6 III Search Trees: Data Structures using C
Binary Search Trees, – A. S. Tanenbaum, Y.
Definition, Langsam, and M.J.
Augenstein,
Implementation,
Operations-
2
Searching, Insertion PHI/Pearson Education
and Deletion, AVL
Trees, Definition,
Height of an AVL
Tree, Operations –
Insertion, Deletion
and Searching, Red –
Black, Splay Trees
Week 7 IV Graphs: Graph Data Structures using C
Implementation Methods. – A. S. Tanenbaum, Y.
Graph Traversal Methods. Langsam, and M.J.
Sorting: Heap Sort, Augenstein,
External Sorting- Model PHI/Pearson Education
for external sorting,
Merge Sort.
3
UNIT – 1
INTRODUCTION
Software Development Life Cycle(SDLC)
Requirements
↓
Analysis
↓
Designing
↓
Coding / Implementation
↓
Debugging
↓
Testing
↓
Maintenance
↓
Documentation
4
WHAT IS DATA?
Example : c=a+b
Ex: Arrays
5
Instead of creating multiple variables of same data type. Why not
create an array to store all the values.
Examples:
6
USER DEFINED DATA TYPES:
The operations and values of user defined data types are not
specified in the language itself but is specified by the user.
ADTs are like user defined data types which defines operations on
values using functions without specifying what is there inside the
function and how the operations are performed.
7
Think of ADT as a black box which hides the inner structure and
design of the data type from the user.
Why ADT?
ADVANTAGE:
8
DEFINITION OF DATA STRUCTURE:
9
Advantages of Data Structures:
Data structures
Ex.: Arrays
Queues
Stack
Linked list.
10
Ex.: Tree’s , Graph’s.
An ideal data structure could be the one that takes the least
possible time for all its operations and consumes the less memory
space.
11
Non-primitive data structure
Linear non-linear
(sequential) (random)
1. Arrays 1. Trees
2. Lists i. binary
i. Singly LL ii. BST
ii. Doubly LL 2. Graphs
iii. Circular LL
3. Stacks
4. Queues
Arrays :
12
Operations to be performed onto arrays
1) Insertion
2) Deletion
3) Searching
a. Linear search
b. Binary search
4) Sorting
a. Bubble sort
b. Quick sort
c. Merge sort ., etc.,
Advantages :
Disadvantages :
13
2) Insertion and deletion
LINKED LIST :
Representation of node :
data next
14
If it contains 3 nodes
Head Tail
declaration of node:
struct node
{
int data;
struct node *next;
}*new;
15
Create a list
10 NULL
Head,tail
10 2000 20 NULL
Head tail
Head tail
16
→Sample code to create list :-
do
{ new=(struct node *)malloc(sizeof(struct node));
scanf(“%d”,&value);
new→data=value;
new→next=NULL;
if(head==NULL)
{
head=new;
tail=new;
}
else
{
tail→next=new;
tail=new; }
printf(“Y-continue N-exit”);
fflush()stdin;
scanf(“%c”,&ch);
}while(ch==’Y’);
Trace:::: data – 20, 30, 50
Address – 2000, 3000, 5000.
Head tail
III) value=50(new)
17
Display the list:
Head tail
temp=head;
while(temp!=NULL)
{
printf(“%d”,temp→data);
temp=temp→next;
}
18
INSERTION OF ELEMENT IN THE EXISTING LINKED LIST
Head tail
→Sample code
scanf(“%d”,&value); //5
new→data=value;
new→next=head;
head=new;
5 1000 30 NULL
10 2000 20 3000
Head tail
19
2) INSERTION OF THE ELEMENT AT THE END OF THE
LIST.
5 1000 30 NULL
10 2000 20 3000
Head tail
→sample code
Head tail
20
3) INSERTION OF THE ELEMENT AT SPECIFIED
LOCATION.
5 1000 30 NULL
10 2000 20 3000
Head tail
Sample code
Scanf(“%d”,&value);
Scanf(“%d”,&pos);
temp=head;
for(i=0;i<pos-1;i++)
{ temp=temp→next; }
new→data=value;
new→next=temp→next;
temp→next=new;
5 1000 30 NULL
15 2000 20 3000
10 1500
Head tail
21
DELETION OF AN ELEMENT FROM THE EXISTING LINKED
LIST:
5 1000 30 NULL
15 2000 20 3000
10 1500
Head tail
Head tail
Sample code:
temp=head;
head=head→next;
temp→next=NULL;
22
3. now, we are disconnecting this ‘temp’ variable by removing
the link. (temp→next=NULL)
5 1000 20 3000
10 1500 15 2000
Head tail
Sample code:
temp=head;
while(temp→next!=tail)
{
temp=temp→next;
}
temp→next=NULL;
tail=temp;
23
III. DELETE ELEMENT AT SPECIFIED POSITION:
5 1000 30 NULL
15 2000 20 3000
10 1500
Head tail
Index 0 1 2 3 4
Sample code:
scanf(“%d”,&pos);
temp=head;
for(i=0;i<pos-1;i++)
{
temp=temp→next;
}
temp→next=temp→next→next;
5 1000 30 NULL
10 2000 20 3000
Head tail
Index 0 1 2 3 4
24
COUNTING NUMBER OF NODES IN THE LINKED LIST
5 1000 30 NULL
15 2000 20 3000
10 1500
Head tail
Sample code
int count=0;
temp=head;
while(temp!=NULL)
{
count++;
temp=temp→next;
}
printf(“count=%d”,count);
25
ADVANTAGES OF LINKED LIST
1. no wastage of memory
2. no need to use contiguous memory locations
3. insertion and deletion becomes simple
4. linear data structure like stack , queue can be implemented
using LL
5. can store any type of data in node
26
CIRCULAR LINKED LIST :
Head tail
Head tail
struct node
{
int data;
struct node *next;
}*new,*head,*tail,*temp;
27
→create a circular linked list
scanf(“%d”,&value);
new→data=value; new node
new→next=NULL;
30 NULL
if(head==NULL)
{
head=new;
tail=new;
}
else
{
tail→next=new;
tail=new;
tail→next=head;
}
printf(“’y’ continue ‘n’ exit”);
fflush(stdin);
scan(“%c”,&ch);
}while(ch==’y’);
Trace:
I.
10 NULL
10 2000 20 1000
Head tail
28
III.
Head tail
Sample code:
temp=head;
while(temp→next!=NULL)
{
printf(“%d”,temp→data);
temp=temp→next;
}
printf(“%d”,temp→data);
//output : 10 20 30
29
INSERTION
existing LL::
Head tail
Output::
Head tail
Sample code:
30
2) insertion at the end of the list:
existing LL::
Head tail
Output::
Head tail
Sample code:
31
3) Insertion at the specified location of the list
existing LL::
Index 0 1 2
Head tail
Output::
Head tail
Sample code:
scanf(“%d”,&pos); //2
new=(struct node *)malloc(sizeof(struct node));
scanf(“%d”,&value);
new→data=value;
temp=head;
for(i=0;i<pos-1;i++)
{
temp=temp→next;
}
new→next=temp→next;
temp→next=new;
32
DELETION:
Existing LL
Head tail
Output
Head tail
Sample code:
temp=head;
head=head→next;
tail→next=head;
temp→next=NULL;
33
2) Deletion at the end of the LL:
Existing LL
Head tail
Output
Head tail
Sample code:
temp=head;
while(temp→next!=tail)
{ temp=temp→next; }
tail→next=NULL;
temp→next=head;
tail=temp;
34
3) Deletion of the element at the specified location:
Existing LL
Index 0 1 2 3
Head tail
Output
Head tail
Sample code:
scanf(“%d”,&pos);
temp=head;
for(i=0;i<pos-1;i++)
{
temp=temp→next;
}
temp→next=temp→next→next;
35
DOUBLY LINKED LIST:
Representation of node
If it is head, if it is tail,
Then ., prev=NULL then ., next=NULL
Ex:-
1000 20 3000 2000 30 NULL
NULL 10 2000
36
Create node using structure:
struct node
{
struct node *prev;
int data;
struct node *next;
}*new,*head,*tail,*temp;
Create a list:
do
{
new=(struct node *)malloc(sizeof(struct node));
scanf(“%d”,&value);
new→prev=NULL;
new→data=value;
new→next=NULL;
if(head==NULL)
{
head=tail=new;
}
else
{
tail→next=new;
new→prev=tail;
tail=new;
}
Printf(“’y’ continue ‘n’ exit”);
}while(ch==’y’);
37
Trace:
I) NULL 10 2000
1000(new,head,tail)
Head tail
III)
1000 20 3000 2000 30 NULL
NULL 10 2000
temp=head;
while(temp!=NULL)
{
printf(“%d”,temp→data);
temp=temp→next;
}
// 10 20 30
38
INSERTION
Existing LL.,
Output.,
Sample code:
39
2) Insertion at the end of the list:
Existing LL.,
Output.,
Sample code:
40
3) Insertion at the specified location of the list:
Existing LL.,
Output.,
Sample code:
41
DELETION
Existing LL:
temp=head;
head=head→next;
temp→next=NULL;
head→prev=NULL;
42
2) Deletion at the end of the list:
Existing LL:
temp=tail;
tail=tail→prev;
temp→prev=NULL;
tail→next=NULL;
43
3) Deletion at the specified location:
Existing LL:
scanf(“%d”,&pos);
temp=head;
for(i=0;i<pos-1;i++)
temp=temp→next;
temp→next=temp→next→next;
temp→next→prev=temp;
44
STACKs
PUSH POP
TOP=-1
STACK(empty)
45
If we inserted 1 element into stack say 10. Then top=1.,i.e.,top
will be incremented by one.
PUSH
TOP=1 10
STACK
PUSH
TOP=2 20
10
STACK
46
PUSH
TOP=3 30
20
10
STACK
POP
TOP=2 20
10
STACK
47
➔ If we want to PUSH(insert) elements, then we have to
increment TOP first then insert element into stack.
➔ If we want to POP(delete) element, then we have to
decrement TOP first then delete element.
1) OVERFLOW CONDITION:
PUSH TOP=6 60
50
40
30
20
10
STACK(full)
48
2)UNDERFLOW CONDITION:
POP
STACK(empty)
TOP=-1
49
Implementation of stacks using arrays
/* SIZE=10
TOP=-1
STACK[10] */
push()
{
scanf(“%d”,&ele);
if(TOP==SIZE-1)
{
printf(“overflow”);
}
else
{
TOP++;
STACK[TOP]=ele;
}
}
50
POP() → To delete the element from the stack
(decrement’s TOP by 1)
pop()
{
if(TOP==-1)
printf(“underflow”);
else
{
ele=STACK[TOP];
TOP--;
}
}
display()
{
if(TOP!=-1)
{
for(i=TOP;i>=0;i++)
{
printf(“%d”,STACK[i]);
}
}
else
printf(“empty stack”);
}
51
Implementation of stack using linked list:
PUSH POP
TOP=-1
struct node
{
int data;
struct node *next;
}*new,*temp,*top;
52
INSERTION of ELEMENT into STACK
push()
{
new=(struct *node)malloc(sizeof(struct node));
scanf(“%d”,&ele);
if(top==NULL)
{
new→data=ele;
new→next=NULL;
top=new;
}
else
{
new→data=ele;
new→next=top;
top=new;
}
}
TOP
53
DELETION of ELEMENT from the STACK
pop()
{
If(top==NULL)
Printf(“stack is empty”);
Else
{
Temp=top;
Printf(“%d is deleted ”,top→data);
Top=top→next;
Temp→next=NULL;
Free(temp);
}
}
54
Applications of Stack:
NOTE:
+,-,*,/ … Operators
INFIX EXPRESSION
<operand>operator<operand>
Ex: A+B
POSTFIX EXPRESSION
<operand1><operand2>operataor
EX: AB+
PREFIX EXPRESSION
Operator<operand1><operand2>
EX: +AB
55
1) CONVERSION OF INFIX TO POSTFIX EXPRESSION
56
Ex: INFIX EXPRESSION → A+B*C
+ ABC *+
POSTFIX EXPRESSION
STACK
STEPS:
1) If the character is an operand, then simply PUSH into the
stack
2) If the character is an operator, POP top two operands from
the stack, perform calculation and PUSH the result back into
stack.
3) After reading all the characters from the POSTFIX EXPRESSION
stack will be having only the value which is the result.
57
EX: POSTFIX EXPRESSION → 562*+
Character stack
5
58
* 6 * 2 = 12
12
+ 5 + 12 = 17
17
EX2: 4 6 7 2 + - *
EX3: 5 4 6 + * 4 9 3 / + *
3)Balancing symbols
(
[ expressions
{ → block & statements
59
So, in order to overcome the above mentioned drawback we
implement it using stacks.
Steps:
1) Read character from expression.
2) If character is open symbol (,[,{ → push symbol into stack
3) If character is closed symbol ),],} →
a. Check if stack is empty , if TRUE expression is
unbalanced
b. If stack is not empty, pop the symbol from stack with the
symbol which is read. If doesn’t matches expression is
unbalanced else repeat the process
4) After reading all characters of expression still stack is not
empty → unbalanced.
EX: [(a+b)(a-b)]
60
QUEUE
• Two ends
1) FRONT – points to starting element
2) REAR – points to ending element
• Representation
QUEUE
Front Rear
(DEQUEUE) (ENQUEUE)
61
IMPLEMENTATION OF QUEUE USING ARRAYS
QUEUE[];
SIZE = 10
FRONT = REAR = -1
ENQUEUE()
{
If(rear==size-1)
printf(“overflow”);
Else
{
scanf(“%d”,&ele);
If(front==-1)
front=0;
rear++;
Queue[rear]=ele;
}
}
QUEUE
Front=Rear=-1
QUEUE
10
Front,Rear
62
QUEUE
10 20
Front Rear
QUEUE
10 20 30
Front Rear
QUEUE
10 20 30 40
Front Rear
63
DEQUEUE()
{
If(rear==-1||front>rear)
{
Printf(“underflow”);
}
Else
{
Ele=queue[front];
Printf(“%d is deleted”,ele) ;
Front++;
}
}
INITIALLY.,
QUEUE
10 20 30 40
Front Rear
QUEUE
20 30 40
Front Rear
QUEUE
30 40
Front Rear
64
QUEUE
40
Front,Rear
QUEUE
Rear Front
DISPLAY()
{
If(rear==-1||front>rear)
Printf(“empty queue”);
Else
{
For(i=front;i<=rear;i++)
{
Printf(“%d”,queue[i]);
}
}
}
QUEUE
10 20 30 40
Front Rear
65
QUEUE IMPLEMENTATION USING LINKED LIST
struct node
{
int data;
struct node *next;
}*new,*front,*rear,*temp;
//initially
Front=rear=NULL(-1);
66
Initially , no node(no element) → front=rear=NULL
new
10 NULL
1000
Front,rear
new
20 NULL
10 2000
1000 2000
Front rear
new
20 3000 30 NULL
10 2000
new
67
Dequeue() // deletion done at front end ➔ front++
{
If(front==NULL)
printf(“queue is empty”);
else
{
Temp=front;
Front=front→next;
Temp→next=NULL;
Free(temp);
}
}
68
Display()
{
Temp=front;
If(front==NULL)
{
Printf(“queue is empty”);
}
Else
{
While(temp!=NULL)
{
Printf(“%d”,temp→data);
Temp=temp→next;
}
}
}
Output: 10 20 30 40
69
UNIT -2
DICTIONARIES:
Dictionary is a collection of pairs of key and value where every value is associated with the
corresponding key.
Basic operations that can be performed on dictionary are:
1. Insertion of value in the dictionary
2. Deletion of particular value from dictionary
3. Searching of a specific value with the help of key
class dictionary
{
private:
int k,data;
struct node
{
public: int key;
int value;
struct node *next;
} *head;
public: dictionary();
void insert_d( );
void delete_d( );
void display_d( );
void length();
};
Insertion of new node in the dictionary:
Consider that initially dictionary is empty then
head = NULL
We will create a new node with some key and value contained in it.
70