Final Assignment It Radha

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 73

Q 1.

Write a program to find the largest among three


number-
Source code-
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
//getch();
return 0;
}

Output-
Q2.Write a program to find average of five student-
Source code-
#include<iostream.h>
#include<conio.h>
void main()
{
float a[4]; float sum,avg;
clrscr();
cout<<"\n enter first student no:";
cin>> a[0];
cout<<"\n enter second student no:";
cin>> a[1];
cout<<"\n enter third student no:";
cin>>a[2];
cout<<"\n enter forth student no:";
cin>>a[3];
cout<<"\n enter fifth student no:";
cin>>a[4];
sum = a[0]+a[1]+a[2]+a[3]+a[4];
avg = sum/5;
cout<<"average fifth student no is :"<<avg;
getch();
}
Output-
Q3.Write a program to search given number in array-
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10],i,n,ele;
printf("Enter array size:");
scanf("%d", &n);
printf("Enter array elements:");
for (i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to search:");
scanf("%d", &ele);
for (i=0; i<n; i++)
{
if (arr[i] == ele)
{
printf("%d found at position %d", ele, i+1);
return 0;
}
}
printf("element not found");
}
Output-
Q4.Write a program to count the element to given array –
Source Code-
#include<iostream.h>
#include<conio.h>
void main()
{
int n,array[100];
clrscr();
cout<<"\n Enter array size:";
cin>>n;
cout<<"Enter array element:";
for(int i=1;i<=n;i++)
{
cin>>array[i];
}
cout<<"array elements is:";
for(i=1;i<=n;i++)
{
cout<<"\t"<<array[i];
}
cout<<"\nTotal array elements is:"<<i-1;
getch();
}

Output-
Q5. Write a program to print the all element in array-
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5], i;
printf("Enter Array Element:");
for(i=0; i<5; i++)
{
scanf("%d", &a[i]);
}
printf("\n Array Element:");
for (i=0; i<5; i++)
{
printf("%d", a[i]);
}
return 0;
//getch();
}

Output-
Q6. Write a program to print matrix-
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,m,n;
int matrix[10] [20];
printf("Enter number of rows:");
scanf("%d",&m);
printf("Enter number of columns:");
scanf("%d", &n);
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("Enter data in [%d] [%d]:", i,j);
scanf("%d", &matrix[i][j]);
}
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
//getch();
}

Output-
Q7. Write a program to print diagonal matrix of the matrix.
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int array1 [10][10], i,j,m,n,sum = 0;
printf("Enter no of rows::");
scanf("%d", &m);
printf("\n Enter no of colums::");
scanf("%d", &n);
printf("\Enter value of the matrix ::\n");
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\n Enter a[%d][%d] value::",i,j);
scanf("%d", &array1 [i][j]);
}
}
printf("\n the diagonal element of a matrix are:\n\n");
if(m==n)
{
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(i==j)
printf("\t%d", array1 [i][j]);
else
printf("\t");
}
printf("\n\n");
}
}
else
{
printf("\n matrix is not a sqare matrix:");
}
return 0;
//getch();
}

