BST insertion ( protection exception error) - TechRepublic
General discussion
September 17, 2011 at 03:34 AM
ruksu88

BST insertion ( protection exception error)

by ruksu88 . Updated 14 years, 9 months ago

Hi ,

I have written a prog to insert elements in binary search tree…

wen i run the prog I have got an error i.e general protection exception, processor fault…

Pls help me to find wats the error and how to resolve it…

here is the prog

#include
#include
#include

struct node{
struct node * left;
int val;
struct node * right;
} *root;

//create node and return its adress
struct node * create_node(int x)
{
struct node * new_node = (struct node *)malloc(sizeof(struct node));
new_node->val = x;
return new_node;
}

//function to insert values in BST
void ins(struct node *temp, int x)
{
if(temp->val == x){printf(“\nduplicate value\n”);}
else
{
if(x < temp->val)
{ printf(“\ngoing left\n”);
if(temp->left == NULL){
temp->left = create_node(x);
if(temp->left){printf(“\nnew node created and its value is %d\n”,temp->left->val);}
}
else{ins(temp->left, x);}
}
else{
printf(“\ngoing right\n”);
if(temp->right == NULL){temp->right = create_node(x); }
else{ins(temp->right, x);}
}
}
//free(temp);
return ;
}

void main()
{
int val,label;

do {

printf(“\nINSERT-1\nDELETE-2\nEXIT-3\n”);
scanf(“%d”,&label);
switch(label)
{
case 1 : printf(“enter value\n”);
scanf(“%d”,&val); //printf(“\nvalue u entered is %d\n”,val);
if(root == NULL)
{
root = create_node(val);
if(root)
printf(“\nfirst value inserted as root \n”);
}
else
{
ins(root, val);
}
break;

case 2 : printf(“\ndelete”);
break;

default : exit(0);
}
}while (label<=2); free(root); }

This discussion is locked

All Comments