Data Structure - Ppt-Notes
Data Structure - Ppt-Notes
AND APPLICATIONS
Course Details
• Subject Code: BCS304
• Teaching Hours/Week: 3
• Credits: 03
• Marks:
– CIE (Continuous Internal Evaluation): 50 marks
• To illustrate the representation of different data structures such as Stacks, Queues, Linked Lists,
• To introduce advanced data structure concepts such as Hashing and Optimal Binary Search Trees.
A B C D NULL
Data Link
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
Advantages of Linked Lists:
1.Dynamic Size: The size of a linked list can grow or shrink dynamically,
making it more flexible than arrays.
2.Efficient Insertions/Deletions: Insertion and deletion operations are
more efficient as there is no need for shifting elements, unlike arrays.
Disadvantages:
1.Random Access Not Possible: Linked lists don't support direct access to
elements by index, making random access inefficient compared to arrays.
2.Extra Memory Overhead: Each node requires additional memory for
storing the pointer to the next node.
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
Types of Linked Lists
Linked lists can be classified based on their structure and how the
nodes are connected.
The three primary types of linked lists are:
1.Singly Linked List
2.Doubly Linked List
3.Circular Linked List
Head Node
A B C D NULL
Data Next
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
• Operations:
– Insertion: Can be performed at the beginning, middle, or end of the list.
– Deletion: Nodes can be deleted from the front, middle, or end.
– Traversal: Starts from the head and continues until NULL is encountered.
• Advantages:
– Efficient memory usage as it requires only one pointer per node.
– Easy to implement and suitable for simple applications.
• Disadvantages:
– Cannot be traversed backward, limiting access to the previous node.
– Finding the previous node requires traversal from the head.
• Use Case: Implementing stacks, queues, or simple dynamic lists.
Head Node
NULL A B C D NULL
Structure: Similar to a singly linked list, but the last node’s Next pointer
points back to the first node, forming a circle.
Head Node
A B C D
Data Next
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
• Operations:
– Insertion: Can be performed at the beginning, middle, or end. Since the list is circular, insertion at the end
requires updating the last node’s pointer to point to the new node, which in turn points back to the first
node.
– Deletion: Nodes can be deleted, and the circular structure is maintained by updating the pointers
appropriately.
– Traversal: Starts at any node and continues until the start node is encountered again, making it ideal for
scenarios where you need to loop through the list continuously.
• Advantages:
– The entire list can be traversed starting from any node.
– Useful for applications where circular traversal is needed (e.g., round-robin scheduling).
• Disadvantages:
– More complex to manage due to its circular nature, especially when inserting or deleting nodes.
– Traversal may lead to an infinite loop if not managed correctly.
• Use Case: Round-robin CPU scheduling, implementing circular buffers, and managing players in multiplayer
games.
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
b. Circular Doubly Linked List
Structure: Similar to a doubly linked list, but the Next pointer of the last node points to
the first node, and the Prev pointer of the first node points to the last node, forming a
circular structure.
Head Node
A B C D
data: This field stores the data of the node (in this case, an integer).
link: This pointer of type struct Node points to another node of the same type,
making the structure self-referential.
struct node
{
int data;
struct node *link;
};
typedef struct node * NODE;
if(first==NULL)
return temp;
temp->link = first;
return temp;
} Structure and Applications
Data Nagaraj Bhat, SMVITM, Udupi
Insert at end
NODE insert_at_end(NODE first) /*list not empty*/
{ cur = first;
int val;
while(cur->link!=NULL)
NODE temp, cur;
temp = getnode(); {
cur = cur->link;
printf("\nEnter the value: "); }
scanf("%d", &val);
temp->data = val;
cur->link = temp;
return first;
/*List is empty*/
}
if(first==NULL)
return temp;
NODE getnode()
{
NODE x;
x = (NODE)malloc(sizeof(struct stack));
x->link = NULL;
}
NODE getnode()
{
NODE x;
x = (NODE)malloc(sizeof(struct queue));
x->link = NULL;
}
typedef struct {
float coeff; // Coefficient of the term
int expon; // Exponent (degree) of the term
} PolynomialTerm;
struct node
{ NODE
int coeff; coeff
expon
int expon;
link
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x =(NODE) malloc(sizeof(struct node));
return x;
}
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
void main() printf("\nEnter the first polynomial \n");
{ head1 = read_poly(head1);
NODE head1,head2,head3; printf("\nEnter the second polynomial \n");
head1 = getnode(); head2 = read_poly(head2);
head2 = getnode();
head3 = poly_add(head1,head2,head3);
head3 = getnode();
printf("\nPolynomial 1:\t");
head1->link=head1; display(head1);
head2->link=head2; printf("\nPolynomial 2:\t");
head3->link=head3; display(head2);
printf("\nPolynomial Result:\t");
display(head3);
head1 head2 head3
coeff
}
expon
link &head1
Data Structure &head2
and Applications &head3 Nagaraj Bhat, SMVITM, Udupi
NODE read_poly(NODE head) NODE attach(int coeff, int expon, NODE head)
{ {
NODE temp, cur;
int i = 1, coeff, expon;
printf("\nEnter the coefficient as -999 to end the polynomial "); temp = getnode();
while(1) temp->coeff = coeff;
temp->expon = expon;
{
cur = head->link;
printf("\nEnter the %d term:\n", i++);
while(cur->link != head)
printf("\n\tCoeff = "); {
scanf("%d", &coeff); cur = cur->link;
if(coeff == -999) }
break; cur->link = temp;
Consider the polynomials:
temp->link = head;
printf("\n\tPow x = "); • a(x)=4x3+3x2+5x+6
return head;
scanf("%d", &expon); }
• b(x)=5x4+4x2+2x+1
} head1 term1 term2 term3 term4 head2 term1 term2 term3 term4 head3
coeff 4 3 5 6 5 4 2 1
return head; }
Data Structure and Applications expon 3 2 1 0 4Nagaraj2Bhat, SMVITM,
1 0
Udupi
link term1 term2 term3 term4 head1 term1 term2 term3 term4 head2 head3
NODE poly_add(NODE head1, NODE head2, NODE head3) a
{ head1 term1 term2 term3 term4
NODE a, b;
int coeff; coeff 4 3 5 6
a = head1->link; expon 3 2 1 0
b = head2->link; link term1 term2 term3 term4 head1
while(a != head1 && b != head2)
{ b
if(a->expon == b->expon) head2 term1 term2 term3 term4
{
coeff = a->coeff + b->coeff; coeff 5 4 2 1
if(coeff != 0) expon 4 2 1 0
head3 = attach(coeff, a->expon, head3); link term1 term2 term3 term4 head2
a = a->link;
b = b->link;
} head3 term1 term2 term3 term4 term5
else if(a->expon > b->expon) coeff 5 4 7 7 7
{ expon 4 3 2 1 0
head3 = attach(a->coeff, a->expon, head3); link term1 term2 term3 term4 terrm5 head3
a = a->link;
}
else Consider the polynomials:
{
head3 = attach(b->coeff, b->expon, head3); • a(x)=4x3+3x2+5x+6
b = b->link; • b(x)=5x4+4x2+2x+1
}
}//end Data Structure and Applications
while a(x)+b(x)=d(x)= 5x4 +4x 3 +7x2+7x+7
Nagaraj Bhat, SMVITM, Udupi
while(a != head1)
{
head3 = attach(a->coeff, a->expon, head3);
a = a->link;
}
while(b != head2)
{
head3 = attach(b->coeff, b->expon, head3);
b = b->link;
}
return head3;
}
Head Node
NULL A B C D NULL
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
• Each column of a spars matrix represented as a circular linked list with a header
node having three fields: next, down and right.
head next: not used
next
right: points to next element in the row
down right down: not used
• Each item represented as a node having five fields: row, col, val, down, right.
• First node in the below list diagram is identified as variable head which contains
size of the matrix (No. of Columns, No of Rows and No of Non Zero Values)
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
0 1 2 3 4 5
6 6 6 next next next next next next
down right down right down right down right down right down right down right
next 0 5 8
0 down right down right
next 1 1 1
1
down right down right
next 2 2 6
2
down right down right
next
3 down right
next 4 1 2 4 4 4
4
down right down right down right
next 5 2 3
5
down right down right
Node
A
Node
Node E Node F
Node
A Root
Node
Node E Node F
Edge
Edge Edge
E F
Parent
A
Children
Parent
A
Siblings
E F
Leaf E Leaf F
E F
Ancestors ? E F
Ancestors ?
Descendants
E F
E F
E F
E F
E F
• The right subtree of a node is the subtree that starts at the right child of that node and
includes all of its descendants.
E F
A
1
E F
A
1
2
E F
Level 1 A
Level 2
Level 3 E F F
• List Representation
• Left Child and Right Sibling Representation
• Left Child-Right Child Representation
B C
E F G H I J
1st Child 2nd Child 3rd Child
A NULL
1st child 2nd child 1st child 1st child 2nd child 3rd child
B C
E F G H I J
K L
A NULL
1st child 2nd child 1st child 1st child 2nd child 3rd child
B C
E F G H I
K L
A C NULL
1st child 2nd child 1st child 2nd child 3rd child
B F NULL D H I J NULL
A A
B C B C
E F G H I J E F G H I J
K L K L
B C B C
E F G H I J E F G G H I
K L M N O K L M N O
P Q
Data Structure and Applications P Q Nagaraj Bhat, SMVITM, Udupi
Left Child-Right Child Representation
(Binary Tree)
B C B C
E F G H I J E F G H I J
K L M K L M
45o
B C B C
E F G H I J E F G H I J
Left Child-Right
Data Structure Child Representation
and Applications Nagaraj Bhat, SMVITM, Udupi
(Binary Tree)
Binary Tree
A A A A
E F F F E F
Types of Binary Tree
4. Expression Tree: A binary tree used to 5. Binary Search Tree (BST): A binary tree in which each
represent arithmetic expressions. Internal node’s left child contains values less than the node, and
nodes represent operators, and leaf nodes the right child contains values greater than the node.
represent operands.
In an expression tree, nodes represent
operands (like 3, 4, 5) or operators (like +, *).
This tree represents the expression (3 + 4) * 5.
* 5
4 4 7 9
Properties of the Binary Tree
Property 1: Maximum Number of Nodes at Any Level
Statement: The maximum number of nodes at any level i of a binary
tree is 2i−1, where i represents the level.
Proof
• At level 1 (the root level), there is 1 node: 21−1=20=1.
• At level 2, each node at level 1 can have two children, so there
are 2 nodes: 22−1=21=2.
• At level 3, each node at level 2 can also have two children, so
there are 4 nodes: 23−1=22=4.
• This pattern continues such that at any level i, the maximum
number of nodes is 2i−1.
Proof
• To find the maximum number of nodes in a binary tree of
depth k, we add up the maximum number of nodes at i=K
each level from i=1 to i=k.
• From Property 1, we know that the maximum number of For a binary tree of depth k=3:
nodes at level i is 2i−1.
• Therefore, the total maximum number of nodes up to
So, the maximum number of nodes in a
depth k is: binary tree of depth 3 is 23−1=7.
n0 −1 = n2
n0Data
=n2Structure
+1 and Applications Nagaraj Bhat, SMVITM, Udupi
Binary Tree Representation
• Array Representation
• List Representation
1.Root Node:
4 D 5 E 6 F •The root node of the tree is stored at the first index of
the array, typically at index 1.
A B C D E F
0 1 2 3 4 5 6 2.Indexing Formula:
For a node at position i in the array:
Index Node Explanation •The left child of the node is stored at index 2 * i.
1 A Root node •The right child of the node is stored at index 2 * i + 1.
2 B Left child of A (2 * 1)
3 C Right child of A (2 * 1 + 1)
4 D Left child of B (2 * 2)
5 E Right child of B (2 * 2 + 1)
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
6 F Left child of C (2 * 3 )
1 A
Array Representation
In an array representation of a binary tree, the tree
nodes are stored in a single-dimensional array rather
2 B 3
than using linked structures.
1.Root Node:
4 D 5 E 7 F •The root node of the tree is stored at the first index
of the array, typically at index 1.
A B C D E F
2.Indexing Formula:
0 1 2 3 4 5 6 7 For a node at position i in the array:
•The left child of the node is stored at index 2 * i.
Index Node Explanation
•The right child of the node is stored at index 2 * i
1 A Root node
+ 1.
2 B Left child of A (2 * 1)
3 C Right child of A (2 * 1 + 1)
4 D Left child of B (2 * 2)
5 E Right child of B (2 * 2 + 1)
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
7 F Right child of C (2 * 3 + 1)
List Representation
A
In the list representation of a binary tree, each node of the
tree is represented by the list node. Each node contains
B three main parts:
1.Data: The value or data stored in the node.
2.Left Pointer: A pointer to the left child of the node.
E 3.Right Pointer: A pointer to the right child of the node.
D F G The leaf nodes links points to NULL
H I
A
E
Data Structure and Applications E E E E
Nagaraj Bhat, SMVITM, Udupi
Inorder C Function • Starting at the Root (A):
–
–
The function is initially called with root = A.
Check if A is NULL: A is not NULL, so the function proceeds.
– It first recursively calls inorder(A->left), moving to B.
#include <stdio.h> • At Node B:
– The function is now at B.
– Check if B is NULL: B is not NULL, so the function proceeds.
– It then calls inorder(B->left), moving to D.
struct Node { • At Node D:
char data; –
–
The function is now at D.
Check if D is NULL: D is not NULL, so the function proceeds.
struct Node *left; –
–
D has no left child, so inorder(D->left) is called and immediately returns since D->left is NULL.
Visit D: D is printed. Output so far: D
struct Node *right; – The function calls inorder(D->right), which immediately returns because D->right is NULL.
• Back to Node B:
}; – After finishing the left subtree of B, the function returns to B.
– Visit B: B is printed. Output so far: D B
struct Node *root = NULL; – The function then calls inorder(B->right), moving to E.
• At Node E:
void inorder( Node root) { – The function is now at E.
– Check if E is NULL: E is not NULL, so the function proceeds.
if (root == NULL) return; – E has no left child, so inorder(E->left) returns immediately since E->left is NULL.
–
inorder(root->left); –
Visit E: E is printed. Output so far: D B E
The function calls inorder(E->right), which returns immediately because E->right is NULL.
printf("%c ", root->data); • Back to Node A:
– After finishing the left subtree of A, the function returns to A.
inorder(root->right); –
–
Visit A: A is printed. Output so far: D B E A
The function calls inorder(A->right), moving to C.
} • At Node C:
– The function is now at C.
– Check if C is NULL: C is not NULL, so the function proceeds.
– C has no left child, so inorder(C->left) returns immediately since C->left is NULL.
– Visit C: C is printed. Output so far: D B E A C
– The function calls inorder(C->right), which returns immediately because C->right is NULL.
– The final output of the inorder traversal for the given tree is: D B E A C
E
Data Structure and Applications E E E E
Nagaraj Bhat, SMVITM, Udupi
• Starting at the Root (A):
Preorder C Function –
–
The function is initially called with root = A.
Check if A is NULL: A is not NULL, so the function proceeds.
– Visit A: A is printed. Output so far: A
#include <stdio.h> – The function calls preorder(A->left), moving to B.
• At Node B:
– The function is now at B.
– Check if B is NULL: B is not NULL, so the function proceeds.
struct Node { – Visit B: B is printed. Output so far: A B
– The function then calls preorder(B->left), moving to D.
char data; • At Node D:
struct Node *left; –
–
The function is now at D.
Check if D is NULL: D is not NULL, so the function proceeds.
struct Node *right; –
–
Visit D: D is printed. Output so far: A B D
The function calls preorder(D->left), which returns immediately because D->left is NULL.
}; – The function calls preorder(D->right), which returns immediately because D->right is NULL.
• Back to Node B:
struct Node *root = NULL; – After finishing D, the function returns to B.
– The function calls preorder(B->right), moving to E.
void preorder(Node root) { • At Node E:
if (root == NULL) return; –
–
The function is now at E.
Check if E is NULL: E is not NULL, so the function proceeds.
– The final output of the inorder preorder for the given tree is: A BDEC
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
3. Postorder Traversal
• Definition: In Postorder traversal, the A
nodes are visited in the following order:
– Traverse the left subtree.
– Traverse the right subtree.
– Visit the root node.
• Pattern: Left -> Right -> Root E
DE B C A
A A A A A
E
Data Structure and Applications E E E E
Nagaraj Bhat, SMVITM, Udupi
• Starting at the Root (A):
Postorder C Function –
–
The function is initially called with root = A.
Check if A is NULL: A is not NULL, so the function proceeds.
– The function calls postorder(A->left), moving to B.
#include <stdio.h> • At Node B:
– The function is now at B.
– Check if B is NULL: B is not NULL, so the function proceeds.
–
struct Node { •
The function then calls postorder(B->left), moving to D.
At Node D:
char data; – The function is now at D.
– Check if D is NULL: D is not NULL, so the function proceeds.
struct Node *left; – D has no left child, so postorder(D->left) returns immediately because D->left is NULL.
– D has no right child, so postorder(D->right) returns immediately because D->right is NULL.
struct Node *right; – Visit D: D is printed. Output so far: D
}; • Back to Node B:
– After finishing D, the function returns to B.
struct Node *root = NULL; – The function calls postorder(B->right), moving to E.
• At Node E:
void postorder(Node root) { – The function is now at E.
if (root == NULL) return; – Check if E is NULL: E is not NULL, so the function proceeds.
– E has no left child, so postorder(E->left) returns immediately because E->left is NULL.
postorder(root->left); – E has no right child, so postorder(E->right) returns immediately because E->right is NULL.
– Visit E: E is printed. Output so far: D E
postorder(root->right); • Back to Node B:
printf("%c ", root->data); – After finishing the left and right subtrees of B, the function returns to B.
– Visit B: B is printed. Output so far: D E B
} • Back to Node A:
– After finishing the left subtree of A, the function returns to A.
– The function calls postorder(A->right), moving to C.
• At Node C:
– The function is now at C.
– Check if C is NULL: C is not NULL, so the function proceeds.
– C has no left child, so postorder(C->left) returns immediately because C->left is NULL.
– C has no right child, so postorder(C->right) returns immediately because C->right is NULL.
– Visit C: C is printed. **Output so far: D E B C
Data Structure and Applications • Back to Node A: Nagaraj Bhat, SMVITM, Udupi
– After finishing the left and right subtrees of A, the function returns to A.
Tree Traversals: An Example
B C
D E F
G
Preorder: (Root->Left->Right)
Postorder: (Left->Right->Root)
In Order: (Left->Root->Right)
Data Structure and Applications Nagaraj Bhat, SMVITM, Udupi
Tree Traversals: An Example
B C
D E F
Preorder: A
(visit each node as your reach it)
G
B C
D E F
Preorder: AB
(visit each node as your reach it)
G
B C
D E F
Preorder: ABD
(visit each node as your reach it)
G
B C
D E F
Preorder: ABDE
(visit each node as your reach it)
G
B C
D E F
Preorder: ABDEC
(visit each node as your reach it)
G
B C
D E F
Preorder: ABDECF
(visit each node as your reach it)
G
B C
D E F
Preorder: ABDECFG
(visit each node as your reach it)
G
B C
D E F
Postorder:
G
B C
D E F
Postorder:
G
B C
D E F
Postorder: D
G
B C
D E F
Postorder: DE
G
B C
D E F
Postorder: DEB
G
B C
D E F
Postorder: DEB
G
B C
D E F
Postorder: DEB
G
B C
D E F
Postorder: DEBG
G
B C
D E F
Postorder: DEBGF
G
B C
D E F
Postorder: DEBGFC
G
B C
D E F
Postorder: DEBGFCA
G
B C
D E F
G
In Order:
B C
D E F
G
In Order:
B C
D E F
G
In Order: D
B C
D E F
G
In Order: DB
B C
D E F
G
In Order: DBE
B C
D E F
G
In Order: DBEA
B C
D E F
G
In Order: DBEA
B C
D E F
G
In Order: DBEAF
B C
D E F
G
In Order: DBEAFG
B C
D E F
G
In Order: DBEAFGC
B C
D E F
Preorder: ABDECFG
Postorder: DEBGFCA
G
In Order: DBEAFGC
B C
D E
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
In Order: DHFIBEACG
H I
B C
D E
Preorder: A
H I
B C
D E
Preorder: AB
H I
B C
D E
Preorder: ABD
H I
B C
D E
Preorder: ABDF
H I
B C
D E
Preorder: ABDFH
H I
B C
D E
Preorder: ABDFHI
H I
B C
D E
Preorder: ABDFHIE
H I
B C
D E
Preorder: ABDFHIEC
H I
B C
D E
Preorder: ABDFHIECG
H I
B C
D E
Postorder:
H I
B C
D E
Postorder:
H I
B C
D E
Postorder:
H I
B C
D E
Postorder:
H I
B C
D E
Postorder: H
H I
B C
D E
Postorder: HI
H I
B C
D E
Postorder: HIF
H I
B C
D E
Postorder: HIFD
H I
B C
D E
Postorder: HIFDE
H I
B C
D E
Postorder: HIFDEB
H I
B C
D E
Postorder: HIFDEB
H I
B C
D E
Postorder: HIFDEBG
H I
B C
D E
Postorder: HIFDEBGC
H I
B C
D E
Postorder: HIFDEBGCA
H I
B C
D E
In Order:
H I
B C
D E
In Order:
H I
B C
D E
In Order: D
H I
B C
D E
In Order: D
H I
B C
D E
In Order: DH
H I
B C
D E
In Order: DHF
H I
B C
D E
In Order: DHFI
H I
B C
D E
In Order: DHFIB
H I
B C
D E
In Order: DHFIBE
H I
B C
D E
In Order: DHFIBEA
H I
B C
D E
In Order: DHFIBEAC
H I
B C
D E
In Order: DHFIBEACG
H I
B C
D E
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
In Order: DHFIBEACG
H I