如何将const转换为非const

时间:2014-11-15 05:45:10

标签: c++ data-structures

我已经获得了以下函数来编写

T getMinimum() const {}

我必须使用以下帮助函数

void getMinimumHelper(Node * subtree, Node * &Location) const {}

但是,我从来不知道如何传递这样的函数。我有一个二进制搜索树类,节点作为BST类的成员。我尝试过很多东西,比如

Node * minNode = this->Node;
Node minNode = this->getMinimumHelper(findMin, findMin);
return minNode->data;

辅助功能:

void getMinimumHelper(Node * subtree, Node * &Location) const {
    if (subtree == NULL){
        Location = NULL;
    }
    if (subtree->left == NULL){
        Location = subtree;
    }
    else{
        getMinimumHelper(subtreeRoot->left, subtree);
    }
}

然而,我作为' - >'的右侧是非法的 当然,辅助函数无论出于何种原因都是空的,所以它实际上并没有返回任何东西。我已经工作了好几个小时,并没有取得任何进展,也无法解决这个问题。我有更多这样的辅助函数函数,我不知道该怎么做。

类别:

template <class T>
class  {
private:
    class Node {
    public:
        T data;
        Node * left;
        Node * right;
        Node * parent;

        Node() :left(NULL), right(NULL), parent(NULL) {};
        Node(const T& item) {
            data = item;
            left = NULL;
            right = NULL;
            parent = NULL;
        };
    };
public:
    BST();
    BST(BST&);

    ~BST();

    bool isEmpty() const;
    bool search(const T&) const;
private:
    Node * _root;

    void getMaximumHelper(Node *, Node * &) const;
    void getMinimumHelper(Node *, Node * &) const;
};

1 个答案:

答案 0 :(得分:0)

template <class T>
T BinarySearchTree<T>::getMinimum() const {
    Node * minNode;
    getMinimumHelper(_root, minNode);
    return minNode->data;
};
相关问题