简体   繁体   English

指向根的二叉树指针需要被引用和取消引用。 为什么?

[英]Binary tree pointer to the root needs to be referenced and dereferenced. Why?

My question is why do I need to dereference and reference a pointer for the following code to work? 我的问题是为什么我需要取消引用并引用指针才能使以下代码正常工作? Doesn't ref/deref cancel each other? ref / deref不会互相抵消吗? I would really appreciate if anyone could explain it like I'm five :) 如果有人能像我五岁那样解释它,我将不胜感激。

Code: 码:

template <typename T> 
class binNode {
private:
    T key;
public:
    binNode * left;
    binNode * right;
    binNode * parent;
    binNode() {
        this->left = NULL;
        this->right = NULL;
        this->parent = NULL;
    }
    // arg constructor:
    binNode (T key) {
        this->key = key;
        this->left = NULL;
        this->right = NULL;
        this->parent = NULL;
    }

    T getKey() {
        return this->key;
    }
    void setKey(T key) {
        this->key = key;
    }
};

template<typename T> class Tree {
private:
    binNode <T> *root;
public:
    Tree() {
        this->root = NULL;
    }
    Tree(binNode <T> * node) {
        node->parent = NULL;
        this->root = node;
    }
    /* THIS IS THE PART I  DON'T GET */
    void addNode(binNode<T> *&x, binNode<T> * node) { // what's up with the *&???
        if (x == NULL) {
            x = node;
            return;
        } else if (x->getKey() == node->getKey()) {
            node->left = x;
            node->parent = x->parent;
            x->parent = node;
            return;
        }

        if (node->getKey() < x->getKey()) {
            addNode(x->left, node);
        } else {
            addNode(x->right, node);
        }

    }

    void addNode(binNode<T> * node) {
        addNode(this->root, node);
    }

    binNode<T> * treeSearch(binNode<T> * x, T key) {
        if (x == NULL || key == x->getKey()) {
            return x;
        }
        if (key < x->getKey()) {
            return treeSearch(x->left, key);
        } else {
            return treeSearch(x->right, key);
        }
    }

    void printOrdered() {
        inorderTreeWalk(root);
        cout << endl;
    }

    void inorderTreeWalk(binNode<T> * node) {
        if (node != NULL) {
            inorderTreeWalk(node->left);
            cout << node->getKey() << '\t';
            inorderTreeWalk(node->right);
        }
    }

};

Here is the main function ( #inlude is not included) 这是主要功能(不包括#inlude

int main() {
    Tree<int> T (new binNode<int>(10));
    // Tree<int> T = new binNode<int>(10);

    T.addNode(new binNode<int> (11));
    T.addNode(new binNode<int> (9));
    T.addNode(new binNode<int> (8));
    T.addNode(new binNode<int> (12));

    T.printOrdered();

}

That's not a reference / dereference of a pointer , it's a reference to a pointer. 这不是对指针的引用/取消引用,而是指针的引用。 It is necessary because... 这是必要的,因为...

void addNode(binNode<T> *&x, binNode<T> * node) {
    if (x == NULL) {
        x = node; // ...here...
        return;
    } else // ...

...you are assigning to the parameter x . ...您正在分配给参数x

If you hadn't passed the pointer x by reference , you would assign to the local copy of the parameter: 如果尚未通过引用传递指针x ,则应将其分配给参数的本地副本:

void addNode(binNode<T> * x, binNode<T> * node) {
    if (x == NULL) {
        x = node; // this acts on the local copy only, and thus does nothing.
        return;
    } else // ...

Via the pointer (without the reference), you get a local copy of the address. 通过指针(没有引用),您可以获得地址的本地副本。 Which means you can manipulate the value behind the pointer (in this case *x) which would change. 这意味着您可以操纵将更改的指针(在本例中为* x)后面的值。 But if you change the address itself, the address would behave like a local copy and you lose the address-changes after leaving the method. 但是,如果您更改地址本身,则该地址的行为将类似于本地副本,并且在退出方法后会丢失地址更改。

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

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