代码中的错误表示无法将 bstNode* 转换为 bstNode** 。我在这里使用双指针的概念和递归

Error in the code saying cannot convert bstNode* to bstNode** . I am using the concept of double pointer here alongwith recursion

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


     struct bstNode
     {
     int data;
     struct bstNode *left;
     struct bstNode *right;
     };



     struct bstNode* getNewNode(int data)
     {
     struct bstNode* newNode = (struct bstNode*)malloc(sizeof(struct bstNode));
      newNode->data = data;
     newNode->left = newNode->right = NULL;
     return newNode;
     }

     void Insert(struct bstNode** root, int data)
     {
        if(*root == NULL)
       {
           *root = getNewNode(data);
     }

     else
     if(data >= (*root)->data)
     {
         Insert((*root)->right, data);
     }
     else
     {
         Insert((*root)->left, data);
     }
 }

 void Search(struct bstNode** root,int data)
 {
     if(data == (*root)->data)
     {
         printf("Data Found");
         getchar();
         exit(0);
     }

     else
     if(data >= (*root)->data)
     {
         Search((*root)->left, data);
     }
     else
     {
         Search((*root)->right, data);
     }
 }

 int main()
 {
     struct bstNode* root = NULL;
     Insert(&root,12);
     Insert(&root,13);
     Insert(&root,1);
     Insert(&root,16);
     Insert(&root,8);
     Insert(&root,19);

     Search(&root,8);
     Search(&root,6);

     return 0;
 }

在上面的代码中,我试图将指针变量的地址从主函数传递给 Insert function.As 据我所知,这里它应该接受参数作为指向指针的指针,但它不是接受。问题是什么 ?请帮忙,以便我也可以更新我的知识。谢谢。

详细错误如下:

main.cpp: 在函数‘void Insert(bstNode**, int)’中: main.cpp:32:37: 错误:无法将参数“1”的“bstNode*”转换为“bstNode**”到“void Insert(bstNode**, int)” 插入((*根)->右,数据); ^

main.cpp:36:36: 错误:无法将参数“1”的“bstNode*”转换为“bstNode**”到“void Insert(bstNode**, int)” 插入((*根)->左,数据); ^ main.cpp: 在函数‘void Search(bstNode**, int)’中: main.cpp:52:36: 错误:无法将参数“1”的“bstNode*”转换为“bstNode**”到“void Search(bstNode**, int)” 搜索((*根)->左边,数据); ^

main.cpp:56:37: 错误:无法将参数“1”的“bstNode*”转换为“bstNode**”到“void Search(bstNode**, int)” 搜索((*root)->右,数据); ^

在你的函数的 if 语句中

 if(data >= (*root)->data)
 {
     Insert((*root)->right, data);
 }
 else
 {
     Insert((*root)->left, data);
 }

例如,表达式 (*root)->right 的类型为 struct bstNode*,但函数需要一个类型为 struct bstNode** 的参数。更改此语句和类似的语句,如

     Insert( &(*root)->right, data);

EXPLANATION

您在多个 InsertSearch 函数调用中错过了 (*root)->left(*root)->right 之前的 &。我已经修改了你的程序并添加了所需的 & 运算符地址。现在编译好了。

但是您发布的代码会导致分段错误,即使在纠正了类型错误之后也是如此。这是因为,正如我在评论中提到的,您的程序中存在多个 逻辑错误 。我在下面列出了它们:

  • 您没有在 Search 函数内指定递归的终止条件。递归搜索最终得到 Segmentation Fault.

  • 您在 else ifelse 中递归调用 Search 函数时使用了错误的参数。如果 data < *root -> data,则意味着当前元素 (*root -> data) 与您要查找的值相比更大,因此,您必须进一步查看左子树并跳过右子树-树。当data > *root -> data时情况正好相反。但是当 data < *root -> data 时你正在寻找正确的子树,反之亦然。这导致了不正确的搜索。

  • 虽然不是错误,但是当您在 BST 中找到搜索值时,您正在使用 exit(0)。这将立即终止程序,因此,如果 BST 中存在该值,则只能在主函数中使用一次 Search

  • 您没有消息表明 BST 中不存在该值。

我所做的修改包括:

  • Search函数内部递归查找的终止条件,即检查是*root == NULL

  • 交换递归 Search 调用的参数。

  • 添加一条消息以确定 BST 中何时不存在该值

  • 添加了用于检查 BST 中不存在的值的注释搜索

以下是经过修改的代码的最终工作版本。我建议您摆脱 exit(0) 并用其他机制替换它。

MODIFIED WORKING CODE

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

    struct bstNode
    {
        int data;
        struct bstNode *left;
        struct bstNode *right;
    };

    struct bstNode *getNewNode(int data)
    {
        struct bstNode *newNode = (struct bstNode *)malloc(sizeof(struct bstNode));
        newNode->data = data;
        newNode->left = newNode->right = NULL;
        return newNode;
    }

    void Insert(struct bstNode **root, int data)
    {
        if (*root == NULL)
        {
            *root = getNewNode(data);
        }

        else if (data >= (*root)->data)
        {
            Insert(&((*root)->right), data);
        }
        else
        {
            Insert(&((*root)->left), data);
        }
    }

    void Search(struct bstNode **root, int data)
    {
        if (*root != NULL)
        {
            if (data == (*root)->data)
            {
                printf("Data Found");
                getchar();
                exit(0);
            }

            else if (data > (*root)->data)
            {
                Search(&((*root)->right), data);
            }
            else
            {
                Search(&((*root)->left), data);
            }
        }
    }

    int main()
    {
        struct bstNode *root = NULL;
        Insert(&root, 12);
        Insert(&root, 13);
        Insert(&root, 1);
        Insert(&root, 16);
        Insert(&root, 8);
        Insert(&root, 19);

        Search(&root, 8);
        // Search(&root, 29);
        printf("Data Not Found");

        return 0;
    }

这是一个实现相同目标的可行解决方案,无需使用双重引用 (**)。

MY SOLUTION

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

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

struct node *createNode(value){
    struct node *newNode = malloc(sizeof(struct node));
    newNode->data = value;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}


struct node *insert(struct node *root, int data)
{
    if (root == NULL) return createNode(data);

    if (data < root->data)
        root->left  = insert(root->left, data);

    else if (data > root->data)
        root->right = insert(root->right, data);   

    return root;
}

void search(struct node *root, int data, int *found){
    if(root == NULL) return;

    search(root->left, data, found);

    if(root->data == data){
        *found = 1;
    } 

    search(root->right, data, found);
}

int main(){
    struct node *root = NULL;

    root = insert(root, 8);
    insert(root, 3);
    insert(root, 1);
    insert(root, 6);
    insert(root, 7);
    insert(root, 10);
    insert(root, 14);
    insert(root, 4);

    int found7 = 0, found9 = 0;
    search(root, 7, &found7);
    search(root, 9, &found9);

    found7 ? printf("7 found in BST\n") : printf("7 not found\n");

    found9 ? printf("9 found in BST\n") : printf("9 not found\n");

    return 0;
}