简体   繁体   中英

Returning pointer from function and calling function with pointer recursively - BST

Recently I started to write BST implementation and I'm still struggling with pointers. First of my problem is that I try to find 'predecessor' of given node with function, which given pointer of given node as a argument returns pointer to the predecessor of given node. I tried to think logically with calling a function and giving argument, but I'm stuck.

First of all, I've declared 'root' node in 'main' like this: tree *root; , then I call a function from which I call a function findPredecessor like this: del(&root, k1); , here's a declaration of a del function: void del(tree **root, int k) then I use 'iterator' with which I go through whole BST tree declared like this: tree *w = NULL . Finally here's how I call findPredecessor function (inside del function): tree *predecessor = findPredecessor(w); and I get an error C2040 'FindPredecessor': 'tree *(tree *)' differs in levels of indirection from 'int ()' , here's a body of a findPredecessor function:

tree* findPredecessor(tree *w) {
w = w->left;
while (w->right != NULL) {
    w = w->right;
}
return w;}

I'm also strugling with deallocating memory from whole tree, here's a body a function delAll:

void delAll(tree **root) {
    if (*root == NULL) {
        return;
    }
    delAll(&((*root)->left));
    delAll(&((*root)->right));
    free(*root);
}

,which I'm calling from 'main' same manner as I do with del . I know my problem is some kind references/pointers related, I would be thankful for help. Sorry for my english.

I think you are calling findPredecessor() before declaring it. Just add a forward declaration. Or you can rearrange your code such that findPredecessor() is implemented before del().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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