0% found this document useful (0 votes)
31 views12 pages

DSA Lab Programs: Trees, Sorts, Searches

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)
31 views12 pages

DSA Lab Programs: Trees, Sorts, Searches

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/ 12

DSA Lab Internal III Programs

1) Traversal Of Trees :
#include<stdio.h>
#include<stdlib.h>

typedef struct Node {


int data;
struct Node * left , * right;

}node;
node * que[20];
node * root = NULL ,*curr;

int f=0, r=0 , ele;


node* NodeCreate()
{
printf("\n Enter Numbers (-1 to quit ) : ");
scanf("%d",&ele);
while (ele!=-1)
{
node * temp = (node*) malloc (sizeof(node));
temp->data = ele;
temp->left = temp->right = NULL;
if(root== NULL){
root = temp;
que[r] = temp;
}
else{
curr = que[f];
if(curr->left == NULL)
curr->left = temp;
else
curr->right = temp;
que[++r] = temp;
if(curr->left!=NULL && curr->right != NULL)
f++;
}
printf("\nEnter element (-1 to quit : ) : ");
scanf("%d",&ele);
}
return root;
}
void inOrder(node *ptr){
if(ptr!=NULL){
inOrder(ptr->left);
printf("%d ",ptr->data);
inOrder(ptr->right);
}
}
void preOrder(node * ptr){
if(ptr!=NULL){
printf("%d ",ptr->data);
preOrder(ptr->left);
preOrder(ptr->right);
}
}

void postOrder(node * ptr){


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

void main(){
node * p = NodeCreate();
printf("\nInorder Traversal : ");
inOrder(p);
printf("\nPreorder Traversal : ");
preOrder(p);
printf("\nPostorder Traversal : ");
postOrder(p);
}

2) Binary Search Tree :


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

typedef struct Node


{
int data;
struct Node *left,*right;
}node;

node * insert(node * ptr,int ele){


if(ptr == NULL){
node * temp = (node*) malloc(sizeof(node));
temp->data = ele;
temp->left = temp->right = NULL;
return temp;
}
else if(ele > (ptr->data))
ptr->right = insert(ptr->right,ele);
else if(ele < (ptr->data))
ptr->left = insert(ptr->left,ele);

return ptr;
}
node * findmin(node * ptr){
if(ptr == NULL) return NULL;
if(ptr->left)
return findmin(ptr->left);
else
return ptr;
}
node * del(node * ptr,int ele){
node * p;
if(ptr == NULL)
printf("\nElement not found");
else if( ele < ptr->data)
ptr->left = del(ptr->left , ele);
else if (ele > ptr->data )
ptr->right = del(ptr->right , ele);
else{
if(ptr->right && ptr->left){
p = findmin(ptr->right);
ptr->data = p->data;
ptr->right = del(ptr->right,p->data);
}
else{
p = ptr;
if(ptr->left == NULL )
ptr = ptr->right;
else if(ptr->right == NULL )
ptr = ptr->left;
free(p);
}
}
return ptr;
}
node * search(node * ptr , int ele){
if(ptr == NULL)
return NULL;
if(ele > ptr->data)
return search(ptr->right , ele);
else if( ele < ptr->data)
return search(ptr->left,ele);
else
return ptr;
}

void inOrder(node * ptr){


if(ptr!=NULL){
inOrder(ptr->left);
printf("%d ",ptr->data);
inOrder(ptr->right);
}
}
void main(){
node * root = NULL,*p;
int ele, ch;
do{
printf("\nBST Operations: \n");
printf("1.Insert\n2.Delete\n3.Inorder\n4.Search\n5.EXIT\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch){
case 1 :
printf("\nEnter the Element to be insert : ");
scanf("%d",&ele);
root = insert(root,ele);
break;
case 2 :
printf("\nEnter Element to Deleted : ");
scanf("%d",&ele);
root = del(root, ele);
break;
case 3 :
printf("\nInorder Traversal : ");
inOrder(root);
break;
case 4 :
printf("\nEnter Element to be Searched : ");
scanf("%d",&ele);
p = search(root,ele);
if(p == NULL)
printf("\nElement not found");
else
printf("\n %d found. \n",p->data);
break;
case 5 :
exit(0);
}
}while(ch>=1 && ch <=5);
}

3) Linear Search :
#include <stdio.h>

