C 二叉搜索树。

C 二叉搜索树。,c,C,我对我所犯的错误感到困惑。为什么不按前序、后序和顺序打印树呢。我把问题写在我感到困惑的代码行旁边。另外,我不明白如何与结构相关 #include <stdio.h> #include<stdlib.h> typedef struct BST_{ int data; struct BST_ *lchild, *rchild, *parent; }BST; typedef struct BiTree_{ int size; BST *roo

我对我所犯的错误感到困惑。为什么不按前序、后序和顺序打印树呢。我把问题写在我感到困惑的代码行旁边。另外,我不明白如何与结构相关

#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);

}
#包括
#包括
类型定义结构BST_{
int数据;
结构BST*lchild,*rchild,*parent;
}英国理工学院;
类型定义结构位树_{
整数大小;
BST*root;//是否可以将此根与BST类型的指针父级关联?我是,那么如何关联?
}比特树;;
英国标准时间*温度;
BST*createNode(整数数据){
英国夏令时*新奥尔良;
new_ele=(BST*)malloc(sizeof(BST));
新建元素->数据=数据;
new_ele->lchild=NULL;
新建元素->rchild=NULL;
新建元素->父元素=NULL;
返回新的_ele;
}
void insertNode(BST*temp、BST*r、int数据){
if(temp==NULL){
temp=createNode(数据);
}
否则如果(临时->数据>=r->数据){
如果(r->rchild==NULL){
r->rchild=温度;
}
否则{
插入节点(r->rchild、temp、data);
}
}
else if(临时->数据){
如果(r->lchild==NULL){
r->lchild=温度;
}
否则{
插入节点(r->lchild、temp、data);
}
}
//return r->data;//为什么我不能这样做?
}
无效预订单(BST*r){
如果(r!=NULL){
printf(“%d”,r->data);
预订单(r->lchild);
预订单(r->rchild);
}
}
无效顺序(BST*c){
如果(c!=NULL){
顺序(c->lchild);
printf(“%d”,c->data);
顺序(c->rchild);
}
}
无效邮购(英国夏令时*r){
如果(r!=NULL){
邮购(r->lchild);
邮购(r->rchild);
printf(“%d”,r->data);
}
}
无效删除(BST*r){
如果(r!=NULL){
自由(r);
}
}
void main(){
BST*新节点,*r=NULL;
比特树*搜索;
search=malloc(sizeof(BiTree));
搜索->根=空;
搜索->大小=0;
int i,a,n,数据;
printf(“输入要插入的元素编号\n”);
scanf(“%d”和“&n”);
printf(“输入数据”);
对于(i=0;idata);//无法打印此
搜索->大小++;
}
printf(“大小为%d\n”,搜索->大小);
前序(r);//为什么我不能打印r的数据。r是树的根。
邮购(r);
有序(r);
删除(r);
}
  • //是否可以将此根目录与BST类型的指针父目录关联?我是的,那怎么办*
请重新措辞这个问题

  • //返回r->data//为什么我不能这样做
函数
void insertNode(BST*temp,BST*r,int data){
的签名表示不应返回任何内容。请更改此项

  • printf(“输入数据%d\n”,r->data);//无法打印此数据
在函数中,
r
被初始化为
NULL
,函数中的任何内容都不会改变这一点

  • 为什么我不能打印r的数据。r是树的根
参见上面的要点
  • //是否可以将此根目录与BST类型的指针父目录相关联?是的,那么如何关联*
  • 请重新措辞这个问题

    • //return r->data;//为什么我不能这样做
    函数
    void insertNode(BST*temp,BST*r,int data){
    的签名表示不应返回任何内容。请更改此项

    • printf(“输入数据%d\n”,r->data);//无法打印此数据
    在函数中,
    r
    被初始化为
    NULL
    ,函数中的任何内容都不会改变这一点

    • 为什么我不能打印r的数据。r是树的根

    请看上面的一点

    temp
    没有设置为NULL,我认为如果它不是
    NULL
    ,它将包含垃圾。
    有了这个函数,
    insertNode()
    函数将不会分配内存,并且这个函数中的所有代码都完全依赖于temp的垃圾值。

    temp
    没有设置为NULL,我认为如果它不是
    NULL
    ,它将包含垃圾。
    使用此选项,您的
    insertNode()
    函数不会分配内存,该函数中的所有代码完全取决于temp的垃圾值。

    请通过提供输入、预期输出和实际输出更准确地描述程序的行为。
    r
    从不为空。它只要求输入并给出大小。我想在哪里按前序、后序和顺序打印树,然后将其删除。我不知道如何修复它。请通过提供输入、预期输出和实际输出更准确地描述程序的行为。
    r
    从不为空。它只要求输入并给出大小。我想按顺序打印树的位置是storder和inorder,然后将其删除。我不知道如何修复它。很抱歉,由于其他问题,它可能无法工作。但此初始化是必要的。您是否尝试将其置于调试器下?如果没有,请告诉我-我会这样做。很抱歉,由于其他问题,它可能无法工作。但此初始化是无效的必要的。你试过把它放到调试器下吗?如果没有,让我知道-我会做的。