简体   繁体   English

无法获得指向节点的指针

[英]Can't get pointer to point to a node

I am trying to create my own Binary Tree data structure. 我正在尝试创建自己的二叉树数据结构。

I am using a BinaryNode<T> class as the leaves with a BinaryNode<T>* root to keep track of the root node. 我使用BinaryNode<T>类作为具有BinaryNode<T>*根的叶子,以跟踪根节点。

template<class T>
class PPBinaryTree {
private:
    BinaryNode<T>* root;

public:
    PPBinaryTree();

    bool add(T Data, BinaryNode<T>* Root); 

    T* search(T Data, BinaryNode<T>* Root);  
};  

Here is the implementation of the add function. 这是add函数的实现。 No matter what I try I cannot create any new branches. 无论我尝试什么,都无法创建任何新分支。 The RootProbe seems to be a copy of the branch pointers so when I assign Data to it nothing changes in the branch. RootProbe似乎是分支指针的副本,因此当我为其分配Data时,分支中没有任何变化。

The only solution I can think of is to change the line 我能想到的唯一解决方案是更改生产线

RootProbe = (new BinaryNode<T>(Data));

to

*RootProbe = *(new BinaryNode<T>(Data)); 

but that just makes my program crash with an unhandled exception. 但这只会使我的程序因未处理的异常而崩溃。

template<class T>
bool PPBinaryTree<T>::add(T Data, BinaryNode<T>* Root){
    bool isSuccessful = false;
    BinaryNode<T>* RootProbe;

    if(Root == NULL){
        Root = new BinaryNode<T>(Data);
        this->setRoot(Root);
        isSuccessful = true;
        return isSuccessful;
    }
    else{
        while(RootProbe != NULL){
            if(Data > Root->getData())
                RootProbe = Root->getRightBranch();
            else
                RootProbe = Root->getRightBranch();
        }
        RootProbe = (new BinaryNode<T>(Data));

        isSuccessful = true;
        return isSuccessful;
    }
}

Here is the BinaryNode class: 这是BinaryNode类:

template<class S>
class BinaryNode {
private:
    S data;
    BinaryNode *leftBranch;
    BinaryNode *rightBranch;

public:
    BinaryNode(S data);

    void setData(S data);

    S getData( );

    void setLeftBranch(BinaryNode newNode);

    BinaryNode* getLeftBranch( );

    void setRightBranch(BinaryNode newNode);

    BinaryNode* getRightBranch( );
};

I can guess that: 我可以猜测:

while(RootProbe != NULL){
    if(Data > Root->getData())
        RootProbe = Root->getRightBranch();
    else
        RootProbe = Root->getRightBranch();
}
RootProbe = (new BinaryNode<T>(Data));

Have to be changed, to initialize RootProbe from right and left branches. 必须改变,从右侧和左侧分支初始化RootProbe。 Also you forgot to set your RootProbe somewhere in the tree. 您也忘记将RootProbe设置在树中的某个位置。

BinaryNode<T>* LastLiveProbe;
while(RootProbe != NULL){
    if(Data > Root->getData())
    {
        RootProbe = Root->getLeftBranch();
        if (RootProbe == NULL)
            LastLiveProbe->setLeftBranch(new BinaryNode<T>(Data));
        else
            LastLiveProbe = RootProbe;
    }
    else
    {
        RootProbe = Root->getRightBranch();
        if (RootProbe == NULL)
            LastLiveProbe->setRightBranch(new BinaryNode<T>(Data));
        else
            LastLiveProbe = RootProbe;
    }
}

If you don't have other problems, that should help. 如果您没有其他问题,那应该会有所帮助。

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

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