【问题标题】:Search function not working in C program to perform functions on a Binary Tree搜索功能在 C 程序中无法在二叉树上执行功能
【发布时间】:2021-05-09 08:22:04
【问题描述】:

我一直试图让这个搜索功能工作,但它说即使元素在树中也找不到元素。其他所有功能都有效。

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

struct node
{
        struct node *lchild;
        int info;
        struct node *rchild;
};
int flag;
struct node *root = NULL;
struct node *insert(struct node *ptr, int ikey);
void in_order_search(struct node *ptr, int val);
void display(struct node *ptr,int level);

int main( )
{
        struct node *root=NULL,*ptr;
        int choice,k,v;

        while(1)
        {
                printf("\n");
                printf("1.Insert\n");
                printf("2.Display\n");
                printf("3.Search\n");
                printf("\nEnter your choice : ");
                scanf("%d",&choice);

                switch(choice)
                {
                case 1:
                        printf("\nEnter the key to be inserted : ");
                        scanf("%d",&k);
                        root = insert(root, k);
                        break;

                case 2:
                        printf("\n");
                        display(root,0);
                        printf("\n");
                        break;

                case 3:
                        printf("\nEnter the key to be searched : ");
                        scanf("%d",&v);
                        in_order_search(root, v);
                        if (flag==1)
                        {
                            printf("Element present in the binary tree\n");
                        }
                        else
                        {
                            printf("Element not present in the binary tree\n");
                        }
                        break;
                 default:
                        exit(1);
                }
        }

        return 0;

}


struct node *insert(struct node *ptr, int ikey )
{
        if(ptr==NULL)
        {
                ptr = (struct node *) malloc(sizeof(struct node));
                ptr->info = ikey;
                ptr->lchild = NULL;
                ptr->rchild = NULL;
        }
        else if(ikey < ptr->info) /*Insertion in left subtree*/
                ptr->lchild = insert(ptr->lchild, ikey);
        else if(ikey > ptr->info) /*Insertion in right subtree */
                ptr->rchild = insert(ptr->rchild, ikey);
        else
                printf("\nDuplicate key\n");
        return ptr;
}

void in_order_search(struct node *ptr, int val)
{
    if (!ptr)
    {
        return;
    }
    in_order_search(ptr->lchild, val);
    if(ptr->lchild == val)
    {
        printf("\nElement present in the binary tree.\n");
        flag = 1;
    }
    in_order_search(ptr->rchild, val);


}

void display(struct node *ptr,int level)
{
        int i;
        if(ptr == NULL )
                return;
        else
    {
                display(ptr->rchild, level+1);
                printf("\n");
                for (i=0; i<level; i++)
                        printf("    ");
                printf("%d", ptr->info);
                display(ptr->lchild, level+1);
        }
}

6 8 7 9 3 4 2

这是我输入的树,我搜索其中的所有元素,但它仍然显示元素不存在于二叉树中。我一直在尝试将此搜索功能集成到我在互联网上找到的另一个代码中,如果它看起来很乱,请原谅我。而且我也是初学者,所以如果我不确定这是否是一个愚蠢的错误。

【问题讨论】:

  • if(ptr-&gt;val == value) 那甚至无法编译。 value 不是定义的变量,val 不是结构中的字段。请出示实际的真实代码。
  • ptr-&gt;lchild == val 应该是 ptr-&gt;info == val 然后它将起作用并且不会导致段错误@SwaggerLagger10

标签: c struct tree binary


【解决方案1】:

这是我所做的更改及其工作:

void in_order_search(struct node *ptr, int val)
{
    if (!ptr)
    {
        return;
    }
    in_order_search(ptr->lchild, val);
    if(ptr->info == val)
    {
/*         printf("\nElement present in the binary tree.\n"); to prevent double printing
 */        flag = 1;
            return;/* no need to go to the next line*/
    }
    in_order_search(ptr->rchild, val);
}

另外,注意线路引起的seg错误:

ptr->lchild == val

这是坏线

if(ptr->info == val)

这是更正

【讨论】:

  • @SwaggerLagger10 检查此注释(在评论中)
猜你喜欢
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 2020-11-07
相关资源
最近更新 更多