0% found this document useful (0 votes)
10 views25 pages

UNIT III To V DS Pgms

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

UNIT III To V DS Pgms

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

UNIT III

1. Implementation of Binary Search Tree


2. Implementation of AVL Tree
3. Priority queue implementation using Binary Heap (min heap)
UNIT IV:

1. Implementation of Dijkstra’s Algorithm


2. Implementation of Prim’s Algorithm
UNIT V

1. Implementation of Linear Search


2. Implementation of Binary Search
3. Implementation of Insertion Sort
4. Implementation of Selection Sort
5. Implementation of Merge Sort
6. Implementation of Open Addressing Linear Probing
7. Implementation of Open Addressing Quadratic Probing

1.// Binary search Tree

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>

struct tree
{
int data;
struct tree *right,*left;
}*newnode;
typedef struct tree *node;
node t=NULL,u;
int c=0;

node ins(node ,int);


node del(node,int);
node min(node);
void find(node,int);
1
void max(node);
void inorder(node);

void main()
{
int ch,ele;
char cho;
m:printf("\nBINARY SEARCH TREE\n");
printf("----------- ------------ -------\n");
printf("\n\n1.INSERTION\n");
printf("\n2.DELETION\n");
printf("\n3.SEARCHING\n");
printf("\n4.MINIMUM ELEMENT\n");
printf("\n5.MAXIMUM ELEMENT\n");
printf("\n6.EXIT\n");
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{

do
{
printf("\nENTER THE ELEMENT TO BE INSERTED\n");
scanf("%d",&ele);
t=ins(t,ele);
printf("\nENTER YOUR CHOICE(Y/N)\n");
scanf("%s",&ch);
}while(ch=='y'||ch=='Y');
printf("\n\nTHE ELEMENTS ARE(INORDER)\n");
inorder(t);
getch();
goto m;
}
case 2:
{
printf("\nENTER THE ELEMENT TO BE DELETED\n");
scanf("%d",&ele);
find(t,ele);
2
if(c==1)
{
t=del(t,ele);
printf("\nTHE ELEMENT DELETED IS:%d\n",ele);
printf("\n\nTHE ELEMENTS ARE(INORDER)\n");
inorder(t);
}
else
printf("\nELEMENT NOT FOUND\n");
c=0;
getch();
goto m;
}
case 3:
{
printf("ENTER THE ELEMENT TO BE SEARCHED\n");
scanf("%d",&ele);
find(t,ele);
if(c==1)
printf("\nELEMENT FOUND\n");
else
printf("\nELEMENT NOT FOUND\n");
c=0;
getch();
goto m;
}
case 4:
{
u=min(t);
printf("THE MINIMUM ELEMENT IN TREE IS:%d\n",u->data);
getch();
goto m;
}
case 5:
{
max(t);
getch();
goto m;
}
case 6:
3
exit(1);
}
getch();
}

node ins(node t,int x)


{
newnode=(struct tree*)malloc(sizeof(struct tree));
if(t==NULL)
{
newnode->data=x;
newnode->left=NULL;
newnode->right=NULL;
t=newnode;
}
else
{
if(x<=t->data)
t->left=ins(t->left,x);
else
t->right=ins(t->right,x);
}
return t;
}

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

node min(node t)
{
if(t!=NULL)
{
4
if(t->left==NULL)
return t;
else
return min(t->left);
}
return 0;
}

void max(node t)
{
if(t!=NULL)
{
if(t->right==NULL)
printf("THE MAXIMUM ELEMENT IN TREE IS:%d\n",t->data);
else
max(t->right);
}
}

void find(node t,int x)


{
if(t==NULL)
c=-1;
else
{
if(t->data==x)
c=1;
else if(x<t->data)
find(t->left,x);
else if(x>t->data)
find(t->right,x);
}
}

node del(node t,int x)


{
node tmp;
find(t,x);
if(c==1)
{
5
if(x<t->data)
t->left=del(t->left,x);
else if(x>t->data)
t->right=del(t->right,x);
else if(t->left && t->right)
{
tmp=min(t->right);
t->data=tmp->data;
t->right=del(t->right,t->data);
}
else
{
tmp=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tmp);
}
}
return t;
}

2.// C program to implement the avl tree updated

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

// AVL Tree node


struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};

// Function to get height of the node


int getHeight(struct Node* n)
6
{
if (n == NULL)
return 0;
return n->height;
}

// Function to create a new node


