首页 > 解决方案 > 非STL二叉搜索树搜索函数问题

问题描述

class BinarySearchTree
{
    struct Node
    {
        T data;
        Node * left;
        Node * right;
        Node(T key) :data(key), left(nullptr), right(nullptr) {}
    };

    Node * root;

    int size(Node * node);
    void printNode(Node*);
    void deleteNode(Node*);
    void insert(Node * node, Node * nodePtr);   
    void inorder(Node * root, void(*inorderPtr)(T &)) const;
    void preorder(Node * root, void(*preorderPtr)(T &)) const;
    void postorder(Node * root, void(*postorderPtr)(T &)) const;

public:

    BinarySearchTree();
    ~BinarySearchTree();
    T search(T value);
    BinarySearchTree(const BinarySearchTree<T> & source);
    const BinarySearchTree<T> & operator = (const Node & other);
    void insert(const T);
    void print();
    int size();
    void inorder(void(*output)(T &)) const;
    void preorder(void(*preorderPtr)(T &)) const;
    void postorder(void(*postorderPtr)(T &)) const;
};
template<class T>
inline T BinarySearchTree<T>::search(T value)
{
    if (value == this->root){
        return value;
    }
    else if (value < this->root)
    {
        if (left == nullptr){}  
        else
        {
            return left->search(value);
        }
    }
    else if (value > this->root)
    {
        if (right == nullptr){}
        else
        {
            return right->search(value);
        }
    }
}

错误 C2678 二进制“<”:未找到采用“T”类型左操作数的运算符(或没有可接受的转换)

错误 C2678 二进制“==”:未找到采用“T”类型左侧操作数的运算符(或没有可接受的转换)

错误 C2678 二进制“>”:未找到采用“T”类型左操作数的运算符(或没有可接受的转换)

像这样

bool handler::operator>(const weatherData & w)
{
    bool temp = false;

    if (w > this->testD)
    {
        temp = true;
    }

    return temp;
}

我正在尝试检索存储在 BST 中的映射,我对该特定对象有重载运算符,但由于某种原因,上面的代码会引发这些错误。如果它是 T 类型,我是否需要在我的 BST 中使用重载运算符?任何帮助都会非常感谢。

标签: c++searchbinary-search-tree

解决方案


推荐阅读