DSA Module 3
DSA Module 3
1
Module 3
• LINKED LISTS :
• Additional List Operations, Sparse Matrices, Doubly Linked List.
• TREES:
• Introduction, Binary Trees, Binary Tree Traversals, Threaded Binary Trees.
2
LINKED LISTS (contd.)
3
Concatenating Singly Linked (Concatenating two chains)
struct list{ NODE *concatenate(NODE *l1,NODE *l2){
int data; NODE *t;
struct list*link; if(!l1)
}; return(l1);
typedef struct list NODE; else if(!l2)
return(l2);
else
for(t=l1;t->link;t=t->link);
t->link=l2;
return(l1);
}
4
Inverting Singly Linked (Inverting two chains)
NODE *Invert(NODE *l){
struct list{
NODE *result=NULL, *t1=result;
int data;
while(l){
struct list*link;
t=l;
};
l=l->next;
typedef struct list NODE;
t->link=t1;
t1=t;
}
return(result);
}
5
Insertion of a node at front of circular linked
list NODE *Front_circular(NODE *list,NODE *temp){
if(!list)
struct list{ {
int data; list=temp;
temp->link=list;
struct list*link; }
}; else{
temp->link=list->link;
typedef struct list NODE; list->link=node;
}
return(list);
}
6
Finding length of circular linked list
struct list{ int count (NODE *list){
int data; NODE *temp=list;
int count=0;
struct list*link;
if (list)
};
temp=list;
typedef struct list NODE; do{
count++;
temp=temp->link;
}while(temp!=list);
return(count);
}
7
Sparse Matrix
8
Sparse matrix : Linked Representation
• Linked list data structure is used to represent a sparse matrix.
• In this linked list, two different nodes namely header node and element
node are used.
• Header node consists of three fields and element node consists of five
fields.
• This sparse matrix can be represented using linked representation as shown
in the below image.
9
C structure representation
struct sparseMatrix{
union{
struct NODEPTR * Next;
struct entry{
int row,col,value;
}data;
} u;
struct sparseMatrix *down,*right;
};
typedef struct sparseMatrix NODE;
NODE * Head;
11/28/2024 10
0 0 0 0 9 0
0 8 0 0 0 0
4 0 2 0 0 0
0 0 0 0 0 5
0 0 2 0 0 0
5 6 6
0 4 9
1 1 8
2 0 4
2 2 2
3 5 5
4 2 2
11
Doubly Linked List
12
Two way list-Doubly linked list
• Using singly list we can only traverse in one direction. i.e we
can only access next node but not previous node.
• In two way list ,the list can be made accessible in both
direction..
• Each node N is divided into three parts
• An information field INFO which contains the data of N
• A pointer field FORW or RIGHT which contains address of next node
• A pointer field BACK or LEFT which contains address of previous node
11/28/2024 13
14
DLL: Data structure requirements
Let LOCA and LOCB are two pointers of nodes A and B such that
LOCARight=LOCB
LOCBLeft=LOCA
LOCA LOCB
struct Data{ NODE* Getnode(){
int item; NODE *node=(NODE*)calloc(1,sizeof(NODE));
printf("\nEnter item:");
struct Data *Llink,*Rlink; scanf("%d",&node->item);
}; return (node);
}
typedef struct Data NODE;
11/28/2024 16
Rear and Front Deletions
NODE* Delete_Rear(NODE *head){ NODE* Delete_Front(NODE *head){
NODE *p=head,*q; NODE *p=head,*q;
if(head= =NULL)
if(head= =NULL)
printf("\nList is Empty (QUEUE)");
printf("\nList is Empty (QUEUE)");
else {
else {
while(p->right!=NULL)
p=p->right;
if( p->right = =NULL)
if( p->left !=NULL){ head=NULL;
q=p->left; else{
q->right=NULL; head=head->right;
} head->left=NULL;
else }
head=NULL; }
free(p);
return head;
}
}
return head;
}
11/28/2024 17
Trees
18
• TREES: Introduction, Binary Trees, Binary Tree Traversals, Threaded
Binary Trees
19
Terminology
• Tree is a non – linear data structure which organizes data in a
hierarchical structure.
• In tree data structure, every individual element is called as a Node.
• Node in a tree, stores the actual data of that particular element and
link to next element in hierarchical structure.
• Binary tree – A binary tree is a tree which is a collection of zero or
more nodes and finite set of directed lines called branches that
connect the nodes.
21
Tree terminologies
Terminology
• A tree T can be empty or T contains a distinguished node
R called root of T and remaining nodes of T form an
ordered pair of disjoint binary trees T1 and T2.
• Suppose T is non empty and contains two sub trees
namely T1 and T2. If T1 and T2 are non empty , then T1 is
called left successor of R and T2 is called Right successor
of R.
• Root – If tree is not empty, the first node is called root
node.
• Left subtree – It is a tree which is connected to the left of
root.
• Right subtree - It is a tree which is connected to the right
of root.
Terminology
• Complete binary tree - A strictly binary or complete binary tree in which
the number of nodes at any level i is 2i.
• Example shows a complete binary tree because number nodes at level 1
is 2 and at level 2 is 4.
Terminology
• Almost complete binary tree - A tree of depth d is said
to be an almost complete binary tree if it satisfies
following two properties :
1. If the tree is complete up to the level d-1, then the total
number of nodes at the level d-1 should be 2*(d-1)
2. The total number of nodes at level d may be equal to 2d. If
the total number of nodes at level d is less than 2d, then the
number of nodes at level d-1 should be 2(d-1) and in level d
the nodes should be present only from left to right.
Example: The tree is almost complete, because at level 1,
number of nodes are 2. At level 2, number of nodes is less than
4 but nodes are filled from left to right.
Terminology
• Binary search tree –
• The left subtree of a node contains only nodes with keys lesser
than the node’s key.
• The right subtree of a node contains only nodes with keys greater
than the node’s key.
• The left and right subtree each must also be a binary search tree.
• A binary search tree is a binary tree in which for
each node say x in the tree, elements in the left sub-
tree are less than INFO(x) and elements in the right
sub-tree are greater than or equal to INFO(x). Every
node in the tree should satisfy this condition, if left
sub-tree or right sub-tree exists.
• Example: The given tree is Binary search tree
because at root the info field is 7. It left sub tree has
info field lesser and right subtree has higher value i.e
2 <7 and 9>7
Terminology
37
Example for binary tree
38
• a Skewed binary tree
• b complete binary tree
39
40
Maximum number of nodes on level 3=23-1 =4
Maximum number of nodes on level of depth 3= 3=23-1 =7
41
n0=4+1
Degree 2 nodes: A,B,C,D
Leaf Nodes: H I E F G
42
Proof
• Let n be the number of nodes i.e sum of nodes with degree 0,1,2
• n=no+n1+n2 (eqn 1)
• Also number node n is also equal to number of branches B+1
• n=B+1 Where B=n1+2n2
• n=1+n1+2n2 (eqn 2)
• Subtract 2-1
• 0=1-n0+n2
• n0=1+n2
43
Terminology
• Complete binary tree - A strictly binary or complete binary tree in which
the number of nodes at any level i is 2i.
• Example shows a complete binary tree because number nodes at level 1
is 2 and at level 2 is 4.
45
Binary tree representation
• Different methods to represent a binary tree in memory.
• i) Array representation
• Ii) Linked list representation
• iii) Left child Right sibling representation
1) Array representation
• 1-D array is used to represent a tree. Array is filled from
level 0 to last level
• This representation turns out to be inefficient for a binary search
tree representation
Array representation of Binary tree
48
2) Linked list Representation.
• This representation uses double linked list
structure with three nodes namely INFO,RIGHT
and LEFT. This minimizes the wastage of memory.
• C structure representation
struct tree{
int data;
struct tree *left , * right;
};
typedef struct tree NODE;
50
Left child right sibling representation
51
Binary Tree Traversals
52
Binary tree traversals
• There are 6 possible ways of traversals
• Traversals is the process of visiting all nodes exactly once.
• LRV
• LVR
• VLR
• VRL
• RLV
• RVL
• Here only three traversals are discussed :
• Root Left Right
• Left Root Right
• Left Right Root
53
Expression tree
54
Binary Tree Traversals
1 + Print +
57
In-Order Traversing
A/B*C*D+E
59
Post-Order Traversing
i) Traverse the left subtree of R
ii) Traverse the right subtree of R
iii) Process root
• In this traversing mechanism, first left node of root is processed then then right of root
is visited and then root node is visited
void postorder(node *temp) {
if(temp!=NULL) {
postorder(temp->left);
postorder (temp->right);
printf(“%d”,temp->data);
}
}
• Example: In postorder tree traversal the result is : D, E, B, F, G, C, A
Call of post order Value of root Action Call of post order Value of root Action
1 + 23 * Print *
63
In Order traversal :DGBAHEICF
Post order traversal: GDBHIEFCA
Action Parent Left of parent Right of parent A
(postorder) (postorder)
Find root/parent in A GDB HIEFC
postorder B HIEFC
Find parent in GDB B GD --
GD
64
In Order traversal :DGBAHEICF
Post order traversal: GDBHIEFCA
Action Parent Left of parent Right of parent
A
(postorder) (postorder)
Find root/parent in A GDB HIEFC
postorder
B HIEFC
Find parent in GDB B GD --
Find parent in DG D -- D
65
In Order traversal :DGBAHEICF
Post order traversal: GDBHIEFCA
Action Parent Left of parent Right of parent
A
Find root/parent in A GDB HIEFC
postorder
Find parent in GDB B GD -- B HIEFC
Find parent in GD D -- D
66
In Order traversal :DGBAHEICF
Post order traversal: GDBHIEFCA
Action Parent Left of parent Right of parent
A
Find root/parent in A GDB HIEFC
postorder
Find parent in GDB B GD -- B C
Find parent in GD D -- D
Find parent in HIEFC C HIE F
HIE
D F
67
In Order traversal :DGBAHEICF
Post order traversal: GDBHIEFCA
Action Parent Left of parent Right of parent A
Find root/parent in A GDB HIEFC
postorder
Find parent in GDB B GD -- B C
Find parent in GD D -- G
Find parent in HIEFC C HIE F E
Find parent in HIE E H I D F
H I
G
68
In Order traversal :DGBAHEICF
Post order traversal: GDBHIEFCA
A
B C
INORDER: DGBAHEICF
PREORDER: ABDGCEHIF D
E
F
POST ORDER: GDBHIEFCA
H I
G
69
INORDER (L V R A
B C
1
E
D F
H I
G
70
71
72
Construct expression tree:
(a+[b-c])*([d-e]/(f+g)-h)
73
74
75
Postorder
-9 13 34 18 12
76
Pre order
Call to pre order Print Action
Node 12 Call Preorder(12)
Node -9 Call Preorder(-9)
NULL Call Preorder(Left of-9)
NULL Call Preorder(Right of-9)
Print -9 -9
Node 18 Call Preorder(18)
Node 13 Call Preorder(13)
NULL Call Preorder(Left 13)
NULL Call Preorder(right 13)
Print 13 13
Node 34 Call Preorder(34)
NULL Call Preorder(Left of 34)
-9 13 34 18 12
NULL Call Preorder(Right of 34)
Print 34 34
Print 18 18
Print 12 12 77
Inorder
Call to pre order Print Action
12 Call Inorder(12)
-9 Call Inorder(-9)
NULL Call Inorder(Left of -9)
Print -9 -9
NULL Call Inorder(right of -9)
Print 12 12
18 Call Inorder(18)
13 Call Inorder(13)
NULL Call Inorder(left of 13)
Print 13 13
NULL Call Inorder(Right of 13)
Print 18 18
34 Call Inorder(34) -9 12 13 18 34
NULL Call Inorder(left of 34)
Print 34 34
78
NULL Call Inorder(Right of 34)
Threaded binary trees
• The idea of threaded binary trees is to make in order traversal faster
and do it without stack and without recursion. A binary tree is made
threaded by making all child pointers that would normally be NULL
point to the in-order successor/predessor of the node (if it exists).
• The number of null links can be reduced using threads.
INORDER: HDIBEAFCG
Traversal in Threaded Binary Tree
• If ptr leftchild is NULL, replace ptrleftchild with a pointer to the node that
would be visited before ptr in an inorder traversal.
• (In-order- pre-deccssor)
• If ptrrightchild is null, replace ptrrightchild with a pointer to the node that
would be visited after ptr in an in-order traversal
• (In-order- successor)
• In-order traversal for the given binary tree: H D I B E A F C G
• Threaded binary tree is shown by connecting NULL right-child to In-order
successor and left NULL child to inorder predecessor
In this threaded binary tree, there are two dangling pointer, this can
be corrected by connecting to a special node called Header node.
Modified Structure definition for threaded binary tree
leftThread and rightThread is set to
struct threadedTree{ 0 =0 if there is a thread, 1- for child node link
short int leftThread;
threadedTree * leftChild;
char data;
threadedTree * rightChild;
short int rightThread;
};
typedef struct threadedTree NODE;
83