Output-
Q8.Write a program to find sum, multiplication and inverse
of matrix.
Source Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],c[3][3]={0},d[3][3]={0},k,m,n,p,q;
clrscr();
printf("Enter number of rows and columns in A:");
scanf("%d%d",&m,&n);
printf("Enter number of rows and columns in B:");
scanf("%d%d",&p,&q);
printf("Enter elements of matrix A:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter elements of matrix B:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Result of Matrix Addition\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
for(j=0;j<q;j++)
for(k=0;k<p;k++)
d[i][j]+=a[i][k]*b[k][j];
printf("\n Result of Matrix Multiplication:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",d[i][j]);
printf("\n");
getch();
}
}
Output:-
Q9.Write a program to insert an item into linear array.
Source Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int array[50],position,c,n,value;
printf("Enter number of elements in the array");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Please enter the location where you want to insert an new element\n");
scanf("%d",&position);
printf("Please enter the value");
scanf("%d",&value);
for(c=n-1;c>=position-1;c--)
array[c+1]=array[c];
array[position-1]=value;
printf("Resultant array is:\n");
for(c=0;c<=n;c++)
printf("%d\n",array[c]);
getch();
}
Output:-
Q10.Write a program to delete an item from the linear
array-
Source code-
#include<stdio.h>
#include<conio.h>
void main()
{
int i, size, pos;
int a[] = {2,4,6,8,12};
size = size of(a)/ size of(a[0];
printf("the array elements before deletion operation:\n");
for (i=0; i<size; i++)
printf("a[%d] = %d\n", i, a[i]);
printf("\n Enter the position from where you wish to delet the element:");
scanf("%d", &pos);
printf("\n the array element after deletion operation are:\n");
for(i=pos-1; i<size; i++)
a[i] = a[i+1];
size = size -1;
for(i=0; i<size; i++)
printf("a[%d] = %d\n", i, a[i]);
}
Output-
Q11. Write a program in bubble short-
Source Code-
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
//getch()
}
Output-
Q12.Write a program into search an item with the help of
binary search.
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}

