0% found this document useful (0 votes)
18 views50 pages

DS Notes For 2IA

Uploaded by

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

DS Notes For 2IA

Uploaded by

thranjith66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structures and Applications [BCS304]

LINKED LIST

Linked List is a linear data structure, in which elements are not stored at a contiguous location,
rather they are linked using pointers. Linked List forms a series of connected nodes, where each
node stores the data and the address of the next node.

Features of Linked List

 A linked list is a linear data structure that stores a collection of data elements dynamically.

 Nodes represent those data elements, and links or pointers connect each node.

 Each node consists of two fields, the information stored in a linked list and a pointer that
stores the address of its next node.

 The last node contains null in its second field because it will point to no node.

 A linked list can grow and shrink its size, as per the requirement.
 It does not waste memory space.

Representation of a Linked List

Structure Representtaion of a node:

A node in a linked list typically consists of two components:


Data: It holds the actual value or data associated with the node.
Next : It is a Pointer to store the memory address (reference) of the next node in the sequence.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

struct node
{
int data:
struct node*next;
};
node

data *next

Self Referential Structures

Self Referential structures are those structures that have one or more pointers which point to
the same type of structure, as their member.

Example structure node in a linked list


A node in a linked list typically consists of two components:
Data: It holds the actual value or data associated with the node.
Next : It is a Pointer to store the memory address (reference) of the next node in the sequence.

struct node
{
int data:
struct node*next;
};
node

data *next

A Menu Driven Program for a Singly Linked List of integers


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node*next;
};

struct node * first=NULL;


struct node *temp, *q, *p;

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

void insertfront()
{
temp=(struct node *) malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next =NULL;

if(first==NULL)
first=temp;
else
{
temp->next=first;
first=temp;
}
}

void insertend()
{
temp=(struct node *) malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next =NULL;

if(first==NULL)
first=temp;
else
{
q=first;
while(q->next!=NULL)
q=q->next;

q->next=temp;
}
}

void deletefront()
{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
first=first->next;
printf("\n Element Deleted is%d",q->data);
free(q);
}
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

void deleteend()
{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
while(q->next->next!=NULL)
{
p=q;
q=q->next;
}

p->next=NULL;
printf("\n Element Deleted is %d",q->data);
free(q);
}
}

void display()
{

if(first==NULL)
printf("\n Listisempty.\n");

else
for(q=first; q!=NULL;q= q->next)
printf("%d ->", q->data);
}

void main()
{
int choice;
while(1)
{
printf("-----SINGLYLINKEDLIST MENU \n");
printf("1:INSERT AT TFRONT\n");
printf("2:INSERT AT ENDDISPLAY\n");
printf("3:DELETE FROM FRONT\n");
printf("4:DELETE FROM END\n");
printf("5.DISPLAY\n");
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

printf("6:EXIT\n");

printf("Enter your choice\n");


scanf("%d",&choice);

switch(choice)
{
case 1: insertfront();
break;
case 2: insertend();
break;
case 3:deletefront();
break;
case 4:deleteend(); ;
break;
case 5:display();
break;

case 6: exit(0);

default:printf("InvalidChoice\n");
}// switch end
}//while end
}

Function to create a list of inetegers 10,20, 30

First

10 20 30 NULL

void create()
{
for(i=10; i<=30;i=i+10) //create nodes with 10,20,30
{
temp=(struct node *) malloc(sizeof(struct node));
temp->data =i;
temp->next =NULL;

if(first==NULL)
first=temp;

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

else
{
temp->next=first;
first=temp;
}
}
}

Note: LINKED STACK

Perform insertfront() as PUSH and deletefront() as POP ()


It works as a stack using with linked list

IMPLEMENT A STACK USING LINKED LIST

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node*next;
};

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

struct node * first=NULL;


struct node *temp, *q, *p;

void insertfront() // same as push()


{
temp=(struct node *) malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next =NULL;

if(first==NULL)
first=temp;
else
{
temp->next=first;
first=temp;
}
}

void deletefront()// same as pop()


