c ++模板二进制搜索树

时间:2017-10-02 00:49:47

标签: c++ c++11 templates binary-search-tree

我想要的是一个相当简单的二叉搜索树,通过模板标签,允许在其中使用任何数字数据,但我有一些相当讨厌的问题,我不知道如何得到摆脱,如果有人可以帮助,将不胜感激。不断弹出的错误信息是"无效使用模板名称' BST'没有参数列表" - 坦率地说,我不知道如何解决它。它出现在bst.cpp文件中的第31,89,105,120,130,141行:

Main.cpp的

#include <iostream>
#include "bst.h"

using namespace std;

int main()
{

BST <int> tree;
tree.insert(8);
tree.insert(25);
tree.insert(99);
tree.insert(20);
tree.insert(25);
tree.insert(20);
tree.insert(2);
tree.insert(89);
tree.insert(15);
tree.insert(10);
tree.insert(30);
tree.insert(50);
tree.displayorder();



int number;

int Inputnumber;
while (true){
    cout << "Choose what you want to do: " << endl << "1# Insert" << endl << "2# Display Orders" << endl <<  "3# Search" << endl << "4# Delete" << endl << endl << endl;
    cin >> Inputnumber;
    if (Inputnumber==1){
        cout << endl << "Enter the number you want inserted: ";
        cin >> number;
        tree.insert(number);
        cout << endl << endl << endl;
    }
    if (Inputnumber==2){
        cout<<"Display Orders: " << endl;
        tree.displayorder();
        cout << endl << endl << endl;
    }

    if (Inputnumber==3){
        cout<<"Enter the number you want to search for: ";
        cin >> number;
        tree.search(number);
        cout << endl << endl << endl;
    }
    if (Inputnumber==4){
        cout << "Enter the number you want to remove: ";
        cin >> number;
        tree.remove(number);
        cout << endl << endl << endl;
    }
}
}

bst.h

#ifndef BST_H
#define BST_H


template <class T>
class BST
{
struct node
{
    T data;
    node* left;
    node* right;
};

node* root;

node* makeEmpty(node* tree);

node* insert(T x, node* tree);

node* findMin(node* tree);

node* findMax(node* tree);

node* remove(T x, node* tree);

void inorder(node* tree);

void preorder(node* tree);



void postorder(node* tree);


public:
BST();

~BST();


node* find(node* tree, T x);

void insert(T x);

void remove(T x);

void displayorder();

void search(T x);
};

#endif // BST_H

bst.cpp

#include <iostream>
#include "bst.h"

using namespace std;

template <class T>
void BST<T>::preorder(node* tree)
{
    if(tree == NULL){
        return;
    }
    cout << tree->data << " ";
    inorder(tree->left);
    inorder(tree->right);
}



template <class T>
void BST<T>::postorder(node* tree)
{
    if(tree == NULL){
        return;
    }
    inorder(tree->left);
    inorder(tree->right);
    cout << tree->data << " ";
}

template <typename T>
BST::node* BST<T>::find(node* tree, T x)        //AN ERROR OCCURS HERE
{
    if(tree == NULL)
        return NULL;
    else if(x < tree->data)
        return find(tree->left, x);
    else if(x > tree->data)
        return find(tree->right, x);
    else
        return tree;
}
template <typename T>
BST<T>::BST()
{
    root = NULL;
}

template <typename T>
BST<T>::~BST()
{
    root = makeEmpty(root);
}

template <class T>
void BST<T>::insert(T x)
{
    root = insert(x, root);
}

template <class T>
void BST<T>::remove(T x)
{
    root = remove(x, root);
}

template <class T>
void BST<T>::displayorder()
{
    inorder(root);
    cout << endl;
    preorder(root);
    cout << endl;
    postorder(root);
    cout << endl << endl;
}

template <class T>
void BST<T>::search(T x)
{
    if(root = find(root, x)){
        cout << endl << "Found!" << endl;
    }
    else{
        cout << endl << "Not Found!" << endl;
    }
}

template <class T>
BST::node* BST<T>::makeEmpty(node* tree)        //AN ERROR OCCURS HERE
{
    if(tree == NULL)
        return NULL;
    {
        makeEmpty(tree->left);
        makeEmpty(tree->right);
        delete tree;
    }
    return NULL;
}




template <class T>
BST::node* BST<T>::insert(T x, node* tree)      //AN ERROR OCCURS HERE
{
    if(tree == NULL)
    {
        tree = new node;
        tree->data = x;
        tree->left = tree->right = NULL;
    }
    else if(x < tree->data)
        tree->left = insert(x, tree->left);
    else if(x >= tree->data)
        tree->right = insert(x, tree->right);
    return tree;
}

BST::node* BST::findMin(node* tree)             //AN ERROR OCCURS HERE
{
    if(tree == NULL)
        return NULL;
    else if(tree->left == NULL)
        return tree;
    else
        return findMin(tree->left);
}

BST::node* BST::findMax(node* tree)             //AN ERROR OCCURS HERE
{
    if(tree == NULL)
        return NULL;
    else if(tree->right == NULL)
        return tree;
    else
        return findMax(tree->right);
}

template <typename T>
BST::node* BST<T>::remove(T x, node* tree)      //AN ERROR OCCURS HERE
{
    node* temp;
    if(tree == NULL)
        return NULL;
    else if(x < tree->data)
        tree->left = remove(x, tree->left);
    else if(x > tree->data)
        tree->right = remove(x, tree->right);
    else if(tree->left && tree->right)
    {
        temp = findMin(tree->right);
        tree->data = temp->data;
        tree->right = remove(tree->data, tree->right);
    }
    else
    {
        temp = tree;
        if(tree->left == NULL)
            tree = tree->right;
        else if(tree->right == NULL)
            tree = tree->left;
        delete temp;
    }

    return tree;
}

template <class T>
void BST<T>::inorder(node* tree)
{
    if(tree == NULL){
        return;
    }
    inorder(tree->left);
    cout << tree->data << " ";
    inorder(tree->right);
}

2 个答案:

答案 0 :(得分:0)

您还需要将{BST :: node}限定为BST<T>::node。另外,请记住,所有模板代码都需要存在bst.h中,因为它需要由使用它的每个源文件实例化(如main.cpp)。

答案 1 :(得分:0)

添加

在函数定义中添加BST ::节点。另外包括.py(cpp文件),如第27行所示,以便在头文件和.ipp文件中实现类方法。如果你不想,你不必将所有模板代码放在头文件中。

另一个提示是,您不能将根节点传递给您的函数,因为类外的任何人都可以访问您的树根。瞄准类似下面代码的东西

1 #ifndef BST_H
  2 #define BST_H
  3
  4 #include <memory>
  5
  6 template<typename T>
  7 class node;
  8
  9 template <typename T>
 10 class BST
 11 {
 12 public:
 13     BST();
 14     BST(std::initializer_list<T> init);
 15    ~BST();
 16
 17     void make_empty();
 18     bool is_empty() const noexcept;
 19     bool is_full() const;
 20     void insert(const T&);
 21     void remove(const T&);
 22     void print_tree() const noexcept;
 23 private:
 24     std::unique_ptr<node<T>> root;
 25 };
 26
 27 #include "binary_search_tree.ipp"
 28
 29 #endif