Output-
Q13.Write a program to print all the elements of linear
linked list.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}
*head;
void createlist(int n);
void display();
int main()
{
int n,index;
printf("enter number of nodes to create;");
scanf("%d",&n);
createlist(n);
printf("\n data in list:\n");
display();
return 0;
}
void createlist(int n)
{
struct node *newnode,*temp;
int data,i;
head=malloc(sizeof(struct node));
if(head==NULL)
{
printf("unable to allocate memory");
exit(0);
}
printf("enter data of node 1:");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newnode=malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("unable to allocate memory");
exit(0);
}
printf("enter data of node %d:",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
void display()
{
struct node*temp;
if(head==NULL)
{
printf("list is empty \n"); getch();
return;
}
temp=head;
while(temp!=NULL)
{
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n");
}
Output:-
Q.14 write a program to search an item into linked list-
Source Code-
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};

void display (struct Node *node)


{

//as linked list will end when Node is Null


while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}

int searchElement (struct Node *head, int item)


{
struct Node *current = head; // Initialize current
int index = 0;
// traverse till then end of the linked list
while (current != NULL)
{
if (current->data == item)
{
return index;
}
current = current->next;
index++;
}
return -1;
}

int main ()
{
int item; int index;

//creating 4 pointers of type struct Node


//So these can point to address of struct type variable
struct Node *head = NULL;
struct Node *node2 = NULL;
struct Node *node3 = NULL;
struct Node *node4 = NULL;

// allocate 3 nodes in the heap


head = (struct Node *) malloc (sizeof (struct Node));
node2 = (struct Node *) malloc (sizeof (struct Node));
node3 = (struct Node *) malloc (sizeof (struct Node));
node4 = (struct Node *) malloc (sizeof (struct Node));

head->data = 10; // data set for head node


head->next = node2; // next pointer assigned to address of node2
node2->data = 15;
node2->next = node3;
node3->data = 20;
node3->next = node4;
node4->data = 25;
node4->next = NULL;
printf ("\nLinked List: ");
display (head);
printf ("Enter element to search: ");
scanf ("%d", &item);
index= searchElement (head, item);
if (index == -1)
printf ("Item not found");
else
printf ("Item found at position: %d", index + 1);

return 0;
}
Output:-
Q15. Write a program to find the location of an item into
sorted Linked list .
Source Code:-
#include <stdio.h>
#include <stdlib.h>

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

int findLocationSorted(struct Node* head, int key) {


struct Node* current = head;
int position = 1;

while (current != NULL && current->data < key) {


current = current->next;
position++;
}

return (current && current->data == key) ? position : -1; // key not found
}

int main() {
struct Node* head = NULL;

// Assume you have a sorted linked list; populate it as needed

int key = 30;


int position = findLocationSorted(head, key);

if (position != -1)
printf("Item %d found at location %d", key, position);
else
printf("Item %d not found in the sorted linked list", key);

return 0;
}
Output:-
Q16. Write a program to insert new node into linked
list as First Node .
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{ int data;
struct Node *next;
};
void linkedlisttraversal(struct Node *ptr)
{ while(ptr!=NULL)
{ printf("yash %d\n",ptr->data);
ptr=ptr->next;
}
}
struct Node * insertatfirst(struct Node * head,int data)
{
struct Node * ptr=(struct Node *) malloc(sizeof(struct Node));
ptr->next=head;
ptr->data=data;
return ptr;
}
int main()
{ struct Node *head;
struct Node *second;
struct Node *third;
//Allocate memory for nodes in the heap
head=(struct Node *) malloc(sizeof(struct Node));
second=(struct Node *) malloc(sizeof(struct Node));
third=(struct Node *) malloc(sizeof(struct Node));
//link first and second
head->data=5;
head->next=second;
second->data=11;
second->next=third;
third->data=12;
third->next=NULL;

printf("linked list before addition\n");


linkedlisttraversal(head);
head=insertatfirst(head,55);
printf("linkd list after addition\n");
linkedlisttraversal(head);
return 0;
}

Output:-
Q17.Write a program to insert new node into linked list as
particular node-
Source Code-
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct Node

{ int data;

struct Node *next;

};

void linkedlisttraversal(struct Node *ptr)

{ while(ptr!=NULL)

{ printf("yash %d\n",ptr->data);

ptr=ptr->next;

struct Node * insertatfirst(struct Node * head,int data)

struct Node * ptr=(struct Node *) malloc(sizeof(struct Node));

ptr->next=head;

ptr->data=data;

return ptr;

int main()

{ struct Node *head;

struct Node *second;

struct Node *third;

//Allocate memory for nodes in the heap


head=(struct Node *) malloc(sizeof(struct Node));

second=(struct Node *) malloc(sizeof(struct Node));

third=(struct Node *) malloc(sizeof(struct Node));

//link first and second

head->data=5;

head->next=second;

second->data=11;

second->next=third;

third->data=12;

third->next=NULL;

printf("linked list before addition\n");

linkedlisttraversal(head);

head=insertatfirst(head,55);

printf("linkd list after addition\n");

linkedlisttraversal(head);

return 0;

Output:-
Q18. Write a program to insert new node into linked
list as End Node .
Source Code:-
#include <stdio.h>

//Represent a node of the singly linked list


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

//Represent the head and tail of the singly linked list


struct node *head, *tail = NULL;

//addAtEnd() will add a new node to the end of the list


void addAtEnd(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty


if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//display() will display all the nodes present in the list


void display() {
//Node current will point to head
struct node *current = head;

if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Adding nodes to the end of the list: \n");
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main()
{
//Adding 1 to the list
addAtEnd(1);
display();

//Adding 2 to the list


addAtEnd(2);
display();

//Adding 3 to the list


addAtEnd(3);
display();

//Adding 4 to the list


addAtEnd(4);
display();

return 0;
}
Output:-
Q19.Write a program to insert an element into stack.
Source Code-

#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
voidpush();
voiddisplay();
intmain()
{
clrscr();
top=-1;
printf("\n enter the size of stack:");
scanf("%d",&n);
printf("\n STACK OPERATION USING ARRAY");
printf("\n\t 1.PUSH\n\t 2.DISPLAY\n\t 3.EXIT");
do
{
printf("\n enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
display();
break;
}
case 3:
{ printf("\n\t exit point");
break;
}
default:\
{
printf("\n\t please enter a valid choice(1/2/3)");
}
}
}
while(choice!=3);
return 0;
}
voidpush()
{
if(top>=n-1)
{
printf("\n\t stack over");
}
else
{
printf("enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
voiddisplay()
{
if(top>=0)
{
printf("\n the elements in stack \n");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\n press next choice");
}
else
{
printf("\n the stack is empty");
}
}

Output-
Q20.write a program to delete an item from stack.
Source Code-
#include<stdio.h>
#include<conio.h>
#define MAX 50
void push();
void pop();
void display();

int stack[MAX],top=-1,item;
void main()
{
int ch;
clrscr();
do
{
printf("\n1.\tpush\n2.\tpop\n3.\tdisplay\n4.\texit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{

case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("invalid entry please try again...");
}
}
while(ch!=4);
getch();
}
void push()
{
if(top==MAX-1)
printf("\n stack is full.");
{
printf("enter item:");
scanf("%d",&item);
top++;
stack[top]=item;
printf("\n item inserted :%d",item);
}
}
void pop()
{
if(top==-1)
printf("stack is empty.");
else{
item=stack[top];
top--;
printf("\n item deleted:%d",item);
}
}
void display()
{int i;
if(top==-1)
printf("stack is empty");
else
{
printf("stack elements are:");
for(i=top;i>=0;i--)
printf("%d\t",stack[i]);
}
}

Output:-
Q21.write a program to insert an item into queue.
Source Code-
#include<iostream.h>
#include<conio.h>
#include<process.h>
void instque();
void display();
int queue[5];
int front=-1,rear=-1;
void main()
{
int choice;
clrscr();
do
{
cout<<"1.insert an element in queue \n";
cout<<"2.display queue \n";
cout<<"3.exit\n";
cout<<"enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
instque();
break;
case 2:
display();
break;
case 3:
exit(0);
}
}while(choice!=3);
getch();
}
void instque()
{
int info;
cout<<"enter the element to be inserted";
cin>>info;
if(front==-1)
{
front=0;
rear=0;
}
else
{rear=rear+1;
}
queue[rear]=info;
}
void display()
{
if(front==-1)
{
cout<<"the queue is empty";
}
else{
for(int i=0;i<=rear;i++)
{cout<<"queue is:"<<queue[i]<<"\t" ;
}
}
cout<<"\n";
}

Output:-
Q22.write a program to delete an item from queue.
Source Code-
#include<iostream.h>
#include<conio.h>
#include<process.h>
void delqueue();
void display();
int queue[5]={11,12,13,15,16};
int front=0,rear=4;
void main()
{ int choice;
clrscr();
do
{
cout<<"1.delete an element from queue \n";
cout<<"2.display queue\n";
cout<<"3.exit\n";
cout<<"enter your choice";
cin>>choice;
switch(choice)
{
case 1:
delqueue();
break;
case 2:
display();
break;
case 3:
exit(0);
}
}while(choice!=0);
getch();
}
void delqueue()
{
int info;
if(front!=-1)
{
info=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else{
front=front+1;
}
cout<<"delete element is:"<<info<<"\n";
}
else
cout<<"queuer is empty";
}
void display()
{
if(front==-1)
cout<<"queue is empty";
else{
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<"\t";
}
}
cout<<"\n";
}

Output:-
Q23. Write a program to sort the given element using
Quick Sort.
Source Code:-
#include <stdio.h>

// function to swap elements


void swap(int *a, int *b) {
int t = *a; int j;
*a = *b;
*b = t;
}

// function to find the partition position


int partition(int array[], int low, int high) {

// select the rightmost element as pivot


int pivot = array[high];

// pointer for greater element


int i = (low - 1);
int j;
// traverse each element of the array
// compare them with the pivot
for ( j = low; j < high; j++) {
if (array[j] <= pivot) {

// if element smaller than pivot is found


// swap it with the greater element pointed by i
i++;

// swap element at i with element at j


swap(&array[i], &array[j]);
}
}

// swap the pivot element with the greater element at i


swap(&array[i + 1], &array[high]);

// return the partition point


return (i + 1);
}

void quickSort(int array[], int low, int high) {


if (low < high) {

// find the pivot element such that


// elements smaller than pivot are on left of pivot
// elements greater than pivot are on right of pivot
int pi = partition(array, low, high);

// recursive call on the left of pivot


quickSort(array, low, pi - 1);

// recursive call on the right of pivot


quickSort(array, pi + 1, high);
}
}

// function to print array elements


void printArray(int array[], int size) { int i;
for ( i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}

// main function
void main() { int j;
int data[] = {8, 7, 2, 1, 0, 9, 6};

int n = sizeof(data) / sizeof(data[0]); clrscr();

printf("Unsorted Array\n");
printArray(data, n);

// perform quicksort on data


quickSort(data, 0, n - 1);

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


printArray(data, n); getch();
}
Output:-
Q24.Write a program to sort the given element using
insertion sort.
Source Code-
#include<iostream.h>
#include<conio.h>
void insertion_sort(int a[],int n)
{
int i,j,temp;
i=1;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0&&temp<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
void main()
{
int i;
int a[10],n;
clrscr();
cout<<"enter the size of array:";
cin>>n;
cout<<"\nenter the element for list:\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<"\nafter sorting list:";
insertion_sort(a,n);
for(i=0;i<=4;i++)
{
cout<<a[i]<<" ";
}
getch();
}
Output:
Q25. Write a program to sort the given element using
Selection Sort.
Source Code:-
#include<iostream.h>
#include<conio.h>
int min(int a[],int k,int n)
{
int loc,min;

min=a[k];
loc=k;
for(int j=k+1;j<=n-1;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
return loc;
}
void main()
{
int a[10],i,temp,n;
int loc;
clrscr();
cout<<"\nenter size of list:";
cin>>n;
cout<<"\nenter the element for list:\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<"before sorting element in list:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
for(i=0;i<=n-1;i++)
{
loc=min(a,i,n);
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nafter sorting element in list:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}

Output:-
Q26. Write a program to print the all nodes of Tree
using Preorder traversal.
Source Code:-
#include<stdio.h>
#include<stdlib.h>
// We are creating struct for the binary tree below
struct node
{
int data;
struct node *left, *right;
};

// newNode function for initialisation of the newly created node


struct node *newNode (int item)
{
struct node *temporary = (struct node *) malloc (sizeof (struct node));
temporary->data = item;
temporary->left = temporary->right = NULL;
return temporary;
}

// Here we print the preorder recursively


void preorder (struct node *root)
{
if (root != NULL)
{
printf ("%d ", root->data);
preorder (root->left);
preorder (root->right);
}
}
// Basic Program to insert new node at the correct position in BST
struct node *insert (struct node *node, int data)
{
/* When there no node in the tree(subtree) then create
and return new node using newNode function */
if (node == NULL)
return newNode (data);

/* If not then we recur down the tree to find correct position for insertion */
if (data < node->data)
node->left = insert (node->left, data);
else if (data > node->data)
node->right = insert (node->right, data);

return node;
}

void main ()
{
/* What our binary search tree looks like really
9
/\
7 14
/\/\
5 8 11 16 */

struct node *root = NULL;


root = insert (root, 9);
insert (root, 7);
insert (root, 5);
insert (root, 8); clrscr();
insert (root, 14);
insert (root, 11);
insert (root, 16);
printf ("The preorder is :\n");
preorder (root);

getch();
}
Output:-
Q27. Write a program to print the all nodes of Tree
using Inorder traversal.
Source Code:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
/*To create a new node*/
struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in Inorder*/
void traverseInorder(struct node* root)
{
if (root == NULL)
return;
traverseInorder(root->left);
printf(" %d ", root->element);
traverseInorder(root->right);
}
void main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
printf("\n The Inorder traversal of given binary tree is -\n");
traverseInorder(root);
getch();
}
Output:-
Q28. Write a program to print the all nodes of Tree
using Postorder traversal.
Source Code:-
#include <stdio.h>
#include <stdlib.h>

struct node {
int element;
struct node* left;
struct node* right;
};

/*To create a new node*/


struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;

return (Node);
}

/*function to traverse the nodes of binary tree in postorder*/


void traversePostorder(struct node* root)
{
if (root == NULL)
return;
traversePostorder(root->left);
traversePostorder(root->right);
printf(" %d ", root->element);
}

int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);

printf("\n The Postorder traversal of given binary tree is -\n");


traversePostorder(root);
return 0;
}
Output:-
Q29. Write a program to insert an item into BST(binary
search tree)
Source Code:-
#include <stdio.h>
#include <stdlib.h>

struct node {
int key;
struct node *left, *right;
};

// A utility function to create a new BST node


struct node* newNode(int item)
{
struct node* temp
= (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST


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

// A utility function to insert


// a new node with given key in BST
struct node* insert(struct node* node, int key)
{
// If the tree is empty, return a new node
if (node == NULL)
return newNode(key);

// Otherwise, recur down the tree


if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);

// Return the (unchanged) node pointer


return node;
}

// Driver Code
void main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80); clrscr();

// Print inorder traversal of the BST


inorder(root);

getch();
}
Output:-
Q30. Write a program to delete an item into
BST(binary search tree)
Source Code:-
#include <stdio.h>
#include <stdlib.h>

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

// A utility function to create a new BST node


struct Node* newNode(int item)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST


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

/* A utility function to insert a new node with given key in


* BST */
struct Node* insert(struct Node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);

/* Otherwise, recur down the tree */


if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);

/* return the (unchanged) node pointer */


return node;
}

/* Given a binary search tree and a key, this function


deletes the key and returns the new root */
struct Node* deleteNode(struct Node* root, int k)
{
// Base case
if (root == NULL)
return root;

// Recursive calls for ancestors of


// node to be deleted
if (root->key > k) {
root->left = deleteNode(root->left, k);
return root;
}
else if (root->key < k) {
root->right = deleteNode(root->right, k);
return root;
}

// We reach here when root is the node


// to be deleted.

// If one of the children is empty


if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}

// If both children exist


else {

struct Node* succParent = root;

// Find successor
struct Node* succ = root->right;
while (succ->left != NULL) {
succParent = succ;
succ = succ->left;
}

// Delete successor. Since successor


// is always left child of its parent
// we can safely make successor's right
// right child as left of its parent.
// If there is no succ, then assign
// succ->right to succParent->right
if (succParent != root)
succParent->left = succ->right;
else
succParent->right = succ->right;

// Copy Successor Data to root


root->key = succ->key;

// Delete Successor and return root


free(succ);
return root;
}
}

// Driver Code
void main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct Node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60); clrscr();

printf("Original BST: ");


inorder(root);

printf("\n\nDelete a Leaf Node: 20\n");


root = deleteNode(root, 20);
printf("Modified BST tree after deleting Leaf Node:\n");
inorder(root);

printf("\n\nDelete Node with single child: 70\n");


root = deleteNode(root, 70);
printf("Modified BST tree after deleting single child Node:\n");
inorder(root);

printf("\n\nDelete Node with both child: 50\n");


root = deleteNode(root, 50);
printf("Modified BST tree after deleting both child Node:\n");
inorder(root);

getch();
}
Output:-
Q31.Write a program to demonstrate breadth first search.
Source Code-
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v) {
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main() {
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i); else
printf("\n Bfs is not possible");
getch();
}
Output:
Q32.Write a program demonstrate to depth first search.
Source Code-
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
voiddfs(int v) {
int i;
reach[v]=1;
for (i=1;i<=n;i++)
if(a[v][i] && !reach[i]) {
printf("\n %d->%d",v,i);
dfs(i);
}
}
int main() {
inti,j,count=0;
printf("\n Enter number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for (i=1;i<=n;i++) {
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected"); else
printf("\n Graph is not connected");
return 0;
}

Output:

You might also like