{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
first=first->next;
printf("\n DeletedStudentUSNis%d",q->data);
free(q);
}
}

void display()
{

if(first==NULL)
printf("\n Listisempty.\n");

else
for(q=first; q!=NULL;q= q->next)
printf("%d ->", q->data);
}

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

void main()
{
int choice;

while(1)
{
printf("-----LINKED STACK \n");
printf("1:PUSH\n");
printf("2:POP\n");
printf("3.DISPLAY\n");
printf("4:EXIT\n");

printf("Enter your choice\n");


scanf("%d",&choice);

switch(choice)
{
case 1: insertfront();
break;
case 2: deletefront();
break;
case 3:display();
break;
case 4:exit(0);

default:printf("InvalidChoice\n");
}// switch end
}//while end
}

C function to conacatenate two SLL;

void concatenate()
{
struct node *first 1;
struct node *first 2;
if(first1!=NULL) && (first2!=NULL)
first1->next=first2;

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

C function to find length of SLL or count of nodes inSLL:


void count()
{
int count=0;
if(first==NULL)
printf("SLL List is empty. \n");
else
{
for(q=start;q!=NULL;q=q->next)
{
printf("%d<==>\n",q->data);
count++;
}
}
printf("\nLength of the SLL : %d\n",count);
}

Doubly Linked List DLL

struct node
{
int data:
struct node*next;
struct node * prev;
};
node
data
*prev *next

#include<stdio.h>
#include<stdlib.h>

struct node
{
Int data;
struct node *next;
struct node *prev;
};

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

struct node *first=NULL;


struct node *temp,*q,*p;

void nodecreate()
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next=NULL;
temp->prev=NULL;
}

void insertfront()
{

nodecreate();
if(first==NULL)
first=temp;
else
{
temp->next=first;
first->prev=temp;
first=temp;
}
}

void insertend()
{

nodecreate();
if(first==NULL)
first=temp;
else
{
q=first;
while(q->next!=NULL)
q=q->next;
temp->prev=q;
q->next=temp;
}
}

void deletefront()
{
if(first==NULL)
printf("The DLL list is empty. \n");
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

else
{
q=first;
printf("Deleted element is %d \n\n",q->data);
first=first->next;
first->prev=NULL;
free(q);
}
}

void deleteend()
{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
while(q->next!=NULL)
{
p=q;
q=q->next;
}
p->next=NULL;
printf("\n Student deleted is %d",q->data);
free(q);
}
}

void display()
{
int count=0;
if(first==NULL)
printf(" DLL List is empty. \n");
else
{
for(q=first;q!=NULL;q=q->next)
{
printf("%d \t <=======>\n",q->data);
count++;
}
}
printf("\nTotal Number of nodes DLL List are: %d\n",count);
}

void main( )
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

{
int choice,n,i;
while(1)
{
printf("\n-----DOUBLY LINKED LIST MENU \n");
printf("1:CREATE DLL\n");
printf("2:DISPLAY DLL\n");
printf("DOUBLE ENDED QUEUE DEMOSTRATION \n");
printf("3:INSERT AT END OF DLL\n");
printf("4:DELETE FROM END OF DLL\n");
printf("5:INSERT AT FRONT OF DLL\n");
printf("6:DELETE FROM FRONT OF DLL\n");
printf("7:EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter number of Employees:\n ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Employee %d details\n",i);
insertend();
}
break;
case 2: display();
break;
case 3: insertend();
break;
case 4: deleteend();
break;
case 5: insertfront();
break;
case 6: deletefront();
break;
case 7: exit(0);
default:printf("Invalid Choice\n");
}
}
}

C function to conacatenate two DLL;

void conacatenate()
{
struct node *first 1;
struct node *first 2;

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

if(first1!=NULL) && (first2!=NULL)


{
first1->next=first2;
first2->prev=first1;
}
}

C function to find length of SLL or count of nodes in DLL:


void count()
{
int count=0;
if(first==NULL)
printf("DLL List is empty. \n");
else
{
for(q=start;q!=NULL;q=q->next)
{
printf("%d<==>\n",q->data);
count++;
}
}
printf("\nLength of theDLL : %d\n",count);
}