struct Node* createNode(int key)
{
struct Node* node
= (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // New node is initially added at leaf
return node;
}

// Utility function to get the maximum of two integers


int max(int a, int b)
{
return (a > b) ? a : b;
}

// Function to get balance factor of a node


int getBalanceFactor(struct Node* n)
{
if (n == NULL)
return 0;
return getHeight(n->left) - getHeight(n->right);
}

// Right rotation function


struct Node* rightRotate(struct Node* y)
{
struct Node* x = y->left;
struct Node* T2 = x->right;

// Perform rotation
x->right = y;
7
y->left = T2;

// Update heights
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;

return x;
}

// Left rotation function


struct Node* leftRotate(struct Node* x)
{
struct Node* y = x->right;
struct Node* T2 = y->left;

// Perform rotation
y->left = x;
x->right = T2;

// Update heights
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;

return y;
}

// Function to insert a key into AVL tree


struct Node* insert(struct Node* node, int key)
{
// 1. Perform standard BST insertion
if (node == NULL)
return createNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
8
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;

// 2. Update height of this ancestor node


node->height = 1
+ max(getHeight(node->left),
getHeight(node->right));

// 3. Get the balance factor of this ancestor node to


// check whether this node became unbalanced
int balance = getBalanceFactor(node);

// 4. If the node becomes unbalanced, then there are 4


// cases

// Left Left Case


if (balance > 1 && key < node->left->key)
return rightRotate(node);

// Right Right Case


if (balance < -1 && key > node->right->key)
return leftRotate(node);

// Left Right Case


if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case


if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}

// Return the (unchanged) node pointer


return node;
9
}

// Function to perform Inorder traversal of AVL tree


void inOrder(struct Node* root)
{
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->key);
inOrder(root->right);
}
}
void preOrder(struct Node* root)
{
if (root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);

preOrder(root->right);
}
}

// Main function
int main()
{
struct Node* root = NULL;

// Inserting nodes
int data,choice;
while(1)
{
printf("\nEnter data for avl tree\n");
scanf("%d",&data);
root = insert(root, data);
printf("Do u want one more data(1/0))\n");
scanf("%d",&choice);
if(choice==0)
break;
}
10
// Print Inorder traversal of the AVL tree
printf("Inorder traversal of AVL tree: \n");
inOrder(root);
printf("\nPreorder traversal of AVL tree:\n ");
preOrder(root);

return 0;
}

3./* Binary Heap (min heap) */


#include <stdio.h>
#include <limits.h>
int heap[100], heapSize;
void Init()
{
heapSize = 0;
heap[0] = -INT_MAX;
}
void Insert(int element)
{
heapSize++;
heap[heapSize] = element;
int now = heapSize;
while (heap[now / 2] > element)
{
heap[now] = heap[now / 2];
now /= 2;
}
heap[now] = element;
}
int DeleteMin()
{
int minElement, lastElement, child, now,i;
minElement = heap[1];
lastElement = heap[heapSize--];
for (now = 1; now * 2 <= heapSize; now = child)
{
child = now * 2;
if (child != heapSize && heap[child + 1] < heap[child])
11
child++;
if (lastElement > heap[child])
heap[now] = heap[child];
else
break;
}
heap[now] = lastElement;
return minElement;
}
main()
{
int number_of_elements,i;
printf("Program to demonstrate Heap:\nEnter the number of elements: ");
scanf("%d", &number_of_elements);
int iter, element;
Init();
printf("Enter the elements: ");
for (iter = 0; iter < number_of_elements; iter++)
{
scanf("%d", &element);
Insert(element);
}
printf("Elements in heap order\n");
for(i=1;i<=heapSize;i++)
printf("%d ",heap[i]);

printf("\n");

//for (iter = 0; iter < number_of_elements; iter++)


printf(" Deleted element is %d ", DeleteMin());
printf("\n");
}

UNIT IV

1.DIJKSTRA'S ALGORITHM
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
12
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;

printf("Enter no.of vertices:");


scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);

}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)

if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
13
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)

if(i!=startnode){
printf("\nDistance of node %d = %d\n",i,distance[i]);
printf("\nPath = %d \n",i);
j=i;
do
{
j=pred[j];
printf(" <- %d",j);
}while(j!=startnode);
}
}

