0% found this document useful (0 votes)
41 views

DS Lab Record

The document describes a C program that implements various binary tree traversal algorithms - preorder, inorder, and postorder. It defines a node structure with left, right, and data pointers. Functions are included to insert nodes, perform the different traversals, and get new node objects. The main function allows the user to create a binary tree by entering nodes, and then choose which traversal to run on the tree. If the tree is empty, it prints a message. This provides an example of implementing and testing different binary tree traversal techniques.

Uploaded by

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

DS Lab Record

The document describes a C program that implements various binary tree traversal algorithms - preorder, inorder, and postorder. It defines a node structure with left, right, and data pointers. Functions are included to insert nodes, perform the different traversals, and get new node objects. The main function allows the user to create a binary tree by entering nodes, and then choose which traversal to run on the tree. If the tree is empty, it prints a message. This provides an example of implementing and testing different binary tree traversal techniques.

Uploaded by

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

EX. NO.

: 1
DATE

: 07/09/11

USAGE OF PRIMITIVE DATA STRUCTURES

AIM
To illustrate the usage of primitive data structures.
PROGRAM
//Primitive data stucture
//BIO DATA
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[20],fname[20],dep[15],adr[40];
clrscr();
printf("Enter Student name: ");
scanf("%s",&name);
printf("Enter Father name: ");
scanf("%s",&fname);
printf("Enter Age: ");
scanf("%d",&age);
printf("Enter Deparment: ");
scanf("%s",&dep);
printf("Enter Student Address: ");
scanf("%s",&adr);
printf("\n\tSTUDENT BIO DATA:\n ");
printf("\nName:
%s\nFather name:%s\nAge:
%s",name,fname,age,dep,adr);
getch();
}

%d\nDepartment: %s\nAddress:

OUTPUT
Enter Student name: MUNI
Enter Father name: SHELLAGANI
Enter Age: 21
Enter Deparment: MCA
Enter Student Address: 7/16_ALAVAKARAI_VADI,KILAKARAI.
STUDENT BIO DATA:
Name:
MUNI
Father name: SHELLAGANI
Age:
21
Department: MCA
Address: 7/16_ALAVAKARAI_VADI,KILAKARAI.

RESULT
The above program has been successfully completed.
2

EX. NO. : 2
DATE

: 07/09/11

USAGE OF ARRAY DATA STRUCTURE

AIM
To illustrate the usage of array data structure.
PROGRAM
//Array data stucture
//Ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[20],t;
clrscr();
printf("Enter ten elements:\n");
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
for(i=1;i<=10;i++)
for(j=1;j<=10;j++)
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("Ascending order:\n");
for(i=1;i<=10;i++)
printf("%d\n",a[i]);
getch();
}

OUTPUT

Enter ten elements:


10
8
9
7
1
2
6
4
5
3
Ascending order:
1
2
3
4
5
6
7
8
9
10

RESULT
4

The above program has been successfully completed.

EX. NO. : 3
DATE

: 21/09/11

STACK - ARRAY IMPLEMENTATION

