数据结构之查找二叉树--BST

#include <iostream>
using namespace std;
template<class Comparable>
class BinarySearchTree
{
public:
    BinarySearchTree();
    BinarySearchTree(const BinarySearchTree& rhs); //copy constructor
    ~BinarySearchTree(); //destructor

public:
    const Comparable& findMin() const;   //find the minimum value
    const Comparable& findMax() const;   //find the maximum value
    bool contains(const Comparable& x) const; //Determine whether the tree contains x
    bool isEmpty() const;             //Determine whether the tree is empty
    void printTree() const;           //print the binary Tree

    void makeEmpty();                 //make binary tree empty
    void insert(const Comparable& x); //insert a value into binary tree
    void remove(const Comparable& x); //remove a value from binary tree

    const BinarySearchTree& operator= (const BinarySearchTree& rhs); //overload operator =

private:
    struct BinaryNode
    {
        Comparable element;
        BinaryNode* left;   //a pointer to left child
        BinaryNode* right;   // a pointer to right child
        BinaryNode(){}
        BinaryNode(const Comparable& theElem, BinaryNode* lt, BinaryNode* rt) : element(theElem), left(lt), right(rt){}//constructor
    };             //the binary tree node type

    BinaryNode* root; //define the root node
    void insert(const Comparable& x, BinaryNode *&t) const;
    void remove(const Comparable& x, BinaryNode *&t) const;
    BinaryNode* findMin(BinaryNode* t) const;
BinaryNode* findMax(BinaryNode* t) const;
    bool contains(const Comparable& x, BinaryNode* t) const;
    void printTree(BinaryNode* t) const;
    void makeEmpty(BinaryNode *&t) const;
    BinaryNode* clone(BinaryNode *t) const;
};
template<class Comparable>
BinarySearchTree<Comparable>::BinarySearchTree()
{
    root = new BinaryNode(); //constructor construct a tree without subtree
    root = NULL;
}
template<class Comparable>
BinarySearchTree<Comparable>::BinarySearchTree(const BinarySearchTree& rhs)
{
    root = new BinaryNode();
    root = NULL;
    makeEmpty();
    root = clone(rhs.root);
}
template<class Comparable>
BinarySearchTree<Comparable>::~BinarySearchTree()
{
    makeEmpty();
}
template<class Comparable>
const Comparable& BinarySearchTree<Comparable>::findMin() const
{
    return findMin(root)->element;
}
template<class Comparable>
const Comparable& BinarySearchTree<Comparable>::findMax() const
{
    return findMax(root)->element;
}
template<class Comparable>
bool BinarySearchTree<Comparable>::contains(const Comparable& x) const
{
    return contains(x, root);
}
template<class Comparable>
bool BinarySearchTree<Comparable>::isEmpty() const
{
    return root == NULL;
}
template<class Comparable>
void BinarySearchTree<Comparable>::printTree() const
{
    printTree(root);
}
template<class Comparable>
void BinarySearchTree<Comparable>::makeEmpty()
{
    makeEmpty(root);
}
template<class Comparable>
void BinarySearchTree<Comparable>::insert(const Comparable& x)
{
    insert(x, root);
}
template<class Comparable>
void BinarySearchTree<Comparable>::remove(const Comparable& x)
{
    remove(x, root);
}
template<class Comparable>
const BinarySearchTree<Comparable>& BinarySearchTree<Comparable>::operator= (const BinarySearchTree& rhs)
{
    if(this != &rhs)
    {
        makeEmpty();
  root = clone(rhs.root);
    }
    return *this;
}
template<class Comparable>
void BinarySearchTree<Comparable>::insert(const Comparable& x, BinaryNode *&t) const
{
    if(t == NULL)
        t = new BinaryNode(x, NULL, NULL);
    else if(x < t -> element)
        insert(x, t -> left);
    else if(t -> element < x)
        insert(x, t -> right);
    else
        ;        //do nothing
}
template<class Comparable>
void BinarySearchTree<Comparable>::remove(const Comparable& x, BinaryNode *&t) const
{
    if (t == NULL)
        return;
    if(x < t -> element)
        remove(x, t -> left);
    else if(t -> element < x)
        remove(x, t -> right);
    else if(t -> left != NULL && t -> right != NULL)
    {
        t -> element = findMin(t -> right) -> element;
        remove(t->element, t->right);
    }
    else
    {
        BinaryNode *oldNode = t;
        t = (t -> left != NULL) ? t -> left : t -> right;
        delete oldNode;
    }
}
template<class Comparable>
typename BinarySearchTree<Comparable>::BinaryNode* BinarySearchTree<Comparable>::findMin(BinaryNode *t) const
{
    if(t == NULL)
        return NULL;
    if(t -> left == NULL)
        return t;
    return findMin(t -> left);
}
template<class Comparable>
typename BinarySearchTree<Comparable>::BinaryNode* BinarySearchTree<Comparable>::findMax(BinaryNode *t) const
{
    if(t == NULL)
        return NULL;
    if(t -> right == NULL)
        return t;
    return findMax(t -> right);
}
template<class Comparable>
bool BinarySearchTree<Comparable>::contains(const Comparable& x, BinaryNode* t) const
{
    if(t == NULL)
        return false;
    else if(x < t -> element)
        return contains(x, t -> left);
    else if(t -> element < x)
        return contains(x, t -> right);
    else
        return true;
}
template<class Comparable>
void BinarySearchTree<Comparable>::makeEmpty(BinaryNode*& t) const
{
    if(t != NULL)
    {
        makeEmpty(t -> left);
        makeEmpty(t -> right);
        delete t;
    }
    t = NULL;
}
template<class Comparable>
void BinarySearchTree<Comparable>::printTree(BinaryNode* t) const
{
    if(t != NULL)
    {
        printTree(t -> left);
        cout << t ->element << " ";
        printTree(t -> right);
    }
}
template<class Comparable>
typename BinarySearchTree<Comparable>::BinaryNode* BinarySearchTree<Comparable>::clone(BinaryNode *t) const
{
    if(t == NULL)
        return NULL;
    return new BinaryNode(t -> element, clone(t -> left), clone(t -> right));
}
int main()
{
    BinarySearchTree<int> bin;
    cout<<"Please enter m numbers:"<<endl;
    int m,num;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        cin>>num;
        bin.insert(num);
    }
    cin>>num;
    bin.remove(num);
    cout<<"print the tree:"<<endl;
    bin.printTree();
    cout<<endl;
    cout<<"Please enter a number which you want to find:"<<endl;
    cin>>num;
    if(bin.contains(num) == true)
    {
        cout<<"Yes!it exist in the tree"<<endl;
    }
    else
    {
        cout<<"No find!"<<endl;
    }
    cout<<"the max number is:"<<bin.findMax()<<endl;
    cout<<"the min number is:"<<bin.findMin()<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值