2.prim's algorithm
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{

printf("\nEnter the number of nodes:");


14
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;

printf("\n");
while(ne< n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0||visited[v]==0)
{
printf("\nEdge %d:(%d to %d)cost: %d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimun cost = %d",mincost);
}

//prim's algorithm
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];
15
void main()
{

printf("\nEnter the number of nodes:");


scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;

printf("\n");
while(ne< n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0||visited[v]==0)
{
printf("\nEdge %d:(%d to %d)cost: %d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimun cost = %d",mincost);
}

16
UNIT -V
1.Linear search
#include <stdio.h>
int array[100],n;
int main()
{
int i,search;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d nos for array element\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter search elements\n");
scanf("%d",&search);

for (i = 0; i < n; i++)


{
if (array[i] == search)
{
printf("Element %d found at position %d",search,i);
break;
}
else
if (i == n-1)
printf("Element %d Not found",search);
}
}

2.Binary Search
#include <stdio.h>
int array[100],n;
int main()
{
int i, low, high, mid,key;
void bubble_sort();
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d nos for array element\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
17
bubble_sort();
printf("After sorting\n");
for(i = 0; i < n; i++)
printf("%d ",array[i]);

printf("\nEnter value to search\n");


scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if (array[mid] == key)
{
printf("%d found at location %d\n", key, mid);
break;
}
else if(array[mid] < key)
low = mid + 1;

else
high = mid - 1;

mid = (low + high)/2;


}
if(low > high)
printf("Not found! %d isn't present in the list\n", key);
return 0;
}
void bubble_sort( )
{
int pass,i,temp;
for( pass = 0; pass< n; pass++)
{
for( i = 0; i < n-pass-1; i++)
{
if(array[ i ] > array[ i+1] )
{
temp = array[ i ];
array[ i ] = array[ i+1 ];
18
array[ i + 1] = temp;
}
}
}

3.insertion sort

#include<stdio.h>
int main()
{
int i,j,k,pass,key;
int array[100],n;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d nos for array element\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
for(i=1;i<n;i++)
{
key = array[i];
j = i-1;
while (j >= 0 && array[j] > key)
{
array[j+1] = array[j];
j = j-1;
}
array[j+1] = key;
printf("\nAfter pass %d\n",i);
for(k = 0; k < n; k++)
printf("%d ",array[k]);
}

printf("\nAfter sorting\n");
for(i = 0; i < n; i++)
printf("%d ",array[i]);

19
4.selection sort

#include<stdio.h>
int main()
{
int i,pass,temp;
int array[100],n;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d nos for array element\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
for(pass=0;pass<n-1; pass++)
{
for(i=pass+1;i<n; i++)
{
if(array[pass]>array[i])
{
temp=array[pass];
array[pass]=array[i];
array[i]=temp;
}
}
printf("\nAfter pass %d\n",pass+1);
for(i = 0; i < n; i++)
printf("%d ",array[i]);
}

printf("\nAfter sorting\n");
for(i = 0; i < n; i++)
printf("%d ",array[i]);

5.Merge sort
#include <stdio.h>
void mergeSort(int [], int, int, int);
void partition(int [],int, int);

int main()
20
{

int list[50];
int i, size;
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
scanf("%d", &list[i]);
partition(list, 0, size - 1);

printf("After merge sort:\n");

for(i = 0;i < size; i++)


printf("%d ",list[i]);
return 0;
}

void partition(int list[],int low,int high)


{
int mid;
if(low < high)
{
mid = (low + high) / 2;

partition(list, low, mid);

partition(list, mid + 1, high);

mergeSort(list, low, mid, high);

}
}

void mergeSort(int list[],int low,int mid,int high)


{

int i, j, k=low,temp[50];
i = low;
21
j = mid + 1;
while ((i <= mid) && (j <= high))
{
if (list[i] <= list[j])
temp[k++] = list[i++];
else
temp[k++] = list[j++];
}

while (i <= mid)


temp[k++] = list[i++];
while (j <= high)
temp[k++] = list[j++];

for (k = low; k <= high; k++)


list[k] = temp[k];

6. Implementation of Open Addressing Linear Probing


/* HASHING - LINEAR PROBING */

#include <stdio.h>
#include <conio.h>
int tsize;

int hashfunction(int key)


{
int i ;
i = key%tsize ;
return i;
}

//-------LINEAR PROBING-------
int rehashlinear(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
22
}

void main()
{
int key,arr[20],hash[20],i,n,s,k;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);

printf ("\nEnter the number of elements: ");


scanf ("%d",&n);

for (i=0;i<tsize;i++)
hash[i]=-1 ;

for (i=0;i<n;i++)
{
printf ("Enter Element %d : ",i);
scanf("%d",&arr[i]);
}

for (i=0;i<tsize;i++)
hash[i]=-1 ;

for(k=0;k<n;k++)
{
key=arr[k] ;
i = hashfunction(key);
while (hash[i]!=-1)
{
i = rehashlinear(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
23
}

7. Implementation of Open Addressing Quadratic Probing


/* HASHING - quadratic PROBING */

#include<stdio.h>
#include<conio.h>
#define MAX 10
int i,j;
void printArray(int arr[],int tsize)

{
printf("The Quadratic Probing Hashtable");
for(i= 0;i<tsize;i++)
{
printf("\n[%d]%d",i,arr[i]);
}
}
void hashing(int table[],int tsize,int arr[],int N)
{
for(i=0;i<N;i++)
{
int hv=arr[i]%tsize;
if (table[hv] == -1)
table[hv]=arr[i];
else
{
for(j=0;j<tsize;j++)
{
int t = (hv + j * j) % tsize;
if(table[t]==-1)
{
table[t]=arr[i];
break;
}
}
24
}
}
printArray(table,tsize);
}
void main()
{
int arr[MAX],i,n,s=10;
printf("\nEnter the no of element for table n:");
scanf("%d",&n);
for(i=0;i<n;i++)

{
printf("\nEnter the Number %d :",i);
scanf("%d",&arr[i]);
}
int hash_table[MAX];
for(i=0;i<s;i++)
{
hash_table[i]=-1;
}
hashing(hash_table,s,arr,n);
}

25

You might also like