C 为什么我的二叉搜索树节点会断开?

C 为什么我的二叉搜索树节点会断开?,c,C,我需要用BST根做一个链表。列表中的每个节点都有其对应的BST根。我不知道问题出在哪里,但它与BST的节点被覆盖有关。例如,如果我在BST中输入:10 11 19,则根(10)的右节点将指向19而不是11 程序是这样工作的:add函数只调用两次,第一次添加BST的根及其信息,第二次调用为链表创建一个新节点,为BST及其信息创建一个新根 类型BST表示BST的节点,其中info是整数。 类型列表表示BST的链接列表的节点 我需要编写一个函数add,将一个节点添加到链表的末尾,而用户需要输入BST的

我需要用BST根做一个链表。列表中的每个节点都有其对应的BST根。我不知道问题出在哪里,但它与BST的节点被覆盖有关。例如,如果我在BST中输入:10 11 19,则根(10)的右节点将指向19而不是11

程序是这样工作的:add函数只调用两次,第一次添加BST的根及其信息,第二次调用为链表创建一个新节点,为BST及其信息创建一个新根

类型BST表示BST的节点,其中info是整数。 类型列表表示BST的链接列表的节点

我需要编写一个函数add,将一个节点添加到链表的末尾,而用户需要输入BST的每个节点的数据,直到他退出

我的代码:

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

typedef struct BST
{
    int info;
    struct BST * left, * right;
}BST;

typedef struct LIST
{
    BST * root;
    struct LIST * next;
}LIST;

BST * new_node(int info)
{
    BST * new = calloc(1, sizeof(BST));
    new->info = info;
    return new;
}

BST * add_node(BST * root, int info)
{
    if(root == NULL) return new_node(info);
    if(info < root->info) root->left = add_node(root->left, info);
    else if(info >= root->info) root->right = add_node(root->right, info);
}

LIST * add(LIST ** head)
{
    if((* head)->root == NULL)
    {
        BST * BTreeNode = NULL;
        BTreeNode = calloc(1, sizeof(BST));
        (* head)->root = BTreeNode;
        printf("Enter tree root: \n");
        int n_root;
        scanf("%d", &n_root);
        BTreeNode->info = n_root;
        char character;
        do
        {
            printf("Add [A], exit [E]: ");
            scanf("\n%c", &character);
            if(character == 'A')
            {
                printf("Enter data: ");
                int temp;
                scanf("%d", &temp);
                add_node(BTreeNode, temp);

            }
        }while(character != 'E');
    }
    else
    {
        LIST * temp = NULL;
        temp = * head;
        for(; temp->next; temp = temp->next);
        LIST * new = NULL;
        new = calloc(1, sizeof(LIST));
        temp->next = new;

        BST * BTreeNode = NULL;
        BTreeNode = calloc(1, sizeof(BST));
        new->root = BTreeNode;
        printf("Enter tree root: \n");
        int n_root;
        scanf("%d", &n_root);
        BTreeNode->info = n_root;
        char character;
        do
        {
            printf("Add [A], exit [E]: ");
            scanf("\n%c", &character);
            if(character == 'A')
            {
                printf("Enter data: ");
                int temp;
                scanf("%d", &temp);
                add_node(BTreeNode, temp);
            }
        }while(character != 'E');
    }
}

void delete_tree(BST * root)
{
    if(root != NULL)
    {
        delete_tree(root->left);
        delete_tree(root->right);
        free(root);
    }
}

void clean_up(LIST ** head)
{
    while(* head)
    {
        LIST * temp = NULL;
        temp = (* head)->next;
        delete_tree((* head)->root);
        free(* head);
        * head = temp;
    }
}

void inorder(BST * root)
{
    if(root != NULL)
    {
        inorder(root->left);
        printf("%d ", root->info);
        inorder(root->right);
    }
}

void write(LIST * head)
{
    int counter = 1;
    LIST * temp = NULL;
    temp = head;
    while(temp)
    {
        printf("\nInfo of %d. tree: ", counter++);
        inorder(temp->root);
        temp = temp->next;
    }
}

