简体   繁体   English

C中的二进制搜索树。

[英]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);

}
  • // is it possible to relate this root with the pointer parent of type BST? //是否可以将此根与BST类型的指针父级相关联? i yes then how?* 我是,那怎么办?*

Please rephrase this question 请改写这个问题

  • // return r->data; //返回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. 函数void insertNode(BST *temp,BST *r,int data){的签名表示什么都不应返回。 Change this 改变这个

  • printf("Enter the data %d\\n",r->data); printf(“输入数据%d \\ n”,r-> data); // unable to print this //无法打印

In the function r is initialised to NULL and nothing in the function changes this 在函数r中初始化为NULL ,函数中没有任何改变

  • why cann't i print the data of r. 为什么我不能打印r的数据。 r is the root of the tree. r是树的根。

See the point above 见以上几点

temp is not set to NULL, and I think if its not NULL , it is going to have garbage in it. temp没有设置为NULL,我认为如果它不是NULL ,它将有垃圾。 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. 有了这个,您的insertNode()函数将不会分配内存,并且此函数内的所有代码完全取决于temp的垃圾值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM