• 二叉堆


    import java.util.Arrays;
    
    
    public class BinaryHeap
    {
        
        public BinaryHeap( )
        {
            this( DEFAULT_CAPACITY );
        }
        public BinaryHeap( Comparable[] items ){  
            currentSize = items.length;  
            array = new Comparable[ (currentSize + 2) * 11 / 10 ];  
              
            int i=1;  
            for( Comparable item : items ){  
                array[ i++ ] = item;  
            }  
            buildHeap();  
        }  
        
        public BinaryHeap( int capacity )
        {
            currentSize = 0;
            array = new Comparable[ capacity + 1 ];
        }
    
        public void insert( Comparable x ) 
        {
            
                // Percolate up
            int hole = ++currentSize;
            for( ; hole > 1 && x.compareTo( array[ hole / 2 ] ) < 0; hole /= 2 )
                array[ hole ] = array[ hole / 2 ];
            array[ hole ] = x;
        }
    
      
        public Comparable findMin( )
        {
            if( isEmpty( ) )
                return null;
            return array[ 1 ];
        }
    
       
        public Comparable deleteMin( )
        {
            if( isEmpty( ) )
                return null;
    
            Comparable minItem = findMin( );
            array[ 1 ] = array[ currentSize-- ];
            percolateDown( 1 );
    
            return minItem;
        }
    
       
        private void buildHeap( )
        {
            for( int i = currentSize / 2; i > 0; i-- )
                percolateDown( i );
        }
    
       
        public boolean isEmpty( )
        {
            return currentSize == 0;
        }
    
        
        public boolean isFull( )
        {
            return currentSize == array.length - 1;
        }
    
       
        public void makeEmpty( )
        {
            currentSize = 0;
        }
    
        private static final int DEFAULT_CAPACITY = 100;
    
        private int currentSize;      // Number of elements in heap
        private Comparable [ ] array; // The heap array
    
       
        private void percolateDown( int hole )
        {
          int child;
         Comparable tmp = array[ hole ];
    
         for( ; hole * 2 <= currentSize; hole = child )
            {
              child = hole * 2;
              if( child != currentSize &&
                     array[ child + 1 ].compareTo( array[ child ] ) < 0 )
                  child++;
              if( array[ child ].compareTo( tmp ) < 0 )
                  array[ hole ] = array[ child ];
                else
                  break;
            }
          array[ hole ] = tmp;
        }
    
            // Test program
        public static void main( String [ ] args )
        {
            int numItems = 50;
            BinaryHeap h = new BinaryHeap( numItems );
            int i = 37;
    
            try
            {
                for( i = 37; i != 0; i = ( i + 37 ) % numItems )
                    h.insert( new Integer( i ) );
                 System.out.println(Arrays.toString(h.array));
                 System.out.println(h.findMin());
                 h.deleteMin();
                 System.out.println(Arrays.toString(h.array));
            }
            catch( Exception e )
              { System.out.println( "Overflow (expected)! " + i  ); }
        }
    }
    这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
  • 相关阅读:
    vim介绍 & vim颜色显示和移动光标& vim一般模式下移动光标 & vim一般模式下复制、剪切和粘贴
    lvm 详解 磁盘故障小案例
    磁盘格式化、磁盘挂载、手动增加swap空间
    df du 磁盘分区
    su sudo 限制root远程登录
    usermod 用户密码管理 mkpasswd
    顺序查找,二分法查找,插值查找算法实现及分析
    完全二叉树的构建及三种遍历
    Mybatis由于类型转换出现的问题
    delimiter解释
  • 原文地址:https://www.cnblogs.com/orco/p/6306541.html
Copyright © 2020-2023  润新知