简体   繁体   English

使用指针的二叉搜索树问题

[英]Binary Search Tree using pointers to pointers problem

I'm relatively new to coding in C++.我对 C++ 中的编码比较陌生。 I'm trying to make a BST in C++ and I'm using pointers for this but I can't figure out the error in pointers even after spending hours of time.我正在尝试在 C++ 中创建 BST,并且为此使用了指针,但即使花费数小时后我也无法弄清楚指针中的错误。 Basically, the error comes in the if statement, where I'm trying to use recursion to change root when there are children present from parent root.基本上,错误出现在 if 语句中,当父根存在孩子时,我试图使用递归来更改根。
Here is the code这是代码

#include<iostream>
using namespace std;

struct node{
    struct node* rightchild;
    int data;
    struct node* leftchild;
};

struct node* newNode(int data){
    struct node* node1 = (node*)malloc(sizeof(node));
    (*node1).data=data;
    (*node1).leftchild = NULL;
    (*node1).rightchild = NULL;
    return node1;
}

void insertIntoBST(struct node** ptrtoroot, struct node** ptrtotemp){
    if((**ptrtotemp).data <= (**ptrtoroot).data){
        if((**ptrtoroot).leftchild != NULL){
            insertIntoBST((*ptrtoroot).leftchild,(*ptrtotemp));
        }
        else{
           (*ptrtoroot->leftchild) = *ptrtotemp;
        }
    }
     if((**ptrtotemp).data > (**ptrtoroot).data){
        if((**ptrtoroot).rightchild != NULL){
            insertIntoBST((*ptrtoroot->rightchild),(*ptrtotemp));
        }
        else{
           (*ptrtoroot->rightchild) = *ptrtotemp;
        }
    }
    
}

void inorder(struct node* root){
    while(root != NULL){
        cout<<(*root).data;
        inorder((*root).leftchild);
        inorder((*root).rightchild);
    }
}

int main(){
    struct node* root = NULL;
    struct node* temp;
    int dat;
    for(int i = 0 ; i < 6 ; i++){
    cin>>dat;
    temp = newNode(dat);
    if(root == NULL){
        root = temp;
    }
    else{
        insertIntoBST(&root,&temp);
    }
 }
 inorder(root);

    return 0;
}

Error code:错误代码:

bst.cpp:21:40: error: request for member ‘leftchild’ in ‘* ptrtoroot’, which is of pointer type ‘node*’ (maybe you meant to use ‘->’ ?)  
21 | insertIntoBST((ptrtoroot).leftchild,(*ptrtotemp));  
   | ^~~~~~~~~ bst.cpp:24:25: error: request for member ‘leftchild’ in ‘ ptrtoroot’, which is of pointer type ‘node*’ (maybe you meant to use ‘->’ ?)  
24 | (*ptrtoroot->leftchild) = *ptrtotemp;  

Okay by looks of it I think in your function as shown below.好的,我认为在您的 function 中,如下所示。

void insertIntoBST(struct node** ptrtoroot, struct node** ptrtotemp){
    if((**ptrtotemp).data <= (**ptrtoroot).data){
        if((**ptrtoroot).leftchild != NULL){
            insertIntoBST((*ptrtoroot).leftchild,(*ptrtotemp));

ptrtoroot is double pointer ie pointer to pointer so this makes (*ptrtoroot) a pointer and here ptrtoroot 是双指针,即指向指针的指针,因此这使得 (*ptrtoroot) 成为指针,并且在这里

(*ptrtoroot).leftchild should be replaced with "->" operator 
like (*ptrtoroot)->leftchild

Secondly under recursive calls insertIntoBST function takes double pointer as argument so you need to pass address of pointer here.其次在递归调用下 insertIntoBST function 将双指针作为参数,因此您需要在此处传递指针的地址。

ie Address of a pointer returned by (*ptrtoroot)->leftchild from your node structure.即 (*ptrtoroot)->leftchild 从您的节点结构返回的指针的地址。

something like &((*ptrtoroot)->leftchild)

I have not completely executed your code but this should work.我还没有完全执行你的代码,但这应该可以。

Same goes for the second parameter (*ptrtotemp).第二个参数 (*ptrtotemp) 也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM