PROGRAM TO GENERATE 20000 RANDOM KEYS AND WRITE TO FILE KEYS.TXT

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};

void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}

void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
file<<garb->data<<" ";
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("keys.txt",ios::out);
while(total<20000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
dump(&root);
file.close();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s