int main()
{
    LIST * head = NULL;
    head = calloc(1, sizeof(LIST));
    add(&head);
    add(&head);
    write(head);
    clean_up(&head);
    return 0;
}
#包括
#包括
#包括
类型定义结构BST
{
国际信息;
结构BST*左,*右;
}英国理工学院;
类型定义结构列表
{
BST*根;
结构列表*下一步;
}名单;
BST*新节点(内部信息)
{
BST*new=calloc(1,sizeof(BST));
新建->信息=信息;
归还新的;
}
BST*添加节点(BST*根,整数信息)
{
if(root==NULL)返回新的_节点(info);
如果(infoinfo)root->left=添加_节点(root->left,info);
如果(信息>=根->信息)根->右=添加节点(根->右,信息);
}
列表*添加(列表**标题)
{
如果((*head)->root==NULL)
{
BST*BTreeNode=NULL;
BTreeNode=calloc(1,sizeof(BST));
(*head)->root=b绿色节点;
printf(“输入树根:\n”);
int n_根;
scanf(“%d”和n_根);
b绿色节点->信息=n\u根;
字符;
做
{
printf(“添加[A],退出[E]:”;
scanf(“\n%c,&字符);
如果(字符=='A')
{
printf(“输入数据:”);
内部温度;
scanf(“%d”、&temp);
添加_节点(BTreeNode,temp);
}
}while(字符!=“E”);
}
其他的
{
LIST*temp=NULL;
温度=*水头;
对于(;temp->next;temp=temp->next);
LIST*new=NULL;
new=calloc(1,sizeof(LIST));
临时->下一步=新建;
BST*BTreeNode=NULL;
BTreeNode=calloc(1,sizeof(BST));
新建->根=b重节点;
printf(“输入树根:\n”);
int n_根;
scanf(“%d”和n_根);
b绿色节点->信息=n\u根;
字符;
做
{
printf(“添加[A],退出[E]:”;
scanf(“\n%c,&字符);
如果(字符=='A')
{
printf(“输入数据:”);
内部温度;
scanf(“%d”、&temp);
添加_节点(BTreeNode,temp);
}
}while(字符!=“E”);
}
}
void delete_树(BST*根)
{
if(root!=NULL)
{
删除_树(根->左);
删除_树(根->右);
自由根;
}
}
无效清理(列表**标题)
{
while(*头)
{
LIST*temp=NULL;
温度=(*头部)->下一步;
删除_树((*头)->根);
自由(*头);
*压头=温度;
}
}
无效顺序(BST*根)
{
if(root!=NULL)
{
顺序(根->左);
printf(“%d”,根->信息);
顺序(根->右);
}
}
无效写入(列表*标题)
{
int计数器=1;
LIST*temp=NULL;
温度=水头;
while(临时)
{
printf(“\n%d.树的信息:”,计数器++);
顺序(临时->根);
温度=温度->下一步;
}
}
int main()
{
列表*head=NULL;
head=calloc(1,sizeof(列表));
添加(&head);
添加(&head);
写(头);
清理(头部和头部);
返回0;
}

当我将BST的根节点传递给函数add_节点并返回包含在循环中的节点时,我没有更改它。 这是一个工作代码

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

typedef struct BST
{
    int info;
    struct BST * left, * right;
}BST;

typedef struct LIST
{
    BST * root;
    struct LIST * next;
}LIST;

BST * new_node(int info)
{
    BST * new = calloc(1, sizeof(BST));
    new->info = info;
    return new;
}

BST * add_node(BST * root, int info)
{
    if(root == NULL) return new_node(info);
    if(info < root->info) root->left = add_node(root->left, info);
    else if(info >= root->info) root->right = add_node(root->right, info);
    return root;
}

LIST * add(LIST ** head)
{
    if((* head)->root == NULL)
    {
        BST * BTreeNode = NULL;
        BTreeNode = calloc(1, sizeof(BST));
        (* head)->root = BTreeNode;
        printf("Enter tree root: \n");
        int n_root;
        scanf("%d", &n_root);
        BTreeNode->info = n_root;
        char character;
        do
        {
            printf("Add [A], exit [E]: ");
            scanf("\n%c", &character);
            if(character == 'A')
            {
                printf("Enter data: ");
                int temp;
                scanf("%d", &temp);
                BTreeNode = add_node(BTreeNode, temp);
            }
        }while(character != 'E');
    }
    else
    {
        LIST * temp = NULL;
        temp = * head;
        for(; temp->next; temp = temp->next);
        LIST * new = NULL;
        new = calloc(1, sizeof(LIST));
        temp->next = new;

        BST * BTreeNode = NULL;
        BTreeNode = calloc(1, sizeof(BST));
        new->root = BTreeNode;
        printf("Enter tree root: \n");
        int n_root;
        scanf("%d", &n_root);
        BTreeNode->info = n_root;
        char character;
        do
        {
            printf("Add [A], exit [E]: ");
            scanf("\n%c", &character);
            if(character == 'A')
            {
                printf("Enter data: ");
                int temp;
                scanf("%d", &temp);
                BTreeNode = add_node(BTreeNode, temp);
            }
        }while(character != 'E');
    }
}

void delete_tree(BST * root)
{
    if(root != NULL)
    {
        delete_tree(root->left);
        delete_tree(root->right);
        free(root);
    }
}

void clean_up(LIST ** head)
{
    while(* head)
    {
        LIST * temp = NULL;
        temp = (* head)->next;
        delete_tree((* head)->root);
        free(* head);
        * head = temp;
    }
}

void inorder(BST * root)
{
    if(root != NULL)
    {
        inorder(root->left);
        printf("%d ", root->info);
        inorder(root->right);
    }
}

void write(LIST * head)
{
    int counter = 1;
    LIST * temp = NULL;
    temp = head;
    while(temp)
    {
        printf("\nInfo of %d. tree: ", counter++);
        inorder(temp->root);
        temp = temp->next;
    }
}

int main()
{
    LIST * head = NULL;
    head = calloc(1, sizeof(LIST));
    add(&head);
    add(&head);
    write(head);
    clean_up(&head);
    return 0;
}
#包括
#包括
#包括
类型定义结构BST
{
国际信息;
结构BST*左,*右;
}英国理工学院;
类型定义结构列表
{
BST*根;
结构列表*下一步;
}名单;
BST*新节点(内部信息)
{
BST*new=calloc(1,sizeof(BST));
新建->信息=信息;
归还新的;
}
BST*添加节点(BST*根,整数信息)
{
if(root==NULL)返回新的_节点(info);
如果(infoinfo)root->left=添加_节点(root->left,info);
如果(信息>=根->信息)根->右=添加节点(根->右,信息);
返回根;
}
列表*添加(列表**标题)
{
如果((*head)->root==NULL)
{
BST*BTreeNode=NULL;
BTreeNode=calloc(1,sizeof(BST));
(*head)->root=b绿色节点;
printf(“输入树根:\n”);
int n_根;
scanf(“%d”和n_根);
b绿色节点->信息=n\u根;
字符;
做
{
printf(“添加[A],退出[E]:”;
scanf(“\n%c,&字符);
如果(字符=='A')
{
printf(“输入数据:”);
内部温度;
scanf(“%d”、&temp);
BTreeNode=add_节点(BTreeNode,temp);
}
}while(字符!=“E”);
}
其他的
{
LIST*temp=NULL;
温度=*水头;
对于(;temp->next;temp=temp->next);
LIST*new=NULL;
new=calloc(1,sizeof(LIST));
临时->下一步=新建;
BST*BTreeNode=NULL;
BTreeNode=calloc(1,sizeof(BST));
新建->根=b重节点;
printf(“输入树根:\n”);
int n_根;
scanf(“%d”和n_根);
b绿色节点->信息=n\u根;
字符;
做
{
printf(“添加[A],退出[E]:”;
scanf(“\n%c,&字符);
如果(字符=='A')
{
printf(“输入数据:”);
内部温度;
scanf(“%d”、&temp);
BTreeNode=add_节点(BTreeNode,temp);
}
}而(c)