Linked Representation of Sparse Matrix

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Linked Representation of Polynomial using SLL

struct node
{
int coef;
int exp;
struct node * next;
};

Linked Representation of Polynomial using Circular SLL with header node

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

TREE DATA STRUCTURE

This data structure is a specialized method to organize and store data in the computer to be
used more effectively. It consists of a central node, structural nodes, and sub-nodes, which are
connected via edges. We can also say that tree data structure has roots, branches, and leaves
connected with one another.

Basic Terminologies In Tree Data Structure:

Root-

 The first node from where the tree originates is called as a root node.
 In any tree, there must be only one root node.
 We can never have multiple root nodes in a tree data structure.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Example-

2,Edge
The connecting link between any two nodes is called as an edge.
 In a tree with n number of nodes, there are exactly (n-1) number of edges.

Example-

3. Parent-

 The node which has a branch from it to any other node is called as a parent node.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

 In other words, the node which has one or more children is called as a parent node.
 In a tree, a parent node can have any number of child nodes.

Example-

Here,
 Node A is the parent of nodes B and C
 Node B is the parent of nodes D, E and F
 Node C is the parent of nodes G and H
 Node E is the parent of nodes I and J
 Node G is the parent of node K

4. Child-

 The node which is a descendant of some node is called as a child node.


 All the nodes except root node are child nodes.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here,
 Nodes B and C are the children of node A
 Nodes D, E and F are the children of node B
 Nodes G and H are the children of node C
 Nodes I and J are the children of node E
 Node K is the child of node G

5. Siblings-

 Nodes which belong to the same parent are called as siblings.


 In other words, nodes with the same parent are sibling nodes.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

1.
2.

Why is My Personal Macro Workbook Not Loading? Top 3 Reasons.


Here,
 Nodes B and C are siblings
 Nodes D, E and F are siblings
 Nodes G and H are siblings
 Nodes I and J are siblings

6. Degree-

 Degree of a node is the total number of children of that node.


 Degree of a tree is the highest degree of a node among all the nodes in the tree.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here,
 Degree of node A = 2
 Degree of node B = 3
 Degree of node C = 2
 Degree of node D = 0
 Degree of node E = 2
 Degree of node F = 0
 Degree of node G = 1
 Degree of node H = 0
 Degree of node I = 0
 Degree of node J = 0
 Degree of node K = 0

7. Internal Node-

 The node which has at least one child is called as an internal node.
 Internal nodes are also called as non-terminal nodes.
 Every non-leaf node is an internal node.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here, nodes A, B, C, E and G are internal nodes.

8. Leaf Node-

 The node which does not have any child is called as a leaf node.
 Leaf nodes are also called as external nodes or terminal nodes.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here, nodes D, I, J, F, K and H are leaf nodes.

9. Level-

 In a tree, each step from top to bottom is called as level of a tree.


 The level count firsts with 0 and increments by 1 at each level or step.

Example-

10. Height-

 Total number of edges that lies on the longest path from any leaf node to a particular node is
called as height of that node.
 Height of a tree is the height of root node.
 Height of all leaf nodes = 0

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here,
 Height of node A = 3
 Height of node B = 2
 Height of node C = 2
 Height of node D = 0
 Height of node E = 1
 Height of node F = 0
 Height of node G = 1
 Height of node H = 0
 Height of node I = 0
 Height of node J = 0
 Height of node K = 0

11. Depth-

 Total number of edges from root node to a particular node is called as depth of that node.
 Depth of a tree is the total number of edges from root node to a leaf node in the longest
path.
 Depth of the root node = 0
 The terms “level” and “depth” are used interchangeably.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here,
 Depth of node A = 0
 Depth of node B = 1
 Depth of node C = 1
 Depth of node D = 2
 Depth of node E = 2
 Depth of node F = 2
 Depth of node G = 2
 Depth of node H = 2
 Depth of node I = 3
 Depth of node J = 3
 Depth of node K = 3

