PROGRAM TO GENERATE BINARY TREE AND FIND ITS DEPTH.

#include<iostream.h>#include<fstream.h>
#include<ctype.h>
#include<conio.h>

int dup=0;
struct bintree
{
int data;
bintree *left,*right;
};
struct avltree
{
int data,diff;
bintree *left,*right;
};

fstream file;

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;
return;
}
else if(garb->data==(*base)->data)
{
cout<<"  "<<garb->data;
dup++;
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}

int depth(bintree *node)
{
int l,r;
if(node==NULL)
return -1;
l=1+depth(node->left);
r=1+depth(node->right);
return ((l>r)?l:r);
}
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;
int num;
file.open("datab2.txt",ios::in);
cout<<"\n DUPLICATE ELEMENTS ARE : \n";
while(file>>num)
{
garb=new bintree;
makenode(&garb,num);
makebintree(garb,&root);
}
cout<<"\n DEPTH="<<depth(root);
cout<<"\n NO.of dup="<<dup;
dump(&root);
file.close();
}

Advertisements

5 thoughts on “PROGRAM TO GENERATE BINARY TREE AND FIND ITS DEPTH.

  1. I simply want to mention I am beginner to blogs and certainly loved this blog site. Very likely I’m want to bookmark your blog post . You absolutely come with great articles and reviews. Cheers for revealing your webpage.

    Like

  2. Aw, this was an exceptionally nice post. Finding the time and actual effort to create a
    superb article… but what can I say… I put things off a lot and never manage to
    get anything done.

    Like

  3. Hi! This is my 1st comment here so I just wanted to give a quick shout out and say I really enjoy reading your posts. Can you recommend any other blogs/websites/forums that go over the same topics? Appreciate it!

    Like

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