AIM
To implement stack operations using array.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define size 10
void main()
{
int stack[size],top=-1,i,data,ch,m=1;
clrscr();
printf("Stack Operation\n");
while(m==1)
{
printf("1.PUSH\n2.POP\n3.VIEW\n4.EXIT\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(top==size-1)
printf("stack is full");
else
{
printf("Enter the element to PUSH\n");
scanf("%d",&data);
top=top+1;
stack[top]=data;
}
break;
case 2:
if(top==-1)
printf("Stack is Empty\n");
else
{
printf("Poped element id%d\n",stack[top]);
top--;
}
break;
case 3:
6

if(top==-1)
printf("Stack is empty\n");
else
{
printf("Stack contents:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
break;
default:
exit(0);
break;
}
printf("Do you want to continue press 1 else press any key\n");
scanf("%d",&m);
}
}

OUTPUT
Stack Operation
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
1
Enter the element to PUSH
6
Do you want to continue press 1 else press any key
1
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
1
Enter the element to PUSH
8
Do you want to continue press 1 else press any key
1
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
3
Stack contents:
8
6
Do you want to continue press 1 else press any key
1
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
2
Poped element id8
Do you want to continue press 1 else press any key
1
1.PUSH
8

2.POP
3.VIEW
4.EXIT
Enter your choice
3
Stack contents:
6
Do you want to continue press 1 else press any key
0

RESULT
9

The above program has been successfully completed.


EX. NO. : 4
DATE

: 28/09/11

STACK - LINKED LIST IMPLEMENTATION

AIM
To implement stack operations using linked list.
PROGRAM
// Stack linked list implementation
#include<stdio.h>
#define null 0
struct node
{
int data;
struct node *link;
};
struct node *top=null,*temp;
void main()
{
int ch,data,m=1;
clrscr();
printf("Stack Operation\n");
while(m==1)
{
printf("1.PUSH\n2.POP\n3.VIEW\n4.eEXIT\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the element to PUSH\n");
scanf("%d",&data);
temp->data=data;
temp->link=top;
top=temp;
break;
case 2:
if(top!=null)
{
printf("Poped element is:%d\n",top->data);
10

top=top->link;
}
else
printf("Stack is Empty\n");
break;
case 3:
temp=top;
if(temp==null)
printf("Stack is empty\n");
else
{
printf("Stack contents:\n");
while(temp!=null)
{
printf("%d\n",temp->data);
temp=temp->link;
}
}
break;
default:
exit(0);
break;
}
printf("Do you want to continue press 1 else press any key\n");
scanf("%d",&m);
}
}

11

OUTPUT
Stack Operation
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
1
Enter the element to PUSH
6
Do you want to continue press 1 else press any key
1
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
1
Enter the element to PUSH
8
Do you want to continue press 1 else press any key
1
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
3
Stack contents:
8
6
Do you want to continue press 1 else press any key
1
1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
2
Poped element id8
Do you want to continue press 1 else press any key
1
12

1.PUSH
2.POP
3.VIEW
4.EXIT
Enter your choice
3
Stack contents:
6
Do you want to continue press 1 else press any key
0

13

RESULT
The above program has been successfully completed.
EX. NO. : 5
DATE

: 30/09/11

QUEUE - ARRAY IMPLEMENTATION

AIM
To implement Queue operations using array.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define size 10
void main()
{
int queue[size],ft=-1,rr=-1,i,ch,m=1;
clrscr();
printf("Queue Operation\n\n");
while(m==1)
{
printf("1.ENQUEUE\n2.DEQUEUE\n3.VIEW CONTENTS\n4.EXIT\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rr==size-1)
printf("Queue Already full");
else
{
printf("Enter the element to Insert\n");
if(ft==-1 && rr==-1)
ft++;
rr++;
scanf("%d",&queue[rr]);
}
break;
case 2:
if(rr==-1)
printf("Queue is Empty\n");
else
{
14

printf("Deleted element is: %d\n",queue[ft]);


for(i=0;i<size;i++)
queue[i]=queue[i+1];
rr--;
}
break;
case 3:
printf("Queue contents are:\n");
for(i=ft;i<=rr;i++)
printf(".%d.",queue[i]);
printf("\n");
break;
default:
exit(0);
break;
}
printf("Do you want to continue press 1 else press any key\n");
scanf("%d",&m);
}
}

15

OUTPUT
Queue Operation
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
1
Enter the element to Insert
5
Do you want to continue press 1 else press any key
1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
1
Enter the element to Insert
6
Do you want to continue press 1 else press any key
1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
3
Queue contents are:
.5..6.
Do you want to continue press 1 else press any key
1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
2
Deleted element is: 5
Do you want to continue press 1 else press any key
16

1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
3
Queue contents are:
..6.
Do you want to continue press 1 else press any key
0

17

RESULT
The above program has been successfully completed.
EX. NO. : 6
DATE

: 12/10/11

QUEUE - LINKED LIST IMPLEMENTATION

AIM
To implement Queue operations using linked list.
PROGRAM
//Queue Linked List implementation
#include<stdio.h>
#include<conio.h>
#define null 0
struct node
{
int data;
struct node *link;
}*front=null,*rear=null,*temp;
void main()
{
int data,ch,m=1;
clrscr();
printf("Queue Operation\n\n");
while(m==1)
{
printf("1.ENQUEUE\n2.DEQUEUE\n3.VIEW CONTENTS\n4.EXIT\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the element to Insert\n");
scanf("%d",&data);
temp->data=data;
temp->link=null;
if(front==null)
front=temp;
18

else
rear->link=temp;
rear=temp;
break;
case 2:
if(front!=null)
{
temp=front;
data=front->data;
front=front->link;
free(temp);
printf("Deleted element is:%d\n",data);
}
else
printf("Queue is Empty\n");
break;
case 3:
temp=front;
if(front==null)
printf("Queue is Empty\n");
else
{
while(temp!=null)
{
printf(".%d.",temp->data);
temp=temp->link;
}
printf("\n");
}
break;
default:
exit(0);
break;
}
printf("Do you want to continue press 1 else press any key\n");
scanf("%d",&m);
}
}

