C++ 二叉查找树

就不写太多注释了,函数名字应该可以看懂哦~

template<typename T>
class BinarySearchTree
{
public:
    BinarySearchTree()
    {
        root = nullptr;
    }

    BinarySearchTree( const BinarySearchTree & rhs )
    {

    }

    ~BinarySearchTree()
    {
        makeEmpty(root);
    }

    const T & findmax() const
    {
        return findmax(root)->element;
    }

    const T & findmin() const
    {
        return findmin(root)->element;
    }

    bool contains( const T & t ) const
    {
        return contains(t, root);
    }

    bool isEmpty() const
    {
        return (nullptr == root);
    }

    void printTree() const
    {
        printTree(root);
    }

    void makeEmpty()
    {
        makeEmpty(root);
    }

    void insert(const T & t)
    {
        insert(t, root);
    }

    void remove(const T & t)
    {
        remove(t, root);
    }

    BinarySearchTree & operator=(const BinarySearchTree &rhs );
private:
    struct Node
    {
        T element;
        Node *left;
        Node *right;
        Node(const T & t, const Node *lf = nullptr, const Node *rh = nullptr ): element(t), left(lf), right(rh) {};
    };

    Node * root;

    void insert(const T & t, Node * n) const
    {
        if(nullptr == n)
            n = new Node(t); //当树为空的时候
        else
        {
            if(t < n->element)
            {
                if( nullptr == n->left)
                    n->left = new Node(t); //左子树为空时候
                else
                    insert(t, n->left);
            }
            else
            {
                if(t > n->element)
                {
                    if( nullptr == n->right)
                        n->right = new Node(t);
                    else
                        insert(t, n->right);
                }
            }
        }
        //当t不小于不大于本节点值时候,无需任何操作
    }

    void remove(const T & t, Node * n) const
    {
        if( nullptr == n)
            return;
        if( t < n->element)
            remove(t, n->left);
        else
        {
            if(t > n->element)
            {
                remove(t, n->right);
            }
            else
            {
                if( nullptr != n->left && nullptr != n->right)
                {
                    n->element = findmin(n->right)->element;
                    remove (n->element, n->right);
                }
                else
                {
                    Node * old = n;
                    n = (n->left != nullptr) ? n->left: n->right;
                    delete old;
                }
            }
        }
    }

    void makeEmpty(Node * n)
    {
        if( nullptr == n)
            return; //树未空时候
        if( nullptr != n->left)
            makeEmpty( n->left);
        if( nullptr != n->right)
            makeEmpty(n->right);
        delete n;
    }

    Node * findmax(Node * n) const
    {
        if(nullptr == n)
            return; //root为空
        if(nullptr != n->right)
            return findmax( n->right);
        return n;
    }

    Node * findmin(Node * n) const
    {
        if(nullptr == n)
            return; //root为空
        if(nullptr != n->left)
            return findmax( n->left );
        return n;
    }

    bool contains( const T & t, Node * n ) const
    {
        if(nullptr == n)
            return false;
        if( t < n->element)
            return contains(t, n->left);
        else
        {
            if(t > n->element)
                return contains(t, n->right);
        }
    }
};

你可能感兴趣的:(C++,函数)