zoukankan      html  css  js  c++  java
  • 二叉查找树简单实现

    树是一种简单的数据结构,其大部分操作的运行时间平均为O(logN)。我将《数据结构与算法分析》上的的代码片段加入自己的理解简单实现了该结构:

    BinarySearchTree.h源码如下:

    #ifndef BINARYSEARCHTREE_H
    #define BINARYSEARCHTREE_H
    
    #include <iostream>
    
    template <typename Comparable>
    class BinarySearchTree
    {
        public:
            BinarySearchTree()
            { root = NULL; }
            BinarySearchTree( const BinarySearchTree &rhs )
            { operator=( rhs ); }
            ~BinarySearchTree()
            { makeEmpty(); }
    
            const Comparable &findMin() const
            {
                BinaryNode *p = findMin( root );
                return p->element;
            }
            const Comparable &findMax() const
            {
                BinaryNode *p = findMax( root );
                return p->element;
            }
            bool contains( const Comparable &x ) const
            { return contains( x,root ); }
            bool iSEmpty() const
            { return root == NULL; }
            void printTree() const
            { printTree( root ); }
    
            void makeEmpty()
            { makeEmpty( root ); }
    
            void insert( const Comparable &x )
            { insert( x,root ); }
            void remove( const Comparable &x )
            { remove( x,root ); }
    
            const BinarySearchTree &operator=( const BinarySearchTree &rhs )
            {
                if ( this != &rhs )
                {
                    makeEmpty();
                    root = clone( rhs.root );
                }
    
                return *this;
            }
    
        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;
    
            void 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 ( x > t->element )
                    insert( x,t->right );
                else
                    ;
            }
    
            void 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;
                }
            }
    
            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 ( t->element < x )
                    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 printTree( BinaryNode *t )const                //先序遍历
            {
                if ( t == NULL )
                    return ;
    
                std::cout << t->element << std::endl;
                printTree( t->left );
                printTree( t->right );
            }
    
            BinaryNode *clone( BinaryNode *t )const
            {
                if ( t == NULL )
                    return NULL;
    
                return new BinaryNode( t->element,clone( t->left ),clone( t->right ) );
            }
    };
    
    #endif // BINARYSEARCHTREE_H

    再写个测试的主程序:

    #include <iostream>
    #include "BinarySearchTree.h"
    
    using namespace std;
    
    int main()
    {
        BinarySearchTree<int> BST;
    
        if ( BST.iSEmpty() )
            cout << "二叉查找树为空!" << endl;
    
        cout << "插入数据中。。" << endl;
        BST.insert( 200 );
        BST.insert( 7 );
        BST.insert( 1 );
        BST.insert( 99 );
        BST.insert( 55 );
    
        cout << "打印二叉树:" << endl;
        BST.printTree();
    
        cout << "最大值为:" << BST.findMax() << endl;
        cout << "最小值为:" << BST.findMin() << endl;
    
        int temp;
        cout << "插入一个值:" << endl;
        cin >> temp;
    
        BST.insert( temp );
        cout << "打印二叉树" << endl;
        BST.printTree();
    
        return 0;
    }

     运行效果:

    我们一路奋战,不是为了改变世界,而是不让世界改变我们 ——《熔炉》
  • 相关阅读:
    IDE-Sublime【3】-配置Node.js开发环境
    Java-Android【2】-弹出对话框
    Node.js-中文分词【1】-node-segment
    Java-Android【1】-控制手机震动
    IDE-Sublime【2】-代码智能提示插件SublimeCodeIntel的安装
    Node.js-安装配置【1】-在Windows XP系统配置环境变量
    Node.js-部署【1】-防火墙端口的配置
    Node.js-npm【1】-常用命令
    Node.js-视图引擎【1】-Swig集成express的安装与配置
    NoSQL-Redis【2】-HDEL给我的一个惊喜
  • 原文地址:https://www.cnblogs.com/ZRBYYXDM/p/5163854.html
Copyright © 2011-2022 走看看