19

OUTPUT
Queue Operation
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
1
Enter the element to Insert
5
Do you want to continue press 1 else press any key
1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
1
Enter the element to Insert
6
Do you want to continue press 1 else press any key
1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
3
Queue contents are:
.5..6.
Do you want to continue press 1 else press any key
1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
2
Deleted element is: 5
20

Do you want to continue press 1 else press any key


1
1.ENQUEUE
2.DEQUEUE
3.VIEW CONTENTS
4.EXIT
Enter your choice
3
Queue contents are:
..6.
Do you want to continue press 1 else press any key
0

21

RESULT
The above program has been successfully completed.
EX. NO. : 7
DATE

: 19/10/11

BINARY TREE TRAVERSALS

AIM
To traverse the binary tree in inorder , preorder , postorder methods.
PROGRAM
#include<stdio.h>
struct bt
{
int data;
struct bt *left,*right;
};
typedef struct bt node;
void insert(node *,node *);
void inorder(node *);
void preorder (node *);
void postorder(node *);
node *get_node();
void main()
{
int ch,ans;
node *new,*root;
root=NULL;
clrscr();
while(1)
{
printf("\n1.Create\n2.Preorder\n3.Inorder\n4.Postorder\n5.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element 0 to stop\n");
do
{
22

new=get_node();
scanf("%d",&new->data);
ans=new->data;
if(ans!=0)
if(root==NULL)
root=new;
else
insert(root,new);
}
while(ans!=0);
break;
case 2:
if(root==NULL)
printf("\nNo elements in the tree\n");
else
{
printf("Preorder Traversal\n");
preorder(root);
}
break;
case 3:
if(root==NULL)
printf("\n No elements int the tree\n");
else
{
printf("inorder Traversal\n");
inorder(root);
}
break;
case 4:
if(root==NULL)
printf("\n No elements int the tree\n");
else
{
printf("Post order Traversal\n");
postorder(root);
}
break;
default:
exit(0);
break;
}
}
}
node *get_node()
{
23

node *temp;
temp=(node*)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
void insert(node *root,node *new)
{
if(new->data<root->data)
{
if(root->left==NULL)
root->left=new;
else
insert(root->left,new);
}
if(new->data>root->data)
{
if(root->right==NULL)
root->right=new;
else
insert(root->right,new);
}
}
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("%d->",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
printf("%d->",temp->data);
inorder(temp->right);
}
}
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->left);
24

postorder(temp->right);
printf("%d->",temp->data);
}
}
OUTPUT
1.Create
2.Preorder
3.Inorder
4.Postorder
5.Exit
1
Enter the element 0 to stop
5
4
3
9
21
6
0
1.Create
2.Preorder
3.Inorder
4.Postorder
5.Exit
2
Preorder Traversal
5->4->3->9->6->21->
1.Create
2.Preorder
3.Inorder
4.Postorder
5.Exit
3
inorder Traversal
3->4->5->6->9->21->
25

1.Create
2.Preorder
3.Inorder
4.Postorder
5.Exit
4
Post order Traversal
3->4->6->21->9->5->
1.Create
2.Preorder
3.Inorder
4.Postorder
5.Exit

