`
128kj
  • 浏览: 584633 次
  • 来自: ...
社区版块
存档分类
最新评论

二叉查找树及实现

阅读更多
//二叉搜索树
public class BinarySearchTree<T extends Comparable<? super T>>
{
    /**
     * 构造一棵空树.
     */
    public BinarySearchTree( )
    {
        root = null;
    }

    /**
     * 在二叉搜索树中插入数据.
     * @param x the item to insert.
     */
    public void insert( T x )
    {
        root = insert( x, root );
    }

    /**
     * 从二叉搜索中删除数据(节点).
     * @param x the item to remove.
     */
    public void remove( T x )
    {
        root = remove( x, root );
    }

    /**
     * 找最小数据.
     * @return smallest item or null if empty.
     */
    public T findMin( )
    {
        if( isEmpty( ) )
            throw new UnderflowException( );
        return findMin( root ).element;
    }

    /**
     * 找最大数据.
     * @return the largest item of null if empty.
     */
    public T findMax( )
    {
        if( isEmpty( ) )
            throw new UnderflowException( );
        return findMax( root ).element;
    }

    //二叉搜索树中是否包含x
    public boolean contains( T x )
    {
        return contains( x, root );
    }

   
    public void makeEmpty( )
    {
        root = null;
    }

   
    public boolean isEmpty( )
    {
        return root == null;
    }

   //中序遍历输出二叉搜索树的内容
    public void printTree( )
    {
        if( isEmpty( ) )
            System.out.println( "Empty tree" );
        else
            printTree( root );
    }

    //插入
    private BinaryNode<T> insert( T x, BinaryNode<T> t )
    {
        if( t == null )
            return new BinaryNode<T>( x, null, null );
        
        int compareResult = x.compareTo( t.element );
            
        if( compareResult < 0 )
            t.left = insert( x, t.left );
        else if( compareResult > 0 )
            t.right = insert( x, t.right );
        else
            ;  // Duplicate; do nothing
        return t;
    }

删除节点是最难的操作了。如果节点是一片树叶,那么它可以立即删除,将原来指向这个节点的引用设为null。如果节点有一个儿子,则该节点可以在其父节点调整自己的链以绕过该节点后被删除。

    //删除
    private BinaryNode<T> remove( T x, BinaryNode<T> t )
    {
         //先在树中查找x   
        if( t == null )
            return t;   // 没有找到,返回
        
        int compareResult = x.compareTo( t.element );
          // 在左树中找
        if( compareResult < 0 )
            t.left = remove( x, t.left );
          //在右树中找
        else if( compareResult > 0 )
            t.right = remove( x, t.right );
        //在树中找到了节点值为x的节点
        else if( t.left != null && t.right != null ) // 这个节点有两个孩子节点
        {
            t.element = findMin( t.right ).element;
            t.right = remove( t.element, t.right );
        }
        else//这个节点只有一个孩子节点或没有孩子节点
            t = ( t.left != null ) ? t.left : t.right;
        return t;
    }

   //在二叉搜索树中找最小值节点
    private BinaryNode<T> findMin( BinaryNode<T> t )
    {
        if( t == null )
            return null;
        else if( t.left == null )
            return t;
        return findMin( t.left );
    }

    //在二叉搜索树中找最大值节点
    private BinaryNode<T> findMax( BinaryNode<T> t )
    {
        if( t != null )
            while( t.right != null )
                t = t.right;

        return t;
    }

   //是否包含
    private boolean contains( T x, BinaryNode<T> t )
    {
        if( t == null )
            return false;
            
        int compareResult = x.compareTo( t.element );
            
        if( compareResult < 0 )
            return contains( x, t.left );
        else if( compareResult > 0 )
            return contains( x, t.right );
        else
            return true;    // Match
    }

   //中序遍历二叉树
    private void printTree( BinaryNode<T> t )
    {
        if( t != null )
        {
            printTree( t.left );
            System.out.print(t.element+"  ");
            printTree( t.right );
        }
       
    }

    // 二叉搜索树节点类
    private static class BinaryNode<T>
    {
            // Constructors
        BinaryNode( T theElement )
        {
            this( theElement, null, null );
        }

        BinaryNode( T theElement, BinaryNode<T> lt, BinaryNode<T> rt )
        {
            element  = theElement;
            left     = lt;
            right    = rt;
        }

        T element;            // The data in the node
        BinaryNode<T> left;   // Left child
        BinaryNode<T> right;  // Right child
    }


      /** 二叉搜索树的根 */
    private BinaryNode<T> root;


        // 测试
    public static void main( String [ ] args )
    {
        BinarySearchTree<Integer> t = new BinarySearchTree<Integer>( );
        final int NUMS = 100;
        final int GAP  =   37;

        System.out.println( "Checking... (no more output means success)" );

        for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
            t.insert( i );
         t.printTree( );
       System.out.printf("\n\n");
       
       for( int i = 1; i < NUMS; i+= 2 )
            t.remove( i );
           t.printTree( );
        System.out.println();
       
        if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
            System.out.println( "FindMin or FindMax error!" );

          
        for( int i = 2; i < NUMS; i+=2 )
             if( !t.contains( i ) )
                 System.out.println( "Find error1!" );

        for( int i = 1; i < NUMS; i+=2 )
        {
            if( t.contains( i ) )
                System.out.println( "Find error2!" );
        }
    }
}



下载源码:
  • 大小: 40.6 KB
  • ex.zip (1.8 KB)
  • 下载次数: 4
分享到:
评论

相关推荐

    二叉查找树的实现

    1、 定义二叉查找树的类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)实现二叉查找树结构; 2) 实现二叉查找树的查找、插入和删除等算法;

    二叉查找树C++实现

    二叉查找树的C++实现

    C++模板类二叉查找树的实现

    这个程序实现了二叉查找树的删除,增加,先序遍历,后序遍历,中序遍历,还有一些非递归和层次遍历!

    C++实现的最优二叉查找树

    用C++实现的最优二叉查找树,简单,明了,是数据结构里经典必学算法,初学者适用

    二叉查找树实现简单的信息检索

    二叉查找树实现简单的信息检索

    C++二叉查找树的实现

    这里是二叉查找树的实现代码,如果有不明白的可以联系我,很多其他的C++代码我都有

    二叉查找排序树的实现代码

    最近在研究数据结构这本书,自己动手实现的一个二叉查找排序树的类BinSortTree,实现数据的插入,查找,删除,层序遍历,中序遍历等操作,熟悉数据结构的朋友都知道,根据二叉排序树的定义,中序遍历后得到的序列...

    简单二叉查找树的java实现

    二叉查找树的实现。包括树的平衡以及树节点的删除。以及树的广度优先遍历,深度优先遍历。

    数据结构课程设计二叉排序树的实现

    4)分别用二叉排序树和数组去存储一个班(50 人以上)的成员信息(至少包括学号、姓名、成绩 3 项),对比查找效率,并说明在什么情况下二叉排序树效率高,为什么? 5) 格式就要按照我们作业的要求,对数据测试,分析,总结...

    二叉查找树(二叉排序树)的详细实现

    这是一个二叉查找树,实现了以下操作:插入结点、构造二叉树、删除结点、查找、 查找最大值、查找最小值、查找指定结点的前驱和后继。上述所有操作时间复杂度均为o(h),其中h是树的高度 注释很详细,具体内容就看...

    二叉查找树(二分搜索树)的C++方法实现

    资源内容:完整的二叉查找树C++头文件,包括运算符重载,bst类构造器、bst类析构器、destroy()、size()、insert(),迭代器类的声明与实现,++运算符重载(前置、后置)、--运算符重载、*运算符重载、!=运算符重载、...

    二叉查找树的C语言实现——插入,删除,查找

    二叉查找树的C语言实现,实现功能:插入、删除、查找。

    二叉排序树的实现

    二叉排序树的实现 这个希望对你二叉排序树上有所帮助

    c++实现二叉查找树

    c++实现的二叉查找树,代码简陋,大家互相学习即可

    二叉查找树 减治法——C++代码

    课程的随堂作业,C语言的,用dev就能运行,萌新代码,勿喷,仅仅帮助不想写作业的朋友方便一下,反正老师也不会仔细检查的

    二叉查找树的C++实现

    这是我自己用C++写的一个二叉查找树的链表实现。由于是老师突然布置的课程实验,时间比较匆忙,所以在写代码的过程中首先考虑的代码的功能的实现,而忽视了代码的可读性和健壮性,有的代码并不是最佳的功能实现。贴...

    二叉查找树实现代码

    二叉查找树的插入、搜索、删除、寻找前驱结点、寻找后继结点

Global site tag (gtag.js) - Google Analytics