12. Subtree-

 In a tree, each child from a node forms a subtree recursively.


 Every child node forms a subtree on its parent node.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

13. Forest-

A forest is a set of disjoint trees.

Example-

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Why Tree is considered a non-linear data structure?


The data in a tree are not stored in a sequential manner i.e., they are not stored linearly.
Instead, they are arranged on multiple levels or we can say it is a hierarchical structure. For
this reason, the tree is considered to be a non-linear data structure .

Properties of Tree Data Structure:


 Number of edges: An edge can be defined as the connection between two nodes. If a tree
has N nodes then it will have (N-1) edges. There is only one path from each node to any
other node of the tree.
 Depth of a node: The depth of a node is defined as the length of the path from the root to
that node. Each edge adds 1 unit of length to the path. So, it can also be defined as the
number of edges in the path from the root of the tree to the node.
 Height of a node: The height of a node can be defined as the length of the longest path
from the node to a leaf node of the tree.
 Height of the Tree: The height of a tree is the length of the longest path from the root of
the tree to a leaf node of the tree.
 Degree of a Node: The total count of subtrees attached to that node is called the degree of
the node. The degree of a leaf node must be 0. The degree of a tree is the maximum degree
of a node among all the nodes in the tree.

Application of Tree Data Structure:

 File System: This allows for efficient navigation and organization of files.
 Data Compression: Huffman coding is a popular technique for data compression that
involves constructing a binary tree where the leaves represent characters and their
frequency of occurrence. The resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
 Compiler Design: In compiler design, a syntax tree is used to represent the structure of a
program.
 are used in database indexing to efficiently search for and retrieve data.

BINARY TREE DATA STRUCTURE

A Binary Tree Data Structure is a hierarchical data structure in which each node has at most two
children, referred to as the left child and the right child. It is commonly used in computer science
for efficient storage and retrieval of data, with various operations such as insertion, deletion and
traversal.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Types of Binary Tree based on the number of children:

1.Full Binary Tree


A Binary Tree is a full binary tree if every node has 0 or 2 children. The following are
examples of a full binary tree. We can also say a full binary tree is a binary tree in which all
nodes except leaf nodes have two children.
A full Binary tree is a special type of binary tree in which every parent node/internal node has
either two or no children. It is also known as a proper binary tree.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

2.Skewed Binary Tree


A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated
by the left nodes or the right nodes. Thus, there are two types of skewed binary tree: left-
skewed binary tree and right-skewed binary tree.

Types of Binary Tree On the basis of the completion of levels:


1. Complete Binary Tree
2. Perfect Binary Tree

Complete Binary Tree


A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly
the last level and the last level has all keys as left as possible.
A complete binary tree is just like a full binary tree, but with two major differences:
 Every level except the last level must be completely filled.
 All the leaf elements must lean towards the left.
 The last leaf element might not have a right sibling i.e. a complete binary tree doesn’t have
to be a full binary tree.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

2. Perfect Binary Tree


A Binary tree is a Perfect Binary Tree in which all the internal nodes have two children and all
leaf nodes are at the same level.

The following are examples of Perfect Binary Trees.


A perfect binary tree is a type of binary tree in which every internal node has exactly two
child nodes and all the leaf nodes are at the same level.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

A Binary Search Tree is a data structure used in computer science for organizing and storing
data in a sorted manner. Each node in a Binary Search Tree has at most two children,
a left child and a right child, with the left child containing values less than the parent node
and the right child containing values greater than the parent node. This hierarchical structure
allows for efficient searching, insertion, and deletion operations on the data stored in the
tree.

Tree Traversal
Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and
postorder traversal of the Binary Search

Inorder Traversal:
Follow the below steps to implement the idea:
 Traverse left subtree
 Visit the root and print the data.
 Traverse the right subtree

Preorder Traversal:

Follow the below steps to implement the idea:


 Visit the root and print the data.
 Traverse left subtree
 Traverse the right subtre