26

RESULT
The above program has been successfully completed.
EX. NO. : 8
DATE

MERGE SORT

: 29/10/11

AIM
To sort the given numbers using merge sort method.
PROGRAM
//MERGE SORT
#include<stdio.h>
int x[10],n,i,j,k,l1,l2,size,u1,u2;
void main()
{
void mergesort(int x[],int n);
clrscr();
printf("Enter the no of terms:\n");
scanf("%d",&n);
printf("Enter the terms:\n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
mergesort(x,n);
printf("\nsorted Order\n");
for(i=0;i<n;i++)
printf("%d\n",x[i]);
getch();
}
void mergesort(int x[],int n)
{
int aux[10];
size=1;
while(size<n)
{
l1=0;
k=0;
while((l1+size)<n)
{
l2=l1+size;
27

u1=l2-1;
u2=(l2+size-1<n)?l2+size-1:n-1;
for(i=l1,j=l2;i<=u1&&j<=u2;k++)
if(x[i]<=x[j])
aux[k]=x[i++];
else
aux[k]=x[j++];
for( ;i<=u1;k++)
aux[k]=x[i++];
for( ;j<=u2;k++)
aux[k]=x[j++];
l1=u2+1;
}
for(i=l1;k<n;i++)
aux[k++]= x[i];
for(i=0;i<n;i++)
x[i]=aux[i];
size*=2;
}
}

28

OUTPUT
Enter the no of terms:
3
Enter the terms:
5
2
9
sorted Order
2
5
9

29

RESULT
The above program has been successfully completed.
EX. NO. : 09
DATE

INSERTION SORT

: 02/11/11

AIM
To sort the given numbers using insertion sort method.
PROGRAM
//Insetion sort
#include<stdio.h>
int x[10],k,y,i,n;
void main()
{
void insertionsort(int x[],int n);
clrscr();
printf("enter the no of terms to sort:\n");
scanf("%d",&n);
printf("Enter the terms:\n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
insertionsort(x,n);
printf("Sorted order:\n");
for(i=0;i<n;i++)
printf("%d\n",x[i]);
getch();
}
void insertionsort(int x[],int n)
{
for(k=1;k<n;k++)
{
y=x[k];
for(i=k-1;i>=0&&y<x[i];i--)
x[i+1]=x[i];
x[i+1]=y;
}
}

30

OUTPUT
Enter the no of terms:
3
Enter the terms:
5
2
9
sorted Order
2
5
9

31

RESULT
The above program has been successfully completed.
EX. NO. : 10
DATE

: 09/11/11

DEPTH FIRST SEARCH

]AIM
To traverse the graph using depth-first search method.
PROGRAM
/*DFS*/
#include<stdio.h>
#include<conio.h>
int adj[10][10],visited[10];
int source,n,i,j;
void main()
{
void dfs(int);
clrscr();
printf("Enter the no of nodes in the graph;\n");
scanf("%d",&n);
printf("\nEnter the Adjacency matrix::\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the Source\n");
scanf("%d",&source);
printf("\nThe nodes are visited in the DFS Order is\n");
dfs(source);
getch();
}
void dfs(int source)
{
int i;
32

visited[source]=1;
printf("%d",source);
for(i=1;i<=n;i++)
if(adj[source][i]&&!visited[i])
dfs(i);
}
OUTPUT
Enter the no of nodes in the graph;
5
Enter the Adjacency matrix::
01110
00001
00010
01010
00000
Enter the Source
1
The nodes are visited in the DFS Order is
12534

33

RESULT
The above program has been successfully completed.
EX. NO. : 11
DATE

: 16/11/11

BREADTH FIRST SEARCH

AIM
To traverse the graph using breadth-first search method.
PROGRAM
#include<stdio.h>
int adj[10][10],visited[10];
int i,j,n,source,queue[10],front;
int rear,root;
void main()
{
printf("enter the no of nodes in the graph;\n");
scanf("%d",&n);
printf("\nEnter the Adjacency matrix::\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the Source\n");
scanf("%d",&source);
front=rear=0;
visited[source]=1;
queue[rear++]=source;
printf("\nThe node visited inthe BFS Order is %d",source);
while(front!=rear)
{
root =queue[front];
for(i=1;i<=n;i++)
{
if(adj[root][i]==1 && visited[i]==0)
{
34

visited[i]=1;
queue[rear++]=i;
printf("%d",i);
}
}
front++;
}
getch();
}
OUTPUT
enter the no of nodes in the graph;
5
Enter the Adjacency matrix::
01110
00001
00010
01010
00000
Enter the Source
1
The node visited inthe BFS Order is
12345

35

RESULT
The above program has been successfully completed.
EX. NO. : 12
DATE

: 30/11/11

WARSHALLS ALGORITHM

AIM
To find the transitive closure for the given graph using Warshalls algorithm.
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int adj[5][5],path[5][5],i,j,k;
clrscr();
printf("Enter the Adjacency Matrix\n");
for(i=0;i<5;++i)
for(j=0;j<5;++j)
scanf("%d",&adj[i][j]);
for(i=0;i<5;++i)
for(j=0;j<5;++j)
path[i][j]=adj[i][j];
for(k=0;k<5;++k)
for(i=0;i<5;++i)
for(j=0;j<5;++j)
if(path[i][k]==1)
path[i][j]=path[i][j]||path[k][j];
printf("\nTransitive Closure using warshall's Algorithm:\n");
for(i=0;i<5;++i)
{
36

for(j=0;j<5;++j)
printf("%d",path[i][j]);
printf("\n");
}
getch();
}

