Here, is my code
#include<iostream>
using namespace std;
int get_data;
struct node
{
int data;
node *left, *right;
}*root,*new_node;
class binary_terr
{
public:
void insert_node(node *tree, node *new_node);
void delete_node(node *tree);
binary_terr()
{
root = NULL;
}
};
int main()
{
int in_data, i=0;
binary_terr bin;
while(i < 6)
{
new_node = new node;
cout<<"Enter the data you want to insert";
cin>>in_data;
new_node->data = in_data;
bin.insert_node(root, new_node);
//new_node = NULL;
cout<<"\n"<<"value of iterator"<<i<<"\n";
++i;
}
cout<<"Enter data you want to find";
bin.delete_node(root);
return 0;
}
void binary_terr :: insert_node(node *tree, node *new_node)
{
// tree = new_node;
if(root == NULL)
{
root = new node;
cout<<"root created";
root->data = new_node->data;
root->left = NULL;
root->right = NULL;
return;
}
else if((tree->data) > (new_node->data))
{
if(tree->left != NULL)
{
insert_node(tree->left, new_node);
}
else
cout<<"Node attach to left of tree"<<tree->data;
{
tree->left = new_node;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
return;
}
}
else
{
if(tree->right != NULL)
{
insert_node(tree->right, new_node);
}
else
{
cout<<"Node attach to right of tree"<<tree->data;
tree->right = new_node;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
return;
}
}
};
void binary_terr :: delete_node(node *tree)
{
if (tree->data > get_data)
{
if((tree->data) != get_data)
{
delete_node(tree->left);
}
else
{
cout<<"Data find on left side of tree";
}
}
else
{
if((tree->data) != get_data)
{
delete_node(tree->right);
}
else
{
cout<<"Data is find on right side of tree";
}
}
}
When i am executing the code. I got following error:
Enter the data you want to insert34
root created
value of iterator0
Enter the data you want to insert23
Node attach to left of tree34
value of iterator1
Enter the data you want to insert12
Node attach to left of tree23
value of iterator2
Enter the data you want to insert54
Node attach to right of tree34
value of iterator3
Enter the data you want to insert65
Node attach to right of tree54
value of iterator4
Enter the data you want to insert76
Node attach to right of tree65
value of iterator5
Segmentation fault (core dumped)
So, where is exactly problem. I appreciate your help thanks!.
Aucun commentaire:
Enregistrer un commentaire