c++ 二叉查找树

<pre name="code" class="cpp">/*
 * @class BinarySearchTree
 * @desc 二叉查找树
 *  树中的节点X,节点X的左子树比节点X小, 该节点X的右子树比节点X大
 */
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H

#include <iostream>

using namespace std;

template <typename Comparable>
class BinarySearchTree
{
public:
    BinarySearchTree(){
        root = NULL;
    }
    BinarySearchTree(const BinarySearchTree &rhs){
        this = rhs;
    }
    ~BinarySearchTree(){
        makeEmpty();
    }

    const BinarySearchTree &operator = (const BinarySearchTree &rhs){
        if( this != rhs){
            makeEmpty();
            root = clone(rhs.root);
        }
        return *this;
    }

    const Comparable &findMin() const{
        BinaryNode *min = findMin( root );
        if( min == NULL ){
            return NULL;
        }
        return min->element;
    }
    const Comparable &findMax() const{
        BinaryNode *max = findMax( root );
        if( max == NULL ){
            return NULL;
        }
        return max->element;
    }
    bool contains( const Comparable &x ) const{
        return contains(x, root);
    }
    bool isEmpty() const{
        return root == NULL;
    }

    void makeEmpty(){
        makeEmpty(root);
    }

    void insert(const Comparable &value){
        insert ( value , root );
    }

    void remove(const Comparable &value){
        remove ( value, root);
    }

    void preorderTraversal(){
        cout << "----------------------preorder traversal------------" << endl;
        if( isEmpty() ){
            cout << "empty tree" << endl;
        }else{
            preorderTraversal( root );
        }
        cout << endl;
    }
    void inorderTraversal(){
        cout << "----------------------inorder traversal------------" << endl;
        if( isEmpty() ){
            cout << "empty tree" << endl;
        }else{
            inorderTraversal( root );
        }
        cout << endl;

    }
    void postorderTraversal(){
        cout << "----------------------postorder traversal------------" << endl;
        if( isEmpty() ){
            cout << "empty tree" << endl;
        }else{
            postorderTraversal( root );
        }
        cout << endl;
    }

private:
    struct BinaryNode
    {
        Comparable element;
        BinaryNode *left;
        BinaryNode *right;

        BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt)
        :element(theElement), left(lt), right(rt)
        { }
    };

    BinaryNode *root;

    //递归实现
    BinaryNode *findMin(BinaryNode *t) const{
        if ( t == NULL ){
            return NULL;
        }
        if ( t->left == NULL){
            return t;
        }
        return findMin( t->left );
    }
    //非递归实现
    BinaryNode *findMax(BinaryNode *t) const{
        if( t != NULL){
            while( t->right != NULL ){
                t = t->right;
            }
        }
        return t;
    }
    bool contains(const Comparable &x, BinaryNode *t) const{
        if ( t == NULL ){
            return false;
        }else if ( x < t->element){
            return contains(x, t->left);
        }else if ( x > t->element){
            return contains(x, t->right);
        }else{
            return true;
        }
    }
    void makeEmpty(BinaryNode *t){
        if( t != NULL ){
            makeEmpty( t->left );
            makeEmpty( t->right );
            delete t;
        }
        t = NULL;
    }

    //递归调用
    void insert(const Comparable &x, BinaryNode *&t){
        if ( t == NULL ){
            t = new BinaryNode(x, NULL, NULL);
        }else if ( x < t->element) {
            insert(x, t->left);
        }else if ( x > t->element ) {
            insert(x, t->right);
        }else{  //相等的情况没有处理
            ;
        }
    }

    void remove(const Comparable &x, BinaryNode *&t){
        if(t == NULL ){
            return ;
        }else if ( x < t->element ){
            remove ( x, t->left );
        }else if ( x > t->element ) {
            remove ( x, t->right );
        }else if ( t->right != NULL && t->left != NULL ){
            t->element = findMin( t->right )->element;
            remove ( t->element, t->right );
        }else {
            BinaryNode *old = t;
            t = (t->left != NULL) ? t->left : t->right;
            delete old;
        }
    }

    BinaryNode *clone(BinaryNode *t) const{
        if( t == NULL){
            return NULL;
        }
        return new BinaryNode( t->element, clone( t->left ), clone( t->right ));
    }

    //前序遍历 中左右
    void preorderTraversal(BinaryNode *t){
        if(t != NULL){
            cout << t->element << "\t";
            preorderTraversal(t->left);
            preorderTraversal(t->right);
        }
    }

    //中序遍历 左中右
    void inorderTraversal(BinaryNode *t){
        if(t != NULL){
            inorderTraversal(t->left);
            cout << t->element << "\t";
            inorderTraversal(t->right);
        }
    }

    //后序遍历 左右中
    void postorderTraversal(BinaryNode *t){
        if(t != NULL){
            postorderTraversal(t->left);
            postorderTraversal(t->right);
            cout << t->element << "\t";
        }
    }
};

#endif

#include <iostream>
#include <vector>
#include "BinarySearchTree.h"

using namespace std;

int main()
{
    BinarySearchTree<int> *tree = new BinarySearchTree<int>();
    std::vector<int> v = {123,34,21,5,754,43,23,75,867,13, 3, 4, 55,12};
//    for(std::vector<int>::iterator itr = v.begin(); itr != v.end(); ++itr){
    for(int i = 0; i < v.size(); ++i){
        cout << v[i] << ",";
        tree->insert(v[i]);
    }
    cout << endl;

    tree->preorderTraversal();
    tree->inorderTraversal();
    tree->postorderTraversal();

    cout << "contains(123):" << tree->contains(123) << endl;
    cout << "contains(1)" << tree->contains(1) << endl;
    cout << "findMin:" << tree->findMin() << endl;
    cout << "findMax:" << tree->findMax() << endl;
    tree->remove(23);
    cout << "-----------------remove(23)-------------------" << endl;
    tree->inorderTraversal();
    return 0;
}


 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值