OUTPUT
Enter the Adjacency Matrix
00110
00100
00011
00001
00010
Transitive Closure using warshall's Algorithm:
00111
00111
00011
00011
00011

37

RESULT
The above program has been successfully completed.
EX. NO. : 13
DATE

: 03/12/11

DIJKSTRAS ALGORITHM

AIM
To find the shortest path from a given source using Dijkstras algorithm.
PROGRAM
/*DIJSTRAS SHORTEST ALGORITHM*/
#include<stdio.h>
#define infinity 999
#define member 1
#define nonmember 0
void main()
{
int weight[4][4],s,t,*pd,precede[4];
int i,j,k,distance[4],perm[4],current,dc,small_dist,new_dist;
clrscr();
printf("enter the weight matrix\n");
for(i=0;i<4;++i)
for(j=0;j<4;++j)
scanf("%d",&weight[i][j]);
printf("enter the value of source & target \n\n");
scanf("%d%d",&s,&t);
//initialization
for (i=0;i<4;++i)
{
perm[i]=nonmember;
distance[i]=infinity;
}
perm[s]=member;
distance[s]=0;
current=s;
38

while(current!=t)
{
small_dist=infinity;
dc=distance[current];
for(i=0;i<4;++i)
if(perm[i]==nonmember)
{
new_dist=dc+weight[current][i];
if(new_dist<distance[i])
{
distance[i]=new_dist;
precede[i]=current;
}
if(distance[i]<small_dist)
{
small_dist=distance[i];
k=i;
}
printf("smallest distance of node %d to node %d is=%d\n",s,i,distance[i]);
}
current=k;
perm[current]=member;
}
*pd=distance[t];
printf("minimum distance from node %d to node %d is =%d",s,t,*pd);
getch();
}

39

OUTPUT
enter the weight matrix
999
2
1
4
999
999
3
999
999
999
999
999
999
999
6
999
enter the value of source & target
0
3
smallest distance of node 0 to node 1 is=2
smallest distance of node 0 to node 2 is=1
smallest distance of node 0 to node 3 is=4
smallest distance of node 0 to node 1 is=2
smallest distance of node 0 to node 3 is=4
smallest distance of node 0 to node 3 is=4
minimum distance from node 0 to node 3 is =4

40

RESULT
The above program has been successfully completed.
EX. NO. : 14
DATE

: 07/12/11

HUFFMANS ALGORITHM

