C++ 将BST制作成模板BST

C++ 将BST制作成模板BST,c++,templates,binary-search-tree,C++,Templates,Binary Search Tree,我制作了这个二叉搜索树,现在我得到了一个任务,使用一个模板将它制作成一个BST,然后我应该在main.cpp中测试它。在之前的任务中,我应该对浮点值进行排序。现在我应该用一个模板对所有数据类型进行排序 我不确定如何实现该模板,它们让我有点困惑,我这周才了解它们,所以我希望有人能纠正我 我得到的提示是:模板只能用于 运算符(例如追加(0); NewTree->append(-1); NewTree->append(-5); NewTree->append(-1); NewTree->append(

我制作了这个二叉搜索树,现在我得到了一个任务,使用一个模板将它制作成一个BST,然后我应该在main.cpp中测试它。在之前的任务中,我应该对浮点值进行排序。现在我应该用一个模板对所有数据类型进行排序

我不确定如何实现该模板,它们让我有点困惑,我这周才了解它们,所以我希望有人能纠正我

我得到的提示是:模板只能用于 运算符(例如追加(0); NewTree->append(-1); NewTree->append(-5); NewTree->append(-1); NewTree->append(6); NewTree->append(-3);
cout在声明BST的对象时使用此语法
BST*Left
并且当您必须为该指针分配内存时
Left=new BST()
。一个明显的错误是
Value T;
。在非模板方法中,它是
float Value;
。那么,为什么不
T Value;
?请不要忘记,模板必须仅作为标题编写。(编译器必须能够始终看到实现,因为模板不是现成的代码,而只是编译器在创建模板实例时要最终确定的代码模板,即使用具有具体类型的模板。)@Talal02@Schleff谢谢你,我会考虑这些的
 class BST
{

private:
    float Value; //float value
    unsigned Counter; //counter
    BST* Left; //pointer left tree
    BST* Right; //pointer right tree
public:
    BST(); //constructor without values
    BST(float w,unsigned z); //constructor with values (value w , counter z)
    ~BST(); //destructor
    BST(const BST&) = delete;
    BST operator=(const BST&) =delete; //no copy constructor

    void incCounter();  //counter
    void append(float w); //appends
    void through(); //go through
    BST* search(float w); //search


};
#include "BST.h"


using namespace std;

BST::BST(): Value(0), Counter(1), Left(nullptr), Right(nullptr)                              //contructor with empty values
{
    cout << "Empty Object made\n" << endl;
}

BST::BST(float w, unsigned z): Value(w), Counter(z), Left(nullptr), Right(nullptr)           //contructor with values
{
    cout << "Object made\n" << endl;
}

BST::~BST()                                                                                   //destructor left right 
{
    delete Left;
    delete Right;
    cout << "Objects Left and Right deleted\n" << endl;
}

void BST::incCounter()
{
    Counter++;           //counter up 1
}

void BST::append(float w)                                                                       //appends value w
{
    if (w == Value)                   //if w=Value counter goes up
    { incCounter(); }
    else if (w < Value)               //if w smaller than value
    {
        if(Left != nullptr)         //if Left has a value 
        {
            Left ->append(w);    //Left goes back to function recursively and gets added to w
            }
            else
                {
                    BST* Neww = new BST(w,1);  //else a new element is created
                    Left = Neww;                        //added to Left
                    } }

        else if (w > Value) //same thing with right side
                {
                    {
                        if(Right != nullptr)
                {
                    Right -> append(w);
                    }
                    else
                        {
                            BST* Neww = new BST(w,1);
                            Right = Neww;
                    }
                }
            }
}





void BST::through()        //sorts Value and prints out the amount of the same numbers/values
{

    if(Left != nullptr) {                     //if Left exists
        Left -> through();                 //it goes through left tree as long theres still a value
    }

    cout << Value << "\t\t" << Counter << endl;    //prints out Value and Counter

    if(Right != nullptr)
    {
        Right -> through();
    }

}

BST* BST::search(float w)
{
    if (w == Value)               //if w=Value then it gives back Value
    {
        return this;
    }
    else if(w < Value)             //else if w smaller than value
    {
        if (Left != nullptr)     //if left points on something
        {
            Left -> search(w);   //then w gets searched, recursive
        }
    }
    else if(w > Value)             // else if w bigger than valueansonsten wenn w größer als Value
    {
        if (Right != nullptr)    //if Right point on something
        {
            Right -> search(w);  //function gets called recursively to search right
        }
    }
    else
    {
        return nullptr; //else return nullpointer
    }
}
#include "BST.h"
#include <iostream>


using namespace std;

int main()
{
    BST* NewTree = new BST();    //constructs new BST that points to NewTree
    BST* test      = nullptr;         //initilize empty pointer
    float input=0;


    NewTree->append(4); //add new numbers to tree (pointer_name)->(variable_name)
    NewTree->append(5);
    NewTree->append(0);
    NewTree->append(-1);
    NewTree->append(-5);
    NewTree->append(-1);
    NewTree->append(6);
    NewTree->append(-3);

    cout << "\ntest operation through:" << endl;
    NewTree->through(); //goes through new tree
    cout <<"\n"<<endl;

    cout << "test operation counter:" << endl;
    NewTree->incZaehler();
    NewTree->through();
    cout <<"\n"<<endl;

    cout <<"Type in a number: "<<endl;
    cin>>input;


    cout << "\ntest operation search:" << endl;
    test = NewTree->search(input);  //shows path after input value
    test->through();
    cout <<"\n"<<endl;

    delete(NewTree);

    return 0;
}
    template<typename T>
class BST {
private:
    Value T;
    unsigned Counter;
    BST* Left; 
    BST* Right;

public:
    BST();
    BST(T,unsigned z); 
    ~BST(); 
    BST(const BST&) = delete;
    BST operator=(const BST&) =delete;

    void incCounter();  
    void append(T);
    void through();
    BST* search(T);

};