Documente Academic
Documente Profesional
Documente Cultură
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
#include<iostream.h>
class bin_tree
{
private:
N *root;
public :
bin_tree()
{
root=NULL;
}
void create()
{
char ans;
int item;
/*
cout<<"\n Enter the value for the root node : ";
cin>>item;
root = new N;
root->data=item;
*/
while(1)
{
cout<<"\n\n Enter the value for the node(-999 to stop) :
";
cin>>item;
if(item == -999)
return;
insert(root,item);
}
}
else
{
if(item < tn->data)
insert(tn->lc,item);
else
insert(tn->rc,item);
}
}
temp=root;
parent=NULL;
while(temp != NULL)
{
if(temp->data == item)
{
req_node=temp;
return(1);
}
else
{
parent=temp;
temp=temp->rc;
}
return(0);
}
if(root == NULL)
cout<<"\n\n\t The tree is empty !! ";
else
{
parent=NULL;
req_node=NULL;
search_result = search(parent,req_node,item);
if(search_result == 0)
{
cout<<"\n\n Data to be deleted was not found in the tree
!!";
return;
}
while(node_suc->lc != NULL)
{
parent = node_suc;
node_suc = node_suc->lc;
}
req_node->data = node_suc->data;
req_node = node_suc;
}
else
parent->lc = NULL;
delete (req_node);//free(req_node);
return;
}
delete (req_node);//free(req_node);
return;
}
else
parent->rc = req_node->lc;
delete (req_node);//free(req_node);
return;
}
inorder(in_root->lc);
cout<<" "<<in_root->data;
inorder(in_root->rc);
}
N * root_val()
{
return(root);
}
};
void main()
{
bin_tree T;
int item;
cout<<"\t\t C++ Program for the Binary Search Tree\n"
"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
T.create();
T.inorder(T.root_val());
T.del(item);
cout<<"\n\n\n The Binary Search Tree after deletion of the item is :\n\n\t";
T.inorder(T.root_val());