AVL树的非递归实现(Java版)

AVL树的非递归实现(Java版)

AVL树是一种二叉查找树的改进算法,它避免了普通二叉树可能出现的长链现象,其根本的算法思想是每当对树进行插入和删除操作时,通过有限的指针变换,改变原有的树的结构,使得每个节点的左子树和右子树相对平衡。

虽然现实中AVL树算法被许多更优秀的算法所替代,但是这一套算法非常适合初学者进行练习,详细的AVL树的教程和博客不难找到。通常,AVL树是通过递归方式来实现的,这种方法思路清晰,代码量小,并且AVL算法有限控制了二叉树的高度,使得递归对运行效率的影响可以忽略不计。对应的,也有非递归的方法,程序运行起来可以快那么一丢丢,但是代码量大,编写难度高,实际应用中不建议使用非递归来实现。

从学习的角度来说,练习编写非递归算法可以让你深入理解递归和栈的联系,可以让你在以后的编程工作中编写出更高效的代码。这里给出一个非递归的Java代码(包括插入和删除都是非递归的写法),有兴趣的可以参考一下:

import java.util.ArrayDeque;

public class AVLTree<E extends Comparable<? super E>> {
    private static class BinaryNode<E> {
        BinaryNode( E e ) { this( e, null, null ); }
        BinaryNode( E e, BinaryNode<E> lt, BinaryNode<E> rt ) {
            element = e; left = lt; right = rt; height = 0;
        }
        E element;
        BinaryNode<E> left, right;
        int height;
    }
    private BinaryNode<E> root = null;
    public AVLTree() { root = null; }
    public void makeEmpty() { root = null; }
    public boolean isEmpty() { return root == null; }
    public boolean contains( E e ) {
        BinaryNode<E> seek = root;
        while( seek != null ) {
            int cmp = e.compareTo( seek.element );
            if( cmp < 0 ) seek = seek.left;
            else if( cmp > 0 ) seek = seek.right;
            else return true;
        }
        return false;
    }
    public E findMin() {
        if( isEmpty() ) return null;
        BinaryNode<E> seek = root;
        while( seek.left != null ) seek = seek.left;
        return seek.element;
    }
    public E findMax() {
        if( isEmpty() ) return null;
        BinaryNode<E> seek = root;
        while( seek.right != null ) seek = seek.right;
        return seek.element;
    }
    public void insert( E e ) {
        if( isEmpty() ) {
            root = new BinaryNode<E>( e, null, null );
            return;
        }
        BinaryNode<E> seek = root;
        ArrayDeque<BinaryNode<E>> stack = new ArrayDeque<>();
        while( true ) {
            int cmp = e.compareTo( seek.element );
            if( cmp < 0 ) {
                stack.push( seek );
                if( seek.left == null ) {
                    seek.left = new BinaryNode<E>( e, null, null );
                    break;
                }
                seek = seek.left;
            }
            else if( cmp > 0 ) {
                stack.push( seek );
                if( seek.right == null ) {
                    seek.right = new BinaryNode<E>( e, null, null );
                    break;
                }
                seek = seek.right;
            }
            else return;
        }
		balance( stack );
    }
    private int height( BinaryNode<E> node ) {
        return node == null ? -1 : node.height;
    }
	private void balance( ArrayDeque<BinaryNode<E>> stack ) {
		BinaryNode<E> seek;
		while( !stack.isEmpty() ) {
            seek = stack.pop();
			int h = seek.height;
            int h1 = height( seek.left );
            int h2 = height( seek.right );
            if( h1 - h2 == 2 ) {
				BinaryNode<E> lt = seek.left;
				if( height( lt.left ) >= height( lt.right ) ) {
					seek.left = lt.right;
					lt.right = seek;
					seek.height = Math.max( height(seek.left), height(seek.right) ) + 1;
					lt.height = Math.max( height(lt.left), height(lt.right) ) + 1;
					if( stack.isEmpty() ) root = lt;
					else {
						BinaryNode<E> f = stack.peek();
						if( f.left == seek ) f.left = lt;
						else f.right = lt;
					}
					if( lt.height == h ) return;
				}
				else {
					BinaryNode<E> rt = lt.right;
					seek.left = rt.right;
					lt.right = rt.left;
					rt.left = lt;
					rt.right = seek;
					seek.height = Math.max( height(seek.left), height(seek.right) ) + 1;
					rt.height = Math.max( height(rt.left), height(rt.right) ) + 1;
					lt.height = Math.max( height(lt.left), height(lt.right) ) + 1;
					if( stack.isEmpty() ) root = rt;
					else {
						BinaryNode<E> f = stack.peek();
						if( f.left == seek ) f.left = rt;
						else f.right = rt;
					}
					if( rt.height == h ) return;
				}
            }
            if( h2 - h1 == 2 ) {
				BinaryNode<E> rt = seek.right;
				if( height( rt.right ) >= height( rt.left ) ) {
					seek.right = rt.left;
					rt.left = seek;
					seek.height = Math.max( height(seek.left), height(seek.right) ) + 1;
					rt.height = Math.max( height(rt.left), height(rt.right) ) + 1;
					if( stack.isEmpty() ) root = rt;
					else {
						BinaryNode<E> f = stack.peek();
						if( f.left == seek ) f.left = rt;
						else f.right = rt;
					}
					if( rt.height == h ) return;
				}
				else {
					BinaryNode<E> lt = rt.left;
					seek.right = lt.left;
					rt.left = lt.right;
					lt.left = seek;
					lt.right = rt;
					seek.height = Math.max( height(seek.left), height(seek.right) ) + 1;
					lt.height = Math.max( height(lt.left), height(lt.right) ) + 1;
					rt.height = Math.max( height(rt.left), height(rt.right) ) + 1;
					if( stack.isEmpty() ) root = lt;
					else {
						BinaryNode<E> f = stack.peek();
						if( f.left == seek ) f.left = lt;
						else f.right = lt;
					}
					if( lt.height == h ) return;
				}
            }
			else if( Math.max( h1, h2 ) + 1 == h ) return;
			else seek.height = Math.max( h1, h2 ) + 1;
        }
	}
    public void remove( E e ) {
		BinaryNode<E> seek = root;
		ArrayDeque<BinaryNode<E>> stack = new ArrayDeque<>();
        while( seek != null ) {
            int cmp = e.compareTo( seek.element );
            if( cmp < 0 ) {
				stack.push( seek );
				seek = seek.left;
			}
            else if( cmp > 0 ) {
				stack.push( seek );
				seek = seek.right;
			}
			else break;
        }
		if( seek == null ) return;
		if( seek.left == null ) {
			if( stack.isEmpty() ) root = seek.right;
			else {
				BinaryNode<E> f = stack.peek();
				if( e.compareTo( f.element ) < 0 ) f.left = seek.right;
				else f.right = seek.right;
				balance( stack );
			}
		}
		else {
			stack.push( seek );
			BinaryNode<E> max = seek.left;
			while( max.right != null ) {
				stack.push( max );
				max = max.right;
			}
			if( stack.peek() == seek ) seek.left = max.left;
			else stack.peek().right = max.left;
			seek.element = max.element;
			balance( stack );
		}
    }
    public void printTree() {
        if( isEmpty() ) System.out.println( "Empty Tree!!!" );
        else printTree( root, "" );
    }
    private void printTree( BinaryNode<E> node, String str ) {
        if( node != null ) {
            printTree( node.left, str+'\t' );
            System.out.println( str + node.element + "(" + node.height + ")" );
            printTree( node.right, str+'\t' );
        }
    }
    public void removeMin() {
		if( isEmpty() ) return;
		BinaryNode<E> seek = root;
		ArrayDeque<BinaryNode<E>> stack = new ArrayDeque<>();
		while( seek.left != null ) {
			stack.push( seek );
			seek = seek.left;
		}
		stack.peek().left = null;
		balance( stack );
	}
    public void removeMax() {
		if( isEmpty() ) return;
		BinaryNode<E> seek = root;
		ArrayDeque<BinaryNode<E>> stack = new ArrayDeque<>();
		while( seek.right != null ) {
			stack.push( seek );
			seek = seek.right;
		}
		stack.peek().right = null;
		balance( stack );
	}
}

你可能感兴趣的:(AVL树的非递归实现(Java版))