AIM
To perform the encoding process using Huffmans algorithm.
PROGRAM
/*************************/
/*************************/
// HFMANN.C
/*************************/
/*************************/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include"input.h"
#define MAXBITS 32
#define MAXNODES 512
#define MAXSYMBS 256
int hmin(void);
void hinsert(int,int);
struct codetype{
int bits[MAXBITS];
int startpos;
};
struct nodetype{
int freq;
int father;
int isleft;
};
typedef struct hlist{
41

int pos;
int hfreq;
struct hlist *next;
}hnode;
hnode *hroot=NULL,*traversal;
extern struct list a[256];
extern int n;
int hmin()
{
int p;
p = hroot->pos;
traversal=hroot;
hroot = traversal->next;
free(traversal);
return(p);
}
void hinsert(int p,int freq)
{
hnode* new1=(hnode *)malloc(sizeof(hnode));
new1->pos = p;
new1->hfreq = freq;
traversal = hroot;
if(hroot == NULL)
{
hroot = new1;
hroot->next = NULL;
return;
}
if(hroot->next == NULL)
{
if(hroot->hfreq>new1->hfreq)
{
new1->next = hroot;
hroot =new1;
traversal->next =NULL;
return;
}
else
{
hroot->next =new1;
new1->next =NULL;
return;
}
}
if(hroot->hfreq>=new1->hfreq)
{
42

new1->next =hroot;
hroot = new1;
return;
}
while(traversal->next->hfreq<new1->hfreq)
{
traversal=traversal->next;
if(traversal->next==NULL)
break;
}
if(traversal->next->hfreq>=new1->hfreq)
{
new1->next = traversal->next;
traversal->next = new1;
return;
}
new1->next = NULL;
traversal->next = new1;
}
int main(){
struct codetype cd,code[MAXSYMBS];
struct nodetype node[MAXNODES];
int i,k,p,p1,p2,root;
char symb,alph[MAXSYMBS];
clrscr();
for(i=0;i<MAXSYMBS;i++)
alph[i]=' ';
//scanf("%d",&n);
input();
for(i=0;i<n;i++){
flushall();
// scanf("%c %d",&symb,&node[i].freq);
symb = a[i].alph;
node[i].freq = a[i].freq;
hinsert(i,node[i].freq);
alph[i]=symb;
}
for(p=n;p<(2*n-1);p++){
p1 =hmin();
43

p2 =hmin();
node[p1].father = p;
node[p1].isleft = 1;
node[p2].father = p;
node[p2].isleft = 0;
node[p].freq =node[p1].freq+node[p2].freq;
hinsert(p,node[p].freq);
}
root = hmin();
for(i=0;i<n;i++){
cd.startpos = MAXBITS;
p=i;
while(p!=root){
--cd.startpos ;
if(node[p].isleft)
cd.bits[cd.startpos] =0;
else
cd.bits[cd.startpos] =1;
p =node[p].father;
}
for(k=cd.startpos;k<MAXBITS;k++)
code[i].bits[k]=cd.bits[k];
code[i].startpos =cd.startpos;
}
for(i=0;i<n;i++){
printf("\n%c %d",alph[i],node[i].freq);
for(k=code[i].startpos;k<MAXBITS;k++)
printf(" %d",code[i].bits[k]);
printf("\n");
}
getch();
}

/*************************/
/*************************/
// INPUT.H
/*************************/
/*************************/

44

#include<stdio.h>
#include<conio.h>
struct list{
char alph;
int freq;
};
struct list a[256];
int n;
void input()
{
FILE *fin,*fout;
char *filein,*fileout,ch;
int i,k,f;
printf("enter the filename of from which the data is to be read::\n");
scanf("%s",filein);
fin=fopen(filein,"r");
for(i=0,n=0;i<256;i++)
{ f=0;k=0;
while((ch=fgetc(fin))!=EOF)
{
if(ch==i)
{ f=1;
a[n].alph=ch;
k++;
}
}
if(f==1){
a[n].freq =k;
n++;
}
rewind(fin);
}
fclose(fin);
getch();
}
//message
AAAAABBBBACCCCCDDDFGGGGHI
45

OUTPUT
enter the filename of from which the data is to be read::
message.txt
A 610
B 4111
C 500
D 3011
F 10100
G 4110
H 101011
I 101010

46

RESULT
The above program has been successfully completed.

47

You might also like