BSTree添加功能

时间:2017-11-22 11:10:51

标签: c++

我正在尝试在BSTree中执行添加功能。但是,我不知道从哪里开始。任何人都可以给我一些提示,我需要先考虑一下。我不知道新的对在哪里(键,值);使用什么是使用Tree :: data;。我只想得到提示,但不是答案。谢谢。

以下是Pair.h文件

#ifndef PAIR_H_
#define PAIR_H_

//a key-value pair
//this simple class is pretty much self-explanatory
//all its implementation is already given
template<typename KeyType, typename ValueType>
class Pair
{
public:
    Pair(KeyType k, ValueType v) :
            key(k), value(v)
    {
    }

    KeyType getKey() const
    {
        return key;
    }

    void setKey(KeyType key)
    {
        this->key = key;
    }

    ValueType getValue() const
    {
        return value;
    }

    void setValue(ValueType value)
    {
        this->value = value;
    }

private:
    KeyType key;
    ValueType value;
};

#endif /* PAIR_H_ */

以下是Tree.h

template<typename Container, typename KeyType, typename ValueType>
class Tree
{
public:
    virtual bool add(KeyType key, ValueType value) = 0; //pure virtual     function here, description can be found in BSTree class
    virtual bool remove(KeyType key) = 0; //pure virtual function here, description can be found in BSTree class
    virtual ValueType getValue(KeyType key) = 0; //pure virtual function here, description can be found in BSTree class
    virtual ~Tree(){}; //an empty destructor

protected:
    Container data; //the data container (a vector, which is either a ListVector or ArrayVector in this assignment)
                    //that stores all the node data of the tree
                    //how the node is stored in the data container is very specific
                    //please refer to the webpage description for details

    //treeToString in Utility is this class's only friend... :(
    template<typename aContainer, typename aKeyType, typename aValueType>
    friend string treeToString(Tree<aContainer, aKeyType, aValueType>* tree);
};

#endif

以下是BSTree.h文件

#ifndef BSTREE_H_
#define BSTREE_H_

#include "Tree.h"
#include "Pair.h"
#include <typeinfo>
using namespace std;

//the Binary Search Tree class
//the keys have to be unique in the tree - you need to make sure of that when performing the insertion
template<typename Container, typename KeyType, typename ValueType>
class BSTree : public Tree<Container, KeyType, ValueType>
{
public:
BSTree(){};
~BSTree();
//add a node to the tree, according to the given key and value
//you have to use the exact algorithm described in the lecture notes
//so that you will have the exact same result as our demo and sample output
//it should do nothing to the tree and return false when there is already a node that has the same key
//otherwise, it should add the node and return true
//hint: you probably will have a statement to allocate the space for the new pair, like so: "new Pair<KeyType, ValueType>(key, value);"
bool add(KeyType key, ValueType value);
bool remove(KeyType key);
ValueType getValue(KeyType key);
int getHeight();
using Tree<Container, KeyType, ValueType>::data;
};
#include "BSTree.tpp"

#endif /* BSTREE_H_ */

0 个答案:

没有答案