Postorder Traversal:
Follow the below steps to implement the idea:
 Traverse left subtree
 Traverse the right subtree
 Visit the root and print the data.
Example:

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Output:
InorderTraversal: 8 12 20 22 25 30 40
PreorderTraversal: 22 12 8 20 30 25 40
Postorde Traversal: 8 20 12 25 40 30 22

Construct A Binary Tree from Inorder and Preorder Traversal

Construct A Binary Tree from Inorder and Preorder Traversal

Given the Inorder and Preorder Traversal of a binary tree, we need to construct the unique
binary tree represented by them.
Example:

Here 10 (first element of preorder) is the root element. So we can find its index in the inorder
traversal(say elem). The left subtree of the root will be present to the left side of in order
whereas the right subtree of root will be present on the right side of elem in the inorder
traversal:

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

We can define a recursive function that creates one node at a time. First, we create the root
node, and then we can take the help of recursion to create its left and right subtrees. In order to
make recursion work, we need to provide the correct inorder and preorder traversal of the
subtree for every recursive call.

Construct Binary Tree from Inorder and PostOrder Traversal

Given Inorder and PostOrder Traversal of a binary tree, we need to construct the unique binary
tree represented by them.
Example:

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Here 10 (last element of postorder) is the root element. So we can find its index in the inorder
traversal(say elem). The left subtree of the root will be present to the left side of inorder
whereas the right subtree of root will be present on the right side of elem in the inorder
traversal:

We can define a recursive function that creates one node at a time. First, we create the root
node, and then we can take the help of recursion to create its left and right subtrees. In order to
make recursion work, we need to provide the correct inorder and postorder traversal of the
subtree for every recursive call.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Expression Tree
he expression tree is a binary tree in which each internal node corresponds to the operator
and each leaf node corresponds to the operand so for example expression tree for 3 +
((5+9)*2) would be:

Prefix Expression; + 3 * + 5 9 2
Postorder Expression: 3 5 9 + 2 * +

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Expression tree for the expression: a*( b + c ) + ((d + e*f)*g):

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

 nfix expression:

(a+(b*c))+(d*(e + f))

 Postfix Expression:

abc*+def+*+

 Prefix Expression:

++a*bc*d+ef

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Binary Tree Representations

A binary tree data structure is represented using two methods. Those methods are as follows...

1. Array Representation

2. Linked List Representation

Consider the following binary tree...

1. Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent
a binary tree.
Consider the above example of a binary tree and it is represented as follows...

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Array Representation of Binary Tree with Example

Number the nodes

number on each node we can easily use the tree for array representation of binary tree and the
array will look like this:

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

2. Linked List Representation of Binary Tree


We use a double linked list to represent a binary tree. In a double linked list, every node
consists of three fields. First field for storing left child address, second for storing actual data
and third for storing right child address.
In this linked list representation, a node has the following structure...

C Function to create BST


PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

struct node * create(struct node *root, int item)


{
if (root == NULL)
{
root=(struct node *)malloc(sizeof(struct node));
root->left = NULL;
root->right = NULL;
root->data = item;
}
else if (item < root->data)
root->left = create(root->left, item);
else
root->right = create(root->right, item);

return root;
}

C Function for Inorder Traversal

void inorder(struct node * root)


{
if (root != NULL)
{
inorder(root->left);
printf(" %d ", root->data);
inorder(root->right);
}
}

C Function for Preorder Traversal

void preorder(struct node * root)