int linearSearch(int arr[], int size, int key) {

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

if (arr[i] == key) {

return i;

return -1;

int main() {

int n, key;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

}
printf("Enter the element to search: ");

scanf("%d", &key);

int result = linearSearch(arr, n, key);

if (result != -1) {

printf("Element found at index %d\n", result);

} else {

printf("Element not found in the array.\n");

return 0;

4) Binary Search :
#include <stdio.h>

void main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int arr[n];
int first, last, middle, key, i;
printf("Enter %d elements in array in ascending order:\n",
n);//reading in ascending form
for (i = 0; i < n; i++) {
printf("Enter the value of arr[%d]: ", i);
scanf("%d", &arr[i]);
}

printf("Enter the value of search element: ");


scanf("%d", &key);
first = 0;
last = n - 1;
middle = (first + last) / 2;

while (first <= last) {


if (arr[middle] < key) {
first = middle + 1;
} else if (arr[middle] == key) {
printf("Element found at index %d\n", middle);
break;
} else {
last = middle - 1;
}
middle = (first + last) / 2;
}

if (first > last) {


printf("Element not found in the array\n");
} }

5) Selection Sort :
void main() {
int size;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];
int i,j;
printf("Enter the elements of the array:\n");
for ( i = 0; i < size; i++) {
printf("Enter the value of arr[%d]: ",i);
scanf("%d", &arr[i]); // Input elements of the array
}

for ( i = 0; i < size - 1; i++) {


int minIndex = i;

for (j = i + 1; j < size; j++) {


if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

int temp = arr[minIndex];


arr[minIndex] = arr[i];
arr[i] = temp;
}

printf("Sorted array in ascending order:\n");


for ( i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}

6) Insertion Sort :
#include <stdio.h>

int arr[100];
int n;

void insertionSort() {
int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

int main() {
int i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("\nElements before Sort : ");


for(i = 0;i < n;i++)
printf("%d \t",arr[i]);

insertionSort();
printf("\nElements After Sort : ");
for (i = 0; i < n; i++) {
printf("%d \t", arr[i]);
}
printf("\n");
return 0;
}

7) Heap Sort :
#include<stdio.h>

int a[40],n,i,j,temp;
void adjust(int i,int n){
j = (2*i)+1;
while (j<=(n-1))
{
if(j<n-1)//Strightly less means has a right child
{
if(a[j] < a[j+1])
j++;
}
if(a[i] < a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
i = j;
j = (2*i)+1;
}
}
void HeapSort(){
for ( i = ((n/2)-1); i >= 0; i--)
adjust(i,n);

for(i = n-1; i>=0 ;i--){


temp = a[0];
a[0] = a[i];
a[i] = temp;
adjust(0,i);
}
}
void main(){
printf("Enter the Number of Elements : ");
scanf("%d",&n);
printf("\nEnter the Elements : ");
for(i=0 ;i < n;i++)
scanf("%d",&a[i]);
printf("\nElements before Sort : ");
for(i=0;i<n;i++)
printf("%d \t",a[i]);

HeapSort();
printf("\nElements After Sort : ");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
}

8) BFS :
#include<stdio.h>

void BFS();
int A[10][10],vis[10],ex[10],n,queue[10],f,r,i,j,k,s;

void main(){
f=r=0;
printf("\nEnter Number of Vertices : ");
scanf("%d",&n);
printf("\nEnter Adjcency Matrix :\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&A[i][j]);

for(i=1;i<=n;i++)
{
vis[i] = 0;
ex[i] = 0;
}
printf("\nEnter the Source Vetiex : ");
scanf("%d",&s);
BFS();
}
void BFS(){
vis[s]=1;
ex[s]=1;
printf("\nBFS Traversing : \n");
printf("%d\t",s);
k = 1;
while(k<n){
for(i=1;i<=n;i++)
{
if(A[s][i] == 1 && vis[i] == 0 && ex[i] == 0)
{
queue[r++] = i;
ex[i] = 1;
}
}
s=queue[f++];
vis[s] = 1;
printf("%d\t",s);
k++;
}
}

9) DFS :
#include<stdio.h>

void DFS();
int A[10][10],vis[10],n,stack[10],i,j,k,s,top;

void main(){
top=-1;
printf("\nEnter Number of Vertices : ");
scanf("%d",&n);
printf("\nEnter Adjcency Matrix :\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&A[i][j]);

for(i=1;i<=n;i++)
vis[i] = 0;

printf("\nEnter the Source Vetiex : ");


scanf("%d",&s);
DFS();
}
void DFS(){
vis[s] = 1;
k=1;
stack[++top] = s;
printf("DFS Traversal : %d\t",s);
while(k<n){
for(i =1;i<=n;i++){
if(A[s][i] == 1 && vis[i] ==0){
s=i;
stack[++top] = s;
vis[i] = 1;
printf("%d\t",s);
break;
}
}
if(i>n)
s=stack[--top];
else
k++;

}
printf("\n");
}

You might also like