W.Ali
W.Ali

Reputation: 45

Binary Search Tree in C.

I am confused that what mistake i am doing. Why isn't it printing the tree in preorder, postorder and inorder. I have written my questions beside the line of code where i am confused. Plus i don't understand how to relate to structs.

#include <stdio.h>
#include<stdlib.h>

typedef struct BST_{
    int data;
    struct BST_ *lchild, *rchild, *parent;
}BST;

typedef struct BiTree_{
    int size;
    BST *root;  // is it possible to relate this root with the pointer parent of type BST? i yes then how?
}BiTree;;
BST *temp;
BST *createNode(int data){
    BST *new_ele;
    new_ele=(BST*)malloc(sizeof(BST));
    new_ele->data=data;
    new_ele->lchild=NULL;
    new_ele->rchild=NULL;
    new_ele->parent=NULL;
    return new_ele;
    }

void insertNode(BST *temp,BST *r,int data){
    if(temp==NULL){
            temp=createNode(data);
         }

    else if(temp->data >= r->data){
            if(r->rchild==NULL){
                r->rchild=temp;
            }
            else{
                insertNode(r->rchild,temp,data);
            }
    }
    else if(temp->data <= r->data){
            if(r->lchild==NULL){
                r->lchild=temp; 
            }
            else{
                insertNode(r->lchild,temp,data);    
                            }
    }
//  return r->data;  //why cann't i do this?
}

void preorder(BST *r){
    if(r!=NULL){
        printf("%d",r->data);
        preorder(r->lchild);
        preorder(r->rchild);
    }
}
void inorder(BST *c){
    if(c!=NULL){

        inorder(c->lchild);
        printf("%d",c->data);
        inorder(c->rchild);
    }
}
void postorder(BST *r){
    if(r!=NULL){
        postorder(r->lchild);
        postorder(r->rchild);
        printf("%d",r->data);
    }
}
void delet(BST *r){
    if(r!=NULL){
        free(r);
    }
}
void main(){
    BST *new_node,*r=NULL;
    BiTree *search;
    search=malloc(sizeof(BiTree));
    search->root=NULL;
    search->size=0;
    int i,a,n,data;
    printf("Enter the number of element to be inserted\n");
    scanf("%d",&n);
    printf("Enter the data\n");
    for(i=0;i<n;i++){
            scanf("%d",&data);


            insertNode(temp,r,data);
            //  printf("Enter the data %d\n",r->data);  // unable to print this

            search->size++;
            }
    printf("size is %d\n",search->size);

    preorder(r);//  why cann't i print the data of r. r is the root of the tree.
    postorder(r);
    inorder(r);
    delet(r);

}

Upvotes: 0

Views: 88

Answers (2)

pankaj
pankaj

Reputation: 1346

temp is not set to NULL, and I think if its not NULL, it is going to have garbage in it. With this your insertNode() function will not allocate memory and all the code inside this function is entirely dependent on the garbage value of temp.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

  • // is it possible to relate this root with the pointer parent of type BST? i yes then how?*

Please rephrase this question

  • // return r->data; //why cann't i do this?

The signature of the function void insertNode(BST *temp,BST *r,int data){ is saying that nothing should be returned. Change this

  • printf("Enter the data %d\n",r->data); // unable to print this

In the function r is initialised to NULL and nothing in the function changes this

  • why cann't i print the data of r. r is the root of the tree.

See the point above

Upvotes: 1

Related Questions