Posts Tagged depth
GENERATING AN AVL TREE OF 10000 RANDOM NUMBERS AND REPORT ITS DEPTH IN C++.
Posted by admin in C++ Examples on July 2nd, 2009
#include <iostream.h> //INCLUDING REQUIRED LIBRARIES
#include <stdio.h>
#include<stdlib.h>
struct node //DECLARATION OF THE NODES OF THE AVL TREE
{
int data; //TO STORE THE NUMBER
int leftdepth,rightdepth; //TO KEEP TRACK OF THE LEFT AND RIGHT DEPTH
struct node *father;
struct node *left,*right; //POINTER TO THE LEFT AND RIGHT CHILD OF THAT NODE
};
class tree //DECLARING THE CLASS TREE
{
private :
struct node * root;
public :
tree() //ZERO-ARGUMENT CONSTRUCTOR
{
root=NULL; //MAKING ROOT NULL
}
void input ()
{
int temp;
FILE * ifp=fopen("int.txt","r"); //OPENING THE FILE CONTAINING 10000 RANDOM INTEGERS
while(fscanf(ifp,"%d",&temp)!=EOF) //TILL THE END OF FILE
{
add (temp); //CALLING FUNCTION TO ADD THE NUMBER TO THE AVL TREE
}
}
struct node * create (int y) //FUNCTION TO CREATE A NODE CONTAINING THE NUMBER
{
struct node * temp=new node ;
temp->data=y; //PUTTING THE DATA IN THE NEW NODE
temp->father=NULL; //MAKING FATHER NULL
temp->left=temp->right=NULL; //MAKING RIGHT AND LEFT CHILD NULL
temp->leftdepth=temp->rightdepth=0; //MAKING LEFT AND RIGHT depth EQUAL TO ZERO
return temp; //RETURNING THE NODE
}
void add (int a) //FUNCTION TO PUT THE NO IN THE NODE AND
//THEN ATTACH IT TO THE AVL TREE AT THE PROPER PLACE
{
struct node * curr= search(a,root),*x,*y,*z;
//CURR IS A POINTER TO THE NODE WHERE THE NEW NODE WILL BE ATTACHED
x=y=z=NULL;
if (curr==NULL) root=create (a); //IF NO ELEMENT IN THE TREE
else if (curr->data!=a)
//CREATING A NODE IF THE NO IS NOT EQUAL TO THE NO PRESENT IN THE CURRENT NODE AND ATTACHING IT TO THE BINARY TREE AT APPROPRIATE PLACE
{
x=create(a);
if (curr->data>a) curr->left=x;
else curr->right= x;
x->father=curr; //MAKING CURRENT NODE TO BE FATHER OF NEW NODE
while(x!=NULL)
{
z=y;
y=x;
x=x->father;
if (x!=NULL )
{
updatedepth(x);
if (check(x)) //***CALLING FUNCTION TO SATISFY THE AVL BALANCING CONDITION
rearrange(x,y,z);
}
}
}
else cout<<a<<"\t";
}
int maxdepth(struct node * p) //RETURNING THE MAXIMUM DEPTH AT EACH NODE
{
if (p==NULL)
return -1;
if (p->leftdepth > p->rightdepth)
return p->leftdepth;
return p->rightdepth;
}
int mod (int a,int b) //TO ADJUDGE WHETHER A SINGLE OR DOUBLE ROTATION IS REQUIRED
{
if(a>b)
return a-b;
else
return b-a;
}
int check(node * a) //***FUNCTION TO CHECK IF THE AVL BALANCING CONDITION IS SATISFIED
{
if(mod (a->leftdepth,a->rightdepth)<2)
return 0;
return 1;
}
void rearrange(struct node * a ,struct node * b,struct node * c)
//FUNCTION TO REARRANGE THE NODES TO MAINTAIN THE AVL BALANCE
{
if( ((a->data - b->data) * (b->data -c->data)) > 0 )
singlerotation (a,b);
else
doublerotation(a,b,c);
}
//FUNCTION TO GIVE NODES SINGLE ROTATION SO THAT THE BALANCING CONDITION OF THE AVL TREE IS SATISFIED
void singlerotation (struct node * a,struct node * b)
{ //FUNCTION TO DO SINGLEROTATION AT THE NODES
if (a->data < b->data)
{
a->right=b->left;
if (b->left!=NULL) b->left->father=a;
b->left=a;
}
else
{
a->left=b->right;
if (b->right!=NULL)b->right->father=a;
b->right=a;
}
if ((b->father=a->father)!=NULL)
{
if( a->father->right==a) a->father->right=b;
else a->father->left=b;
}
else root=b;
a->father=b;
updatedepth (a);
updatedepth (b);
}
//FUNCTION TO GIVE NODES DOUBLE ROTATION SO THAT THE BALANCING CONDITION OF THE AVL TREE IS SATISFIED
void doublerotation(struct node * a,struct node * b,struct node * c)
{ //FUNCTION TO DOUBLE ROTATION OF THE NODES
singlerotation(b,c);
singlerotation(a,c);
updatedepth(a);
updatedepth(b);
updatedepth(c);
}
void updatedepth(struct node * p) //UPDATING THE DEPTH OF EACH NODE WHEN ANOTHER NODE IS ADDED TO IT
{
p->leftdepth=maxdepth(p->left)+1;
p->rightdepth=maxdepth(p->right)+1;
}
struct node * search (int x,struct node * r) //SEARCHING THE TREE FOR THE APPROPRIATE PLACE TO ATTACH
{ //THE INCOMING NODE CONTAINING A NEW NUMBER
while (1)
{
if (r==NULL) //IF EMPTY TREE
return NULL;
else
{
if (x==r->data) //IF NEW NO. IS SAME AS NUMBER IN THE ROOT
break;
else if(x>r->data) //IF NEW NO. IS GREATER THAN THE NO. IN THE CURRENT NODE
{
if(r->right==NULL) break ;
else return search (x,r->right);//RECURSIVE CALLS TO FUNCTION SEARCH
}
else //IF NEW NO IS LESS THAT THE THE NO. IN THE CURRENT NODE
{
if (r->left==NULL) break;
else return search(x,r->left); //RECURSIVE CALLS TO FUNCTION SEARCH
}
}
}
return r; //RETURNING THE POINTER TO THE REQUIRED NODE WHERE THE NEW NODE IS TO BE ATTACHED
}
void output() //FUNCTION TO DISPLAY THE DEPTH
{
cout<<"\n\t**********************************************************";
cout<<"\n\t\tDEPTH OF AVL TREE FORMED IS\t "<<maxdepth(root)<<endl;
}
};
void main () //MAIN FUNCTION
{
tree t; //CREATING AN OBJECT OF TYPE TREE
int i;
FILE *ifp;
ifp=fopen("int.txt","w"); //OPENING FILE TO WRITE 10000 INTEGERS
srand(17); //SETS THE RANDOM NUMBER SEED FOR THE RAND FUNCTION
for(i=0;i<10000;i++) //LOOP TO GENERATE 10000 INTEGERS AND WRITE INTO THE TEXT FILE
{
fprintf(ifp,"%d\n",rand()); //WRITING RANDOM NUMBERS TO THE FILE
};
cout<<"\t***THE FOLLOWING NUMBERS ARE REPEATED IN THE AVL TREE***\n\n";
t.input(); //CALLING FUNCTION TO READ RANDON NOS.,GENERATE AVL TREE AND KEEP TRACK OF ITS DEPTH
t.output(); //CALLING FUNCTION TO DISPLAY THE DEPTH OF THE AVL TREE
}//END OF PROGRAM
Program to find depth of a tree in C.
Posted by admin in C Examples on July 2nd, 2009
#include<stdio.h>
struct node
{
int info,i;
struct node* left;
struct node* right;
};
struct node* maketree(int x,int count);
void inorder(struct node*);
int rec(struct node* ,int ,int ,int );
int depth(struct node*,int ,int );
struct node* p;
int t=0,k=0;
main()
{
FILE *fp;
struct node* q;
int x,d;
fp=fopen("y.c","r");
if(fp==NULL)
{
puts("error in opening file y.c");
exit(1);
}
fscanf(fp,"%d",&x);
p= maketree(x,0);
q=p;
while((fscanf(fp,"%d",&x))>=0)
{
t=0;k=0;
rec(p,x,t,k);
p=q;
}
fclose(fp);
inorder(p);
t=0;k=0;
d=depth(p,t,k);
printf("\n depth is %d",d);
}
void inorder(struct node* p)
{
if(p==NULL)return;
inorder(p->left);
printf("\n%d",p->info);
inorder(p->right);
}
int rec(struct node* p,int x,int t,int k)
{
if((p->info)>x)
{
if(p->left==NULL){
t=++t;
setleft(p,x,t);
return(1);}
++t;
return(rec(p->left,x,t,k));
}
if((p->info)<x)
{
if(p->right==NULL){
k=++k;
setright(p,x,k);
return(1);}
++k;
return(rec(p->right,x,t,k));
}
if(p->info==x)
return(1);
}
struct node* maketree(int x,int count)
{
struct node* p;
p=(struct node*)malloc(sizeof(struct node));
p->info=x;
p->i=count;
p->left=NULL;
p->right=NULL;
return(p);
}
int setleft(struct node* p,int x,int t)
{
if(p==NULL)
puts("error");
if((p->left)!=NULL)
puts("error");
else
(p->left)=maketree(x,t);
return(1);
}
int setright(struct node* p,int x,int t)
{
if(p==NULL)
puts("error");
if((p->right)!=NULL)
puts("error");
else
(p->right)=maketree(x,t);
return(1);
}
int depth(struct node* p,int t,int k)
{
int r,s;
if(((p->left)==NULL)&&((p->right)==NULL)){
return(t+k);}
if(p->left==NULL){
t=t;++k;
return(depth(p->right,t,k));}
if(p->right==NULL){
k=k;++t;
return(depth(p->left,t,k));}
if(((p->left)!=NULL)&&((p->right)!=NULL)){
++t;
r= depth(p->left,t,k);
++k;--t;
s= depth(p->right,t,k);
if(r>=s)
return(r);
else
return(s);
}
}
PROGRAM TO GENERATE BINARY TREE AND FIND ITS DEPTH.
Posted by admin in C++ Examples on June 6th, 2009
#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();
}
PROGRAM TO GENERATE AVL TREE AND FIND ITS DEPTH.
Posted by admin in C++ Examples on June 6th, 2009
tree t;#include <iostream.h> //HEADER FILES
#include <fstream.h>
#include <stdio.h>
struct node //STRUCTURE FOR TREE NODE
{
int data;
int leftheight,rightheight;
struct node *father;
struct node *left,*right;
};
fstream fp;
int dup=0;
class tree //CLASS TREE
{
public :
struct node * root;
tree()
{
root=NULL;
}
void input () //FUNCTIONS DEFINED INLINE
{
int temp;
fp.open("datab2.txt",ios::in);
while(fp>>temp)
add (temp);
}
struct node * create (int y)
{
struct node * temp=new node ;
temp->data=y;
temp->father=NULL;
temp->left=temp->right=NULL;
temp->leftheight=temp->rightheight=0;
return temp;
}
void add (int a)
{
struct node *x,*y,*z;
struct node * temp= search(a,root);
x=y=z=NULL;
if (temp==NULL)
root=create (a);
else if (temp->data!=a)
{
x=create(a);
if (temp->data>a)
temp->left=x;
else
temp->right= x;
x->father=temp;
while(x!=NULL)
{
z=y;
y=x;
x=x->father;
if (x!=NULL )
{
update(x);
if (check(x))
rearrange(x,y,z);
}
}
}
else
{
cout<<a<<" ";
dup++;
}
}
int maxheight(struct node * p)
{
if (p==NULL)
return -1;
if (p->leftheight > p->rightheight)
return p->leftheight;
return p->rightheight;
}
int mod (int a,int b)
{
if(a>b)
return a-b;
else
return b-a;
}
int check(node * a)
{
if(mod (a->leftheight,a->rightheight)<2)
return 0;
return 1;
}
void rearrange(struct node * a ,struct node * b,struct node * c)
{
if((a->data > b->data)&&(b->data > c->data)||(a->data<b->data)&&(b->data<c->data))
singlerotation (a,b);
else
doublerotation(a,b,c);
}
void singlerotation (struct node * a,struct node * b)
{
if (a->data < b->data)
{
a->right=b->left;
if (b->left!=NULL)
b->left->father=a;
b->left=a;
}
else
{
a->left=b->right;
if (b->right!=NULL)
b->right->father=a;
b->right=a;
}
if ((b->father=a->father)!=NULL)
{
if( a->father->right==a)
a->father->right=b;
else
a->father->left=b;
}
else
root=b;
a->father=b;
update (a);
update (b);
}
void doublerotation(struct node * a,struct node * b,struct node * c)
{
singlerotation(b,c);
singlerotation(a,c);
update(a);
update(b);
update(c);
}
void update(struct node * p)
{
p->leftheight=maxheight(p->left)+1;
p->rightheight=maxheight(p->right)+1;
}
struct node * search (int x,struct node * r)
{
while (1)
{
if (r==NULL)
return NULL;
else
{
if (x==r->data)
break;
else if(x>r->data)
{
if(r->right==NULL)
break ;
else
return search (x,r->right);
}
else
{
if (r->left==NULL)
break;
else
return search(x,r->left);
}
}
}
return r;
}
void printnode (struct node * y)
{
cout<<y->data<<" "<<y->leftheight <<" "<<y->rightheight<<endl;
}
void output()
{
cout<<"\n No of duplicates="<<dup;
cout<<"\n Depth of AVL tree="<<maxheight(root)<<endl;
}
void dump(struct node **base) //TO FREE ALL ALLOCATED MEMORY
{
if(*base==NULL)
return;
if((*base)->left==NULL && (*base)->right==NULL)
{
delete(*base);
*base=NULL;
}
else
{
dump(&((*base)->left));
dump(&((*base)->right));
dump(base);
}
}
};
void main ()
{
t.input();
t.output();
t.dump(&(t.root));
}


Recent Comments