0

Below is my code for the BST : insert works fine but search doesnt

typedef struct tree_node{

struct tree_node* parent;
struct tree_node* left;
struct tree_node* right;
int x;
}tree_node;

tree_node *strt=NULL;

tree_node *traverse;

tree_node* create(int info){

tree_node *temp=NULL;
temp=(tree_node*)malloc(sizeof(tree_node));

temp->parent=NULL;
temp->left=NULL;
temp->right=NULL;
temp->x=info;

return temp;
}

tree_node* insert(tree_node *root, int a){

/* here i am printing the address of the node which must be same as the root for the first node */

if(root==NULL){
   printf("%d ",create(a)); 
   return create(a);        
}

else if(a <= root->x)
    return insert(root->left,a);

else
    return insert(root->right,a);

return root;
}

tree_node* search_ele(tree_node *root,int info){

if(root==NULL || root->x==info)
    return root ;

if(info < root->x)
    return search_ele(root->left,info);

else
    return search_ele(root->right,info);

}

void display_inorder(tree_node *root){

if(root==NULL)
    return;

display_inorder(root->left);
printf("%d ",root->x);
display_inorder(root->right);
}

void main(){

int element;
tree_node *search_element;

while(1){

char ch;
int num;

printf("\nWant to enter a node..??\n\n");
scanf(" %c",&ch);
if(ch=='n'||ch=='N')
break;

else{

    printf("Enter the number \n");
    scanf("%d",&num);

    if(strt==NULL)
        printf("Tree is empty...entering first node...!!!\n");

    strt=insert(strt,num);
    printf("%d",strt);
    }
  }



printf("Enter the element u want to search\n");
scanf("%d ",&element);

if(search_ele(strt,element)==NULL)
printf("no such element\n");

else
    printf("element found\n");

display_inorder(strt);
}

The output displays :

Want to enter a node ?

y
Enter the number 
6
Tree is empty...entering first node...!!!
5279480 5279504 (why are these different??)
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101

2 Answers2

2

You print the result of calling create, and then call create again for the return value, thus creating a second node.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

First the problems with your BST Algorithm: Your node creation algorithm never attaches new nodes to a parent. Create() sets node->parent to NULL and your insert() never updates the parent variable to root. Additionally, you're never setting the new left/right fields of your new node's parent

Change

 return create(a)

to

 tree_node * new_node = create(a);
 new_node->parent=root;
 new_node->parent->left/right=new_node;  //include some way to distinguish whether you went left or right in the previous recursive call
 return new_node

Now for your address question, you call create() twice, which (think procedurally) results in two malloc() calls, and thus two addresses.

Kdawg
  • 167
  • 11
  • i have added nodes using the above code....it is accepting the integers and printing it in sorted order when i print the inorder traversal for it !! – user3590607 Aug 13 '15 at 10:48
  • but i dont understnd y the same approach is not working for the search – user3590607 Aug 13 '15 at 10:49
  • moreover am adding nthng to the parent node...so i dnt think it would matter much since evry node's parent part would be null..but the left and right will have the addresses of their children – user3590607 Aug 13 '15 at 10:50