{
if (root != NULL)
{
printf(" %d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

C Function for Postorder Traversal


PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

void postorder(struct node *root)


{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf(" %d ", root->data);
}
}

C Function for search in BSTl

struct node *search(struct node* root, int key)


{
if(root==NULL) /* Element is not found */
return NULL;
if(key > root->data) /* Search in the right sub tree. */
return search(root->right,key);
else if(key < root->data) /* Search in the left sub tree. */
return search(root->left,key);
else /* Element Found */
return root;
}

GRAPH DATA STRUCTURE

Graph Data Structure is a collection of nodes connected by edges. It’s used to represent
relationships between different entities. Graph algorithms are methods used to manipulate
and analyze graphs, solving various problems like finding the shortest path or detecting

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

cycles.

Graphs are commonly represented in two ways:


1. Adjacency Matrix
An adjacency matrix is a 2D array of V x V vertices. Each row and column represent a vertex.
If the value of any element a[i][j] is 1, it represents that there is an edge connecting vertex i and
vertex j.
The adjacency matrix for the graph we created above is

Example: Matrix representation of a graph

Consider the following directed graph G (in which the vertices are ordered as v 1, v2, v3, v4, and
v5), and its equivalent adjacency matrix representation on the right:

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

v1 v2 v3 v4 v5
v1 0 1 0 1 1
v2 0 0 0 1 0
v3 0 0 0 0 1
v4 0 0 0 0 0
v5 0 1 0 0 0

2. Adjacency List
An adjacency list represents a graph as an array of linked lists.
The index of the array represents a vertex and each element in its linked list represents the
other vertices that form an edge with the vertex.
The adjacency list for the graph we made in the first example is as follows:

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Adjacency List of Directed Graph

Difference between BFS and DFS


Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used
for traversing or searching graphs and trees. This article covers the basic difference between
Breadth-First Search and Depth-First Searc

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Difference Between BFS and DFS:


Parameters BFS DFS

BFS stands for Breadth First


DFS stands for Depth First Search.
Stands for Search.

BFS(Breadth First Search) uses


DFS(Depth First Search) uses Stack data
Data Queue data structure for finding
structure.
Structure the shortest path.

DFS is also a traversal approach in which


BFS is a traversal approach in
the traverse begins at the root node and
which we first walk through all
proceeds through the nodes as far as
nodes on the same level before
possible until we reach the node with no
moving on to the next level.
Definition unvisited nearby nodes.

Conceptual BFS builds the tree level by


DFS builds the tree sub-tree by sub-tree.
Difference level.

Approach It works on the concept of FIFO It works on the concept of LIFO (Last In
used (First In First Out). First Out).

BFS is more suitable for


DFS is more suitable when there are
searching vertices closer to the
solutions away from source.
Suitable for given source.

BFS is used in various


DFS is used in various applications such as
applications such as bipartite
acyclic graphs and finding strongly co
Applications graphs, shortest paths, etc.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

HASHING DATA STRUCTURE

Hashing refers to the process of generating a fixed-size output from an input of variable size
using the mathematical formulas known as hash functions. This technique determines an index
or location for the storage of an item in a data structure.

Hash Table is a data structure which stores data in an associative manner. In a hash table, data
is stored in an array format, where each data value has its own unique index value. Access of
data becomes very fast if we know the index of the desired data.

Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash
technique to generate an index where an element is to be inserted or is to be located from.

Hashing

Hashing is a technique to convert a range of key values into a range of indexes of an array.
We're going to use modulo operator to get a range of key values. Consider an example of hash
table of size 20, and the following items are to be stored. Item are in the (key,value) format.

 (1,20)
 (2,70)
 (42,80)
 (4,25)
 (12,44)
 (14,32)
 (17,11)
 (13,78)
 (37,98)

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S


Data Structures and Applications [BCS304]

Sr.No. Key Hash Array Index


1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17

Linear Probing

As we can see, it may happen that the hashing technique is used to create an already used index
of the array. In such a case, we can search the next empty location in the array by looking into
the next cell until we find an empty cell. This technique is called linear probing.

Ke Array After Linear Probing,


Sr.No. Hash
y Index Array Index
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18

Hash Collision

Two or more keys can generate same hash values sometimes. This is called a collision. A
collision can be handled using various techniques. Linear probing is a scheme in computer
programming for resolving collisions in hash tables, data structures for maintaining a collection
of key–value pairs and looking up the value associated with a given key.
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]

et us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85,
92, 73, 101,
which means hash(key)= key% S, here S=size of the table =7,indexed from 0 to 6.We can define
the hash function as per our choice if we want to create a hash table,although it is fixed
internally with a pre-defined formula.

PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S

You might also like