0% found this document useful (0 votes)
205 views15 pages

C1 Assignment - 2

Uploaded by

SIDDHESH VERMA
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)
205 views15 pages

C1 Assignment - 2

Uploaded by

SIDDHESH VERMA
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/ 15

SYBSc(CS) VCACS DS and AlgoII

Assignment 1: Binary Search Tree and Traversals

Set A
a) Implement a Binary search tree (BST) library (btree.h) with
operations – create, search, insert, inorder, preorder and postorder.
Write a menu driven program that performs the
above operations.

btree.h

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20

typedef struct node


{
int info;
struct node *left,*right;
}NODE;

NODE * createbst(NODE *root)


{
NODE *newnode,*temp,*parent;
int i,n,num;
printf("how many nodes");
scanf("%d",&n);
printf("enter the %d elements:",n);
for(i=1;i<=n;i++)
{
newnode=(NODE *)malloc(sizeof(NODE));
Prepared by: Trupti S. Gaikwad Page 1
SYBSc(CS) VCACS DS and AlgoII

printf("\n enter data");


scanf("%d",&num);
newnode->info=num;
newnode->left=newnode->right=NULL;
if(root==NULL)
{
root=newnode;
continue;
}
temp=root;
while(temp!=NULL)
{
parent=temp;
if(num<temp->info)
temp=temp->left;
else
temp=temp->right;
}
if(num<parent->info)
parent->left=newnode;
else
parent->right=newnode;
}
return(root);
}
NODE * search(NODE *root, int key)
{
NODE *temp=root;
while(temp!=NULL)
{

Prepared by: Trupti S. Gaikwad Page 2


SYBSc(CS) VCACS DS and AlgoII

if(key==temp->info)
return temp;
else if(key<temp->info)
temp=temp->left;
else
temp=temp->right;
}
return NULL;
}
NODE *insertbst(NODE *root, int num)
{
NODE *newnode,*temp=root,*parent;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->info=num;
newnode->left=newnode->right=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
while(temp!=NULL)
{
parent=temp;
if(num<temp->info)
temp=temp->left;
else
temp=temp->right;
}
if(num<parent->info)
parent->left=newnode;

Prepared by: Trupti S. Gaikwad Page 3


SYBSc(CS) VCACS DS and AlgoII

else
parent->right=newnode;
return(root);
}

void preorder(NODE *root)


{
NODE *temp=root;
if(temp!=NULL)
{
printf("%d\t",temp->info);
preorder(temp->left);
preorder(temp->right);
}
}

void inorder(NODE *root)


{
NODE *temp=root;
if(temp!=NULL)
{

inorder(temp->left);
printf("\t%d",temp->info);
inorder(temp->right);
}
}

Prepared by: Trupti S. Gaikwad Page 4


SYBSc(CS) VCACS DS and AlgoII

void postorder(NODE *root)


{
NODE *temp=root;
if(temp!=NULL)
{

postorder(temp->left);
postorder(temp->right);
printf("\t%d",temp->info);
}
}

ass1a1.c

#include<stdio.h>
#include "btree.h"
void main()
{
NODE *root=NULL,*temp1;
int ch,num;
clrscr();

do
{
printf("\n1:Create BST");
printf("\n2:Insert");
printf("\n3;search");
printf("\n4:Inodrder");
printf("\n5:Preorder");
printf("\n6:Postorder");

Prepared by: Trupti S. Gaikwad Page 5


SYBSc(CS) VCACS DS and AlgoII

printf("\nenter your choice");


scanf("%d",&ch);
switch(ch)
{
case 1:root=createbst(root);
break;
case 2:printf("enter a value to insert");
scanf("%d",&num);
root=insertbst(root,num);
break;
case 3: printf("enter a value to search");
scanf("%d",&num);
temp1=search(root,num);
if(temp1==NULL)
printf("number not found");
else
printf("number is found");
break;
case 4: inorder(root);
break;
case 5:preorder(root);
break;
case 6:postorder(root);
break;
}
}while(ch!=7);
}

b) Write a program which uses binary search tree library and counts the
total nodes and total leaf nodes in the tree.

