Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 树形加法函数_C++ - Fatal编程技术网

C++ 树形加法函数

C++ 树形加法函数,c++,C++,我正在尝试在BSTree中执行添加功能。但是,我不知道从哪里开始。谁能给我一些提示,我需要先考虑一下。我不知道新的一对(键、值)在哪里;要使用以及使用什么树::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

我正在尝试在BSTree中执行添加功能。但是,我不知道从哪里开始。谁能给我一些提示,我需要先考虑一下。我不知道新的一对(键、值)在哪里;要使用以及使用什么树::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_ */
\ifndef对_
#定义配对_
//键值对
//这个简单的类几乎是不言自明的
//它的所有实现都已经给出
模板
类对
{
公众:
成对(键类型k,值类型v):
键(k),值(v)
{
}
KeyType getKey()常量
{
返回键;
}
无效设置键(键类型键)
{
这个->键=键;
}
ValueType getValue()常量
{
返回值;
}
无效设置值(ValueType值)
{
这个->值=值;
}
私人:
键型键;
值类型值;
};
#endif/*配对*/
下面是树

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
模板
类树
{
公众:
virtual bool add(KeyType key,ValueType value)=0;//此处为纯虚函数,说明见BSTree类
virtual bool remove(KeyType key)=0;//此处为纯虚函数,说明见BSTree类
virtual ValueType getValue(KeyType key)=0;//此处为纯虚函数,说明见BSTree类
virtual~Tree(){};//空析构函数
受保护的:
容器数据;//数据容器(一个向量,在此赋值中为ListVector或ArrayVector)
//它存储树的所有节点数据
//节点在数据容器中的存储方式非常具体
//详情请参阅网页说明
//实用程序中的treeToString是该类唯一的朋友…:(
模板
好友字符串treeToString(Tree*Tree);
};
#恩迪夫
下面是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_ */
\ifndef bTree\u H_
#定义BSTREE_H_
#包括“Tree.h”
#包括“Pair.h”
#包括
使用名称空间std;
//二叉搜索树类
//键在树中必须是唯一的-执行插入时需要确保这一点
模板
类BSTree:公共树
{
公众:
BSTree(){};
~BSTree();
//根据给定的键和值向树中添加节点
//你必须使用课堂讲稿中描述的精确算法
//因此,您将获得与我们的演示和示例输出完全相同的结果
//当已经存在具有相同密钥的节点时,它不应对树执行任何操作,并返回false
//否则,它应该添加节点并返回true
//提示:您可能会有一个语句来为新对分配空间,如:“newpair(key,value);”
bool add(键类型键、值类型值);
bool-remove(键型键);
ValueType获取值(KeyType键);
int getHeight();
使用Tree::data;
};
#包括“BSTree.tpp”
#endif/*BSTREE_H_*/

一个合理的开始位置似乎是“课堂讲稿中描述的精确算法”。不要忘记“请参考网页说明了解详细信息”。在讲稿中,大多数示例都有根、左和右,但这没有根,因此我不知道如何开始。“网页说明”应该包含关于如何在顺序容器中编码这些内容的信息。(您很可能会使用索引作为“指针”。)好的,现在我知道如何做了。谢谢!