• 二叉查找树简单实现


    树是一种简单的数据结构,其大部分操作的运行时间平均为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;
    }

     运行效果:

    我们一路奋战,不是为了改变世界,而是不让世界改变我们 ——《熔炉》
  • 相关阅读:
    ASP.NET2.0中创建自定义配置节处理程序(声明性模型) joe
    .Net3.0里的DependencyProperty(1) joe
    详解Javascript匿名函数的使用(转) joe
    Mark:未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值 joe
    设置windows 7 默认登陆帐户 joe
    数据库的回滚
    关于软件开发人员如何提高自己的软件专业技术方面的具体建议
    查询表结构
    readonly 和 const总结
    深入NHibernate映射
  • 原文地址:https://www.cnblogs.com/ZRBYYXDM/p/5163854.html
Copyright © 2020-2023  润新知