尝试用C语言制作二叉搜索树,但我无法对其进行分支

问题描述 投票:0回答:1

每当插入新节点时,它只是用 NewNode 替换高度 2 处的相同节点,而不是分支并将 NewNode 指向 Null 值分支。

问题出现在第85行。

输出: 输入第一个节点的数据:10 输入节点数据:7

  1. 7 L

10 您想继续吗:1 输入节点数据:6

  1. 6 L

10 您想继续吗:1 输入节点数据:11

  1. 11 R

10 您想继续吗:0

它应该以这种方式工作: 头节点=10 第一个节点=7 L 温度=10

第二个节点=6 L L 温度=7

第三个节点=11 右 温度=10

意味着所有更改都发生在头部左右节点,而不是按预期分支。

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

struct node
{
    int data;
    struct node *left;
    struct node *right;
}*head;

void createtrees();
void treversetrees();
void addvalue(struct node *temp,struct node *NewNode);
void searchtree(int S,struct node *temp);

void main()
{
    int S;
    createtrees();
    printf("\nData in Trees is\n");
    //treversetrees();
    printf("Enter the integer to search for: ");
    scanf("%d",&S);
    struct node *temp;
    temp=head;
    searchtree(S,temp);
}

void createtrees()
{
    struct node *NewNode,*temp;
    int data,answer=1;
    head=(struct node*)malloc(sizeof(struct node));
    
    if (head==NULL)
    {
        printf("Unable to allocate memory.");
        exit(0);
    }
    
    printf("Enter the data of first node: ");
    scanf("%d",&data);
    
    head->data=data;

    do
    {
        NewNode=(struct node*)malloc(sizeof(struct node));
        if (NewNode==NULL)
        {
            printf("Unable to allocate memory.");\
            break;
        }
        printf("Enter the data of node: ");
        scanf("%d",&data);
        printf("\n1. %d\n",data);
        
        NewNode->data=data;
        temp=head;
        
        // printf("2\n");
        if (data<temp->data)
        {
            // printf("3.01\n");
            addvalue(temp,NewNode);
            // printf("3.1\n");
        }
        else
        {
            // printf("3.02\n");
            addvalue(temp,NewNode);
            // printf("3.2\n");
        }
        printf("Do you want to continue: ");
        scanf("%d",&answer);
    }
    while(answer==1);
}

void addvalue(struct node *temp,struct node *NewNode)
{
    // printf("4\n");
    if (temp==NULL)
    {
        temp=NewNode;
    }
    else if (NewNode->data<temp->data)
    {
        printf("L\n");
        printf("\n%d\n",temp->data);
        addvalue(temp->left,NewNode);
    }
    else
    {
        printf("R\n");
        printf("\n%d\n",temp->data);
        addvalue(temp->right,NewNode);
    }
}

void searchtree(int S,struct node *temp)
{
    if (temp->data==S)
    {
        printf("%d is found in tree",S);
        return;
    }
    else if(temp->data>S)
    {
        temp=temp->right;
        searchtree(S,temp);
    }
    else if(temp->data<S)
    {
        temp=temp->left;
        searchtree(S,temp);
    }
    else if(temp==NULL)
    {
        printf("%d is not found in tree",S);
    }
    
}
c pointers data-structures binary-search-tree
1个回答
0
投票

这里有一些可以帮助您入门的功能:

struct node *createnode(int data)
{
    struct node *node = malloc(sizeof(*node));

    if (node == NULL)
    {
        printf("Unable to allocate memory.");
        exit(0);
    }
    node->data = data;
    node->left = NULL;
    node->right = NULL;
}
void addvalue(struct node **temp,struct node *NewNode)
{
    // printf("4\n");
    if ((*temp)==NULL)
    {
        (*temp)=NewNode;
    }
    else if (NewNode->data<(*temp)->data)
    {
        printf("L\n");
        printf("\n%d\n",(*temp)->data);
        addvalue(&(*temp)->left,NewNode);
    }
    else
    {
        printf("R\n");
        printf("\n%d\n",(*temp)->data);
        addvalue(&(*temp)->right,NewNode);
    }
}

您应该能够自己弄清楚其余的事情,但您不需要为第一个节点做任何特殊的事情。只需将

head
初始化为
NULL
并为每个节点调用
NewNode = createnode(data);
addvalue(&head, NewNode);

searchtree()
中存在空指针取消引用错误,但您可以 通过更改测试顺序来修复它。

© www.soinside.com 2019 - 2024. All rights reserved.