#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class tree
{
private:
struct node
{
int data;
struct node *left;
struct node *right;
};
node *start;
public:
tree(void)
{
if(!(start=new node))
{
cout<<"Insufficient memory";
getch();
exit(1);
}
start->data=0;
start->left=NULL;
start->right=NULL;
}
int insert(int);
void display(void);
void display_preorder(node *temp);
void display_inorder(node *temp);
void display_postorder(node *temp);
int count(void);
int remove(int);
int calc_depth(node *temp,int);
int depth(void);
node* search(int);
int empty(void);
~tree()
{
delete start;
cout<<endl<<"List deleted";
}
};
int tree::insert(int x)
{
node *temp=start->left;
if(empty())
{
if(!(start->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
cout<<"Empty "<<x;
start->data=++start->data;
start->left->data=x;
start->left->left=NULL;
start->left->right=NULL;
return 1;
}
}
while(1)
{
if(x<=temp->data)
{
if(temp->left==NULL)
{
if(!(temp->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
cout<<endl<<"Left node "<<x;
return 1;
}
}
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
if(!(temp->right=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
cout<<endl<<"Right node "<<x;
return 1;
}
}
else
temp=temp->right;
}
}
}
void tree::display(void)
{
cout<<endl<<"No. of nodes : "<<start->data;
cout<<endl<<"Preorder : ";
display_preorder(start->left);
cout<<endl<<"ineorder : ";
display_inorder(start->left);
cout<<endl<<"Postorder : ";
display_postorder(start->left);
return;
}
void tree::display_preorder(node *temp)
{
if(temp!=NULL)
{
cout<<"\t"<<temp->data;
display_preorder(temp->left);
display_preorder(temp->right);
}
return;
}
void tree::display_inorder(node *temp)
{
if(temp!=NULL)
{
display_inorder(temp->left);
cout<<"\t"<<temp->data;
display_inorder(temp->right);
}
return;
}
void tree::display_postorder(node *temp)
{
if(temp!=NULL)
{
display_postorder(temp->left);
display_postorder(temp->right);
cout<<"\t"<<temp->data;
}
return;
}
int tree::remove(int x)
{
node *toremove,*temp;
if((toremove=search(x))==NULL)
{
cout<<endl<<"Not Found";
return -1;
}
start->data--;
if(toremove->left->data==x)
{
if(toremove->left->right==NULL && toremove->left->left!=NULL)
{
toremove->left=toremove->left->left;
return 1;
}
if(toremove->left->right==NULL && toremove->left->left==NULL)
{
toremove->left=NULL;
return 1;
}
else
{
temp=toremove->right;
for(;temp->left->left!=NULL;temp=temp->left);
}
}
else
{
if(toremove->right->right==NULL && toremove->right->left!=NULL)
{
toremove->right=toremove->right->left;
return 1;
}
if(toremove->right->right==NULL && toremove->right->left==NULL)
{
toremove->right=NULL;
return 1;
}
else
{
}
}
}
node* tree::search(int x)
{
node *temp=start;
int loc;
if(temp->left->data==x)
return temp;
temp=temp->left;
while(1)
{
if((temp->left!=NULL && temp->left->data==x)
|| (temp->right!=NULL && temp->right->data==x))
{
cout<<endl<<"Found = ";
return temp;
}
if(x<=temp->data)
{
if(temp->left==NULL)
return NULL;
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
return NULL;
else
temp=temp->right;
}
}
}
int tree::empty(void)
{
if(start->left==NULL)
return 1;
else
return 0;
}
int tree::count(void)
{
return(start->data);
}
int tree::depth(void)
{
int depth;
depth=calc_depth(start->left,-1);
return depth;
}
int tree::calc_depth(node *temp,int i)
{
static int depth=-1;
if(temp!=NULL)
{
calc_depth(temp->left,i+1);
calc_depth(temp->right,i+1);
}
if(i>depth)
depth=i;
return depth;
}
int main(void)
{
tree obj;
clrscr();
obj.insert(101);
obj.insert(50);
obj.insert(49);
obj.insert(153);
obj.insert(48);
obj.insert(50);
obj.insert(110);
obj.insert(47);
obj.insert(112);
obj.insert(111);
obj.insert(150);
obj.display();
obj.display();
cout<<endl<<"No. of nodes : "<<obj.count();
cout<<endl<<"Depth of tree is : "<<obj.depth();
getch();
return 0;
} #include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class tree
{
private:
struct node
{
int data;
struct node *left;
struct node *right;
};
node *start;
public:
tree(void)
{
if(!(start=new node))
{
cout<<"Insufficient memory";
getch();
exit(1);
}
start->data=0;
start->left=NULL;
start->right=NULL;
}
int insert(int);
void display(void);
void display_preorder(node *temp);
void display_inorder(node *temp);
void display_postorder(node *temp);
int count(void);
int remove(int);
int calc_depth(node *temp,int);
int depth(void);
node* search(int);
int empty(void);
~tree()
{
delete start;
cout<<endl<<"List deleted";
}
};
int tree::insert(int x)
{
node *temp=start->left;
if(empty())
{
if(!(start->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
cout<<"Empty "<<x;
start->data=++start->data;
start->left->data=x;
start->left->left=NULL;
start->left->right=NULL;
return 1;
}
}
while(1)
{
if(x<=temp->data)
{
if(temp->left==NULL)
{
if(!(temp->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
cout<<endl<<"Left node "<<x;
return 1;
}
}
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
if(!(temp->right=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
cout<<endl<<"Right node "<<x;
return 1;
}
}
else
temp=temp->right;
}
}
}
void tree::display(void)
{
cout<<endl<<"No. of nodes : "<<start->data;
cout<<endl<<"Preorder : ";
display_preorder(start->left);
cout<<endl<<"ineorder : ";
display_inorder(start->left);
cout<<endl<<"Postorder : ";
display_postorder(start->left);
return;
}
void tree::display_preorder(node *temp)
{
if(temp!=NULL)
{
cout<<"\t"<<temp->data;
display_preorder(temp->left);
display_preorder(temp->right);
}
return;
}
void tree::display_inorder(node *temp)
{
if(temp!=NULL)
{
display_inorder(temp->left);
cout<<"\t"<<temp->data;
display_inorder(temp->right);
}
return;
}
void tree::display_postorder(node *temp)
{
if(temp!=NULL)
{
display_postorder(temp->left);
display_postorder(temp->right);
cout<<"\t"<<temp->data;
}
return;
}
int tree::remove(int x)
{
node *toremove,*temp;
if((toremove=search(x))==NULL)
{
cout<<endl<<"Not Found";
return -1;
}
start->data--;
if(toremove->left->data==x)
{
if(toremove->left->right==NULL && toremove->left->left!=NULL)
{
toremove->left=toremove->left->left;
return 1;
}
if(toremove->left->right==NULL && toremove->left->left==NULL)
{
toremove->left=NULL;
return 1;
}
else
{
temp=toremove->right;
for(;temp->left->left!=NULL;temp=temp->left);
}
}
else
{
if(toremove->right->right==NULL && toremove->right->left!=NULL)
{
toremove->right=toremove->right->left;
return 1;
}
if(toremove->right->right==NULL && toremove->right->left==NULL)
{
toremove->right=NULL;
return 1;
}
else
{
}
}
}
node* tree::search(int x)
{
node *temp=start;
int loc;
if(temp->left->data==x)
return temp;
temp=temp->left;
while(1)
{
if((temp->left!=NULL && temp->left->data==x)
|| (temp->right!=NULL && temp->right->data==x))
{
cout<<endl<<"Found = ";
return temp;
}
if(x<=temp->data)
{
if(temp->left==NULL)
return NULL;
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
return NULL;
else
temp=temp->right;
}
}
}
int tree::empty(void)
{
if(start->left==NULL)
return 1;
else
return 0;
}
int tree::count(void)
{
return(start->data);
}
int tree::depth(void)
{
int depth;
depth=calc_depth(start->left,-1);
return depth;
}
int tree::calc_depth(node *temp,int i)
{
static int depth=-1;
if(temp!=NULL)
{
calc_depth(temp->left,i+1);
calc_depth(temp->right,i+1);
}
if(i>depth)
depth=i;
return depth;
}
int main(void)
{
tree obj;
clrscr();
obj.insert(101);
obj.insert(50);
obj.insert(49);
obj.insert(153);
obj.insert(48);
obj.insert(50);
obj.insert(110);
obj.insert(47);
obj.insert(112);
obj.insert(111);
obj.insert(150);
obj.display();
obj.display();
cout<<endl<<"No. of nodes : "<<obj.count();
cout<<endl<<"Depth of tree is : "<<obj.depth();
getch();
return 0;
} #include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class tree
{
private:
struct node
{
int data;
struct node *left;
struct node *right;
};
node *start;
public:
tree(void)
{
if(!(start=new node))
{
cout<<"Insufficient memory";
getch();
exit(1);
}
start->data=0;
start->left=NULL;
start->right=NULL;
}
int insert(int);
void display(void);
void display_preorder(node *temp);
void display_inorder(node *temp);
void display_postorder(node *temp);
int count(void);
int remove(int);
int calc_depth(node *temp,int);
int depth(void);
node* search(int);
int empty(void);
~tree()
{
delete start;
cout<<endl<<"List deleted";
}
};
int tree::insert(int x)
{
node *temp=start->left;
if(empty())
{
if(!(start->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
cout<<"Empty "<<x;
start->data=++start->data;
start->left->data=x;
start->left->left=NULL;
start->left->right=NULL;
return 1;
}
}
while(1)
{
if(x<=temp->data)
{
if(temp->left==NULL)
{
if(!(temp->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
cout<<endl<<"Left node "<<x;
return 1;
}
}
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
if(!(temp->right=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
cout<<endl<<"Right node "<<x;
return 1;
}
}
else
temp=temp->right;
}
}
}
void tree::display(void)
{
cout<<endl<<"No. of nodes : "<<start->data;
cout<<endl<<"Preorder : ";
display_preorder(start->left);
cout<<endl<<"ineorder : ";
display_inorder(start->left);
cout<<endl<<"Postorder : ";
display_postorder(start->left);
return;
}
void tree::display_preorder(node *temp)
{
if(temp!=NULL)
{
cout<<"\t"<<temp->data;
display_preorder(temp->left);
display_preorder(temp->right);
}
return;
}
void tree::display_inorder(node *temp)
{
if(temp!=NULL)
{
display_inorder(temp->left);
cout<<"\t"<<temp->data;
display_inorder(temp->right);
}
return;
}
void tree::display_postorder(node *temp)
{
if(temp!=NULL)
{
display_postorder(temp->left);
display_postorder(temp->right);
cout<<"\t"<<temp->data;
}
return;
}
int tree::remove(int x)
{
node *toremove,*temp;
if((toremove=search(x))==NULL)
{
cout<<endl<<"Not Found";
return -1;
}
start->data--;
if(toremove->left->data==x)
{
if(toremove->left->right==NULL && toremove->left->left!=NULL)
{
toremove->left=toremove->left->left;
return 1;
}
if(toremove->left->right==NULL && toremove->left->left==NULL)
{
toremove->left=NULL;
return 1;
}
else
{
temp=toremove->right;
for(;temp->left->left!=NULL;temp=temp->left);
}
}
else
{
if(toremove->right->right==NULL && toremove->right->left!=NULL)
{
toremove->right=toremove->right->left;
return 1;
}
if(toremove->right->right==NULL && toremove->right->left==NULL)
{
toremove->right=NULL;
return 1;
}
else
{
}
}
}
node* tree::search(int x)
{
node *temp=start;
int loc;
if(temp->left->data==x)
return temp;
temp=temp->left;
while(1)
{
if((temp->left!=NULL && temp->left->data==x)
|| (temp->right!=NULL && temp->right->data==x))
{
cout<<endl<<"Found = ";
return temp;
}
if(x<=temp->data)
{
if(temp->left==NULL)
return NULL;
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
return NULL;
else
temp=temp->right;
}
}
}
int tree::empty(void)
{
if(start->left==NULL)
return 1;
else
return 0;
}
int tree::count(void)
{
return(start->data);
}
int tree::depth(void)
{
int depth;
depth=calc_depth(start->left,-1);
return depth;
}
int tree::calc_depth(node *temp,int i)
{
static int depth=-1;
if(temp!=NULL)
{
calc_depth(temp->left,i+1);
calc_depth(temp->right,i+1);
}
if(i>depth)
depth=i;
return depth;
}
int main(void)
{
tree obj;
clrscr();
obj.insert(101);
obj.insert(50);
obj.insert(49);
obj.insert(153);
obj.insert(48);
obj.insert(50);
obj.insert(110);
obj.insert(47);
obj.insert(112);
obj.insert(111);
obj.insert(150);
obj.display();
obj.display();
cout<<endl<<"No. of nodes : "<<obj.count();
cout<<endl<<"Depth of tree is : "<<obj.depth();
getch();
return 0;
}
#include<conio.h>
#include<stdlib.h>
class tree
{
private:
struct node
{
int data;
struct node *left;
struct node *right;
};
node *start;
public:
tree(void)
{
if(!(start=new node))
{
cout<<"Insufficient memory";
getch();
exit(1);
}
start->data=0;
start->left=NULL;
start->right=NULL;
}
int insert(int);
void display(void);
void display_preorder(node *temp);
void display_inorder(node *temp);
void display_postorder(node *temp);
int count(void);
int remove(int);
int calc_depth(node *temp,int);
int depth(void);
node* search(int);
int empty(void);
~tree()
{
delete start;
cout<<endl<<"List deleted";
}
};
int tree::insert(int x)
{
node *temp=start->left;
if(empty())
{
if(!(start->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
cout<<"Empty "<<x;
start->data=++start->data;
start->left->data=x;
start->left->left=NULL;
start->left->right=NULL;
return 1;
}
}
while(1)
{
if(x<=temp->data)
{
if(temp->left==NULL)
{
if(!(temp->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
cout<<endl<<"Left node "<<x;
return 1;
}
}
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
if(!(temp->right=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
cout<<endl<<"Right node "<<x;
return 1;
}
}
else
temp=temp->right;
}
}
}
void tree::display(void)
{
cout<<endl<<"No. of nodes : "<<start->data;
cout<<endl<<"Preorder : ";
display_preorder(start->left);
cout<<endl<<"ineorder : ";
display_inorder(start->left);
cout<<endl<<"Postorder : ";
display_postorder(start->left);
return;
}
void tree::display_preorder(node *temp)
{
if(temp!=NULL)
{
cout<<"\t"<<temp->data;
display_preorder(temp->left);
display_preorder(temp->right);
}
return;
}
void tree::display_inorder(node *temp)
{
if(temp!=NULL)
{
display_inorder(temp->left);
cout<<"\t"<<temp->data;
display_inorder(temp->right);
}
return;
}
void tree::display_postorder(node *temp)
{
if(temp!=NULL)
{
display_postorder(temp->left);
display_postorder(temp->right);
cout<<"\t"<<temp->data;
}
return;
}
int tree::remove(int x)
{
node *toremove,*temp;
if((toremove=search(x))==NULL)
{
cout<<endl<<"Not Found";
return -1;
}
start->data--;
if(toremove->left->data==x)
{
if(toremove->left->right==NULL && toremove->left->left!=NULL)
{
toremove->left=toremove->left->left;
return 1;
}
if(toremove->left->right==NULL && toremove->left->left==NULL)
{
toremove->left=NULL;
return 1;
}
else
{
temp=toremove->right;
for(;temp->left->left!=NULL;temp=temp->left);
}
}
else
{
if(toremove->right->right==NULL && toremove->right->left!=NULL)
{
toremove->right=toremove->right->left;
return 1;
}
if(toremove->right->right==NULL && toremove->right->left==NULL)
{
toremove->right=NULL;
return 1;
}
else
{
}
}
}
node* tree::search(int x)
{
node *temp=start;
int loc;
if(temp->left->data==x)
return temp;
temp=temp->left;
while(1)
{
if((temp->left!=NULL && temp->left->data==x)
|| (temp->right!=NULL && temp->right->data==x))
{
cout<<endl<<"Found = ";
return temp;
}
if(x<=temp->data)
{
if(temp->left==NULL)
return NULL;
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
return NULL;
else
temp=temp->right;
}
}
}
int tree::empty(void)
{
if(start->left==NULL)
return 1;
else
return 0;
}
int tree::count(void)
{
return(start->data);
}
int tree::depth(void)
{
int depth;
depth=calc_depth(start->left,-1);
return depth;
}
int tree::calc_depth(node *temp,int i)
{
static int depth=-1;
if(temp!=NULL)
{
calc_depth(temp->left,i+1);
calc_depth(temp->right,i+1);
}
if(i>depth)
depth=i;
return depth;
}
int main(void)
{
tree obj;
clrscr();
obj.insert(101);
obj.insert(50);
obj.insert(49);
obj.insert(153);
obj.insert(48);
obj.insert(50);
obj.insert(110);
obj.insert(47);
obj.insert(112);
obj.insert(111);
obj.insert(150);
obj.display();
obj.display();
cout<<endl<<"No. of nodes : "<<obj.count();
cout<<endl<<"Depth of tree is : "<<obj.depth();
getch();
return 0;
} #include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class tree
{
private:
struct node
{
int data;
struct node *left;
struct node *right;
};
node *start;
public:
tree(void)
{
if(!(start=new node))
{
cout<<"Insufficient memory";
getch();
exit(1);
}
start->data=0;
start->left=NULL;
start->right=NULL;
}
int insert(int);
void display(void);
void display_preorder(node *temp);
void display_inorder(node *temp);
void display_postorder(node *temp);
int count(void);
int remove(int);
int calc_depth(node *temp,int);
int depth(void);
node* search(int);
int empty(void);
~tree()
{
delete start;
cout<<endl<<"List deleted";
}
};
int tree::insert(int x)
{
node *temp=start->left;
if(empty())
{
if(!(start->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
cout<<"Empty "<<x;
start->data=++start->data;
start->left->data=x;
start->left->left=NULL;
start->left->right=NULL;
return 1;
}
}
while(1)
{
if(x<=temp->data)
{
if(temp->left==NULL)
{
if(!(temp->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
cout<<endl<<"Left node "<<x;
return 1;
}
}
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
if(!(temp->right=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
cout<<endl<<"Right node "<<x;
return 1;
}
}
else
temp=temp->right;
}
}
}
void tree::display(void)
{
cout<<endl<<"No. of nodes : "<<start->data;
cout<<endl<<"Preorder : ";
display_preorder(start->left);
cout<<endl<<"ineorder : ";
display_inorder(start->left);
cout<<endl<<"Postorder : ";
display_postorder(start->left);
return;
}
void tree::display_preorder(node *temp)
{
if(temp!=NULL)
{
cout<<"\t"<<temp->data;
display_preorder(temp->left);
display_preorder(temp->right);
}
return;
}
void tree::display_inorder(node *temp)
{
if(temp!=NULL)
{
display_inorder(temp->left);
cout<<"\t"<<temp->data;
display_inorder(temp->right);
}
return;
}
void tree::display_postorder(node *temp)
{
if(temp!=NULL)
{
display_postorder(temp->left);
display_postorder(temp->right);
cout<<"\t"<<temp->data;
}
return;
}
int tree::remove(int x)
{
node *toremove,*temp;
if((toremove=search(x))==NULL)
{
cout<<endl<<"Not Found";
return -1;
}
start->data--;
if(toremove->left->data==x)
{
if(toremove->left->right==NULL && toremove->left->left!=NULL)
{
toremove->left=toremove->left->left;
return 1;
}
if(toremove->left->right==NULL && toremove->left->left==NULL)
{
toremove->left=NULL;
return 1;
}
else
{
temp=toremove->right;
for(;temp->left->left!=NULL;temp=temp->left);
}
}
else
{
if(toremove->right->right==NULL && toremove->right->left!=NULL)
{
toremove->right=toremove->right->left;
return 1;
}
if(toremove->right->right==NULL && toremove->right->left==NULL)
{
toremove->right=NULL;
return 1;
}
else
{
}
}
}
node* tree::search(int x)
{
node *temp=start;
int loc;
if(temp->left->data==x)
return temp;
temp=temp->left;
while(1)
{
if((temp->left!=NULL && temp->left->data==x)
|| (temp->right!=NULL && temp->right->data==x))
{
cout<<endl<<"Found = ";
return temp;
}
if(x<=temp->data)
{
if(temp->left==NULL)
return NULL;
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
return NULL;
else
temp=temp->right;
}
}
}
int tree::empty(void)
{
if(start->left==NULL)
return 1;
else
return 0;
}
int tree::count(void)
{
return(start->data);
}
int tree::depth(void)
{
int depth;
depth=calc_depth(start->left,-1);
return depth;
}
int tree::calc_depth(node *temp,int i)
{
static int depth=-1;
if(temp!=NULL)
{
calc_depth(temp->left,i+1);
calc_depth(temp->right,i+1);
}
if(i>depth)
depth=i;
return depth;
}
int main(void)
{
tree obj;
clrscr();
obj.insert(101);
obj.insert(50);
obj.insert(49);
obj.insert(153);
obj.insert(48);
obj.insert(50);
obj.insert(110);
obj.insert(47);
obj.insert(112);
obj.insert(111);
obj.insert(150);
obj.display();
obj.display();
cout<<endl<<"No. of nodes : "<<obj.count();
cout<<endl<<"Depth of tree is : "<<obj.depth();
getch();
return 0;
} #include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class tree
{
private:
struct node
{
int data;
struct node *left;
struct node *right;
};
node *start;
public:
tree(void)
{
if(!(start=new node))
{
cout<<"Insufficient memory";
getch();
exit(1);
}
start->data=0;
start->left=NULL;
start->right=NULL;
}
int insert(int);
void display(void);
void display_preorder(node *temp);
void display_inorder(node *temp);
void display_postorder(node *temp);
int count(void);
int remove(int);
int calc_depth(node *temp,int);
int depth(void);
node* search(int);
int empty(void);
~tree()
{
delete start;
cout<<endl<<"List deleted";
}
};
int tree::insert(int x)
{
node *temp=start->left;
if(empty())
{
if(!(start->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
cout<<"Empty "<<x;
start->data=++start->data;
start->left->data=x;
start->left->left=NULL;
start->left->right=NULL;
return 1;
}
}
while(1)
{
if(x<=temp->data)
{
if(temp->left==NULL)
{
if(!(temp->left=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
cout<<endl<<"Left node "<<x;
return 1;
}
}
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
if(!(temp->right=new node))
{
cout<<"Insufficient memory";
return -1;
}
else
{
start->data=++start->data;
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
cout<<endl<<"Right node "<<x;
return 1;
}
}
else
temp=temp->right;
}
}
}
void tree::display(void)
{
cout<<endl<<"No. of nodes : "<<start->data;
cout<<endl<<"Preorder : ";
display_preorder(start->left);
cout<<endl<<"ineorder : ";
display_inorder(start->left);
cout<<endl<<"Postorder : ";
display_postorder(start->left);
return;
}
void tree::display_preorder(node *temp)
{
if(temp!=NULL)
{
cout<<"\t"<<temp->data;
display_preorder(temp->left);
display_preorder(temp->right);
}
return;
}
void tree::display_inorder(node *temp)
{
if(temp!=NULL)
{
display_inorder(temp->left);
cout<<"\t"<<temp->data;
display_inorder(temp->right);
}
return;
}
void tree::display_postorder(node *temp)
{
if(temp!=NULL)
{
display_postorder(temp->left);
display_postorder(temp->right);
cout<<"\t"<<temp->data;
}
return;
}
int tree::remove(int x)
{
node *toremove,*temp;
if((toremove=search(x))==NULL)
{
cout<<endl<<"Not Found";
return -1;
}
start->data--;
if(toremove->left->data==x)
{
if(toremove->left->right==NULL && toremove->left->left!=NULL)
{
toremove->left=toremove->left->left;
return 1;
}
if(toremove->left->right==NULL && toremove->left->left==NULL)
{
toremove->left=NULL;
return 1;
}
else
{
temp=toremove->right;
for(;temp->left->left!=NULL;temp=temp->left);
}
}
else
{
if(toremove->right->right==NULL && toremove->right->left!=NULL)
{
toremove->right=toremove->right->left;
return 1;
}
if(toremove->right->right==NULL && toremove->right->left==NULL)
{
toremove->right=NULL;
return 1;
}
else
{
}
}
}
node* tree::search(int x)
{
node *temp=start;
int loc;
if(temp->left->data==x)
return temp;
temp=temp->left;
while(1)
{
if((temp->left!=NULL && temp->left->data==x)
|| (temp->right!=NULL && temp->right->data==x))
{
cout<<endl<<"Found = ";
return temp;
}
if(x<=temp->data)
{
if(temp->left==NULL)
return NULL;
else
temp=temp->left;
}
else
{
if(temp->right==NULL)
return NULL;
else
temp=temp->right;
}
}
}
int tree::empty(void)
{
if(start->left==NULL)
return 1;
else
return 0;
}
int tree::count(void)
{
return(start->data);
}
int tree::depth(void)
{
int depth;
depth=calc_depth(start->left,-1);
return depth;
}
int tree::calc_depth(node *temp,int i)
{
static int depth=-1;
if(temp!=NULL)
{
calc_depth(temp->left,i+1);
calc_depth(temp->right,i+1);
}
if(i>depth)
depth=i;
return depth;
}
int main(void)
{
tree obj;
clrscr();
obj.insert(101);
obj.insert(50);
obj.insert(49);
obj.insert(153);
obj.insert(48);
obj.insert(50);
obj.insert(110);
obj.insert(47);
obj.insert(112);
obj.insert(111);
obj.insert(150);
obj.display();
obj.display();
cout<<endl<<"No. of nodes : "<<obj.count();
cout<<endl<<"Depth of tree is : "<<obj.depth();
getch();
return 0;
}
No comments:
Post a Comment