Prepared by: Trupti S. Gaikwad Page 6


DS and AlgoII VCACS,Pune S.Y.B.Sc(CS)

Assignment 3: Graph as Adjacency Matrix


Set A
a) Write a C program that accepts the vertices and edges of a
graph and stores it as an adjacency matrix. Display the
adjacency matrix.

#include<stdio.h>

void main()
{

int m[10][10],n,v,i,j;
clrscr();
printf("How many vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Is there edge between %d->%d (1/0):",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
printf("Adjacency matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
getch();
Prepared by; Trupti S. Gaikwad Page 1
SYBSc(CS) VCACS, Pune DS and algo II

Assignment 2: Binary Tree Applications


Set A
a) Write a C program which uses Binary search tree library and
displays nodes at each level, count of node at each level and total
levels in the tree.

bst.h

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20

typedef struct node


{
int info;
struct node *left,*right;
}NODE;

typedef struct
{
NODE *data[MAXSIZE];
int front,rear;
}QUEUE;

void initq(QUEUE *pq)


{
pq->front=pq->rear=-1;
}

void addq(QUEUE *pq,NODE *tnode)


{
pq->data[++pq->rear]=tnode;

Prepared By: Trupti S.Gaikwad Page 1


SYBSc(CS) VCACS, Pune DS and algo II

NODE* removeq(QUEUE *pq)


{
return pq->data[++pq->front];

int isempty(QUEUE *pq)


{
return(pq->front==pq->rear);
}
int isfull(QUEUE *pq)
{
return(pq->rear==MAXSIZE-1);
}

NODE * createbst(NODE *root)


{
NODE *newnode,*temp,*parent;
int i,n,num;
printf("how many nodes");
scanf("%d",&n);
printf("enter the %d elements:",n);
for(i=1;i<=n;i++)
{
newnode=(NODE *)malloc(sizeof(NODE));
scanf("%d",&num);
newnode->info=num;
newnode->left=newnode->right=NULL;
if(root==NULL)
{
Prepared By: Trupti S.Gaikwad Page 2
SYBSc(CS) VCACS, Pune DS and algo II

root=newnode;
continue;
}
temp=root;
while(temp!=NULL)
{
parent=temp;
if(num<temp->info)
temp=temp->left;
else
temp=temp->right;
}
if(num<parent->info)
parent->left=newnode;
else
parent->right=newnode;
}
return(root);
}

void levelorder(NODE *root)


{
int i,level=0;
NODE *temp,*marker=NULL;
QUEUE q;
int total=0,cnt=0;
initq(&q);
addq(&q,root);
addq(&q,marker);
printf("\n Level %d--->",level);
while(!isempty(&q))
Prepared By: Trupti S.Gaikwad Page 3
SYBSc(CS) VCACS, Pune DS and algo II

{
temp=removeq(&q) ;

if(temp==marker)
{
printf("\t cnt=%d",cnt);
cnt=0;
level++;
if(!isempty(&q))
{
addq(&q,marker);
printf("\nLevel %d--->",level);
}

}
else
{
printf("%d\t",temp->info);
if(temp->left)
addq(&q,temp->left);
if(temp->right)
addq(&q,temp->right);
total=total+1;
cnt=cnt+1;
}
}
printf("\ntotal nodes=%d",total);
}
ass2a1.c
#include<stdio.h>
#include<stdlib.h>
#include "bst.h"
void main()
Prepared By: Trupti S.Gaikwad Page 4
SYBSc(CS) VCACS, Pune DS and algo II

{
NODE *root=NULL;
clrscr();
root=createbst(root);
printf("\nLevel order traversal is");
levelorder(root);
getch();

Set B
a) Write a program to sort n randomly generated elements using
Heapsort method.
#include<stdio.h>

void display(int a[],int n)


{
int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

void heapify(int a[],int top,int last)


{
int j,temp,key;
key=a[top];
j=2*top+1;
if((j<last)&&(a[j]<a[j+1]))
j=j+1;
if((j<=last)&&(key<a[j]))
{
temp=a[top];
a[top]=a[j];
Prepared By: Trupti S.Gaikwad Page 5

You might also like