public class BinaryHeap<AnyType extends Comparable<? super AnyType>> {

private static final int DEFAULT_CAPACITY = 10;
    private AnyType[] array;
    private int currentSize;

public BinaryHeap() {

this(DEFAULT_CAPACITY);
    }

public BinaryHeap(int capacity) {
        currentSize = 0;
        array = (AnyType[]) new Comparable[capacity];

}

public BinaryHeap(AnyType[] items) {
        currentSize = items.length;

array = (AnyType[]) new Comparable[(array.length + 2) * 11 / 10];

int i = 0;
        for (AnyType item : items) {
            array[i++] = item;
            buildHeap();
        }

}

public void makeEmpty() {
        currentSize = 0;
    }

public boolean isEmpty() {
        return currentSize == 0;
    }

public AnyType findMin() {
        if (isEmpty())
            System.out.println("this is empty");
        return array[1];
    }
     private void enlargeArray( int newSize )
        {
            AnyType [] old = array;
            array = (AnyType []) new Comparable[ newSize ];
            for( int i = 0; i < old.length; i++ )
                array[ i ] = old[ i ];        
        }
     public void insert( AnyType x )
        {
            if( currentSize == array.length - 1 )
                enlargeArray( array.length * 2 + 1 );

// 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 AnyType deleteMin() {
        if (isEmpty())
            System.out.println("this is empty");

AnyType minItem = findMin();
        array[1] = array[currentSize--];
        percolateDown(1);

return minItem;
    }

private void buildHeap() {
        for (int i = currentSize / 2; i > 0; i--)
            percolateDown(i);
    }

private void percolateDown(int hole) {

int child;
        AnyType 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;
    }

public static void main( String [ ] args )
        {
            int numItems = 10000;
            BinaryHeap<Integer> h = new BinaryHeap<Integer>( );
            int i = 37;

for( i = 37; i != 0; i = ( i + 37 ) % numItems )
                h.insert( i );
            for( i = 1; i < numItems; i++ )
                if( h.deleteMin( ) != i )
                    System.out.println( "Oops! " + i );
        }
    
    
}

BinaryHeap Java实现的更多相关文章

  1. binary heap

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. 数据结构(Java语言)——BinaryHeap简单实现

    优先队列priority queue是同意至少下列两种操作的数据结构:insert插入以及deleteMin(删除最小者),它的工作是找出,返回并删除优先队列中最小的元素.insert操作等价于enq ...

  4. 二叉堆的构建(Java)

    package com.rao.linkList; /** * @author Srao * @className BinaryHeap * @date 2019/12/3 14:14 * @pack ...

  5. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  6. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  7. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  8. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  9. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

随机推荐

  1. Oracle配置本地网络服务名

    Oracle安装完成后,可以使用客户端自带的的网络配置向导(Net Configuration Assistant)进行配置 1.启动Net Configuration Assistant.选择&qu ...

  2. 笔记本自带 WiFi 功能

    在寝室,动网速基本崩溃.平时打电话什么的都得到阳台,有时候还听不清声音.对于学校的环境,我不说什么了. 笔记本可以上网,那就要满足手机等移动电子设备上网的上网需求. WiFi 热点就显得尤为重要了. ...

  3. -Xmx 和 –Xms 设置最大堆和最小堆

    C:\Java\jre1.6.0\bin\javaw.exe 按照上面所说的,最后参数在eclipse.ini中可以写成这个样子: -vmargs     -Xms128M     -Xmx512M ...

  4. Ruby(rails)win环境下安装

    1.RubyInstaller 在RubyInstaller官网下载window版本安装,地址:http://rubyinstaller.org/downloads/  执行安装程序,勾选Add Ru ...

  5. configure.ac:32: error: possibly undefined macro: AC_DEFINE

    在ubuntu 下编译snappy时,在检查依赖关系时,处理autoconf的包时,在相关依赖包都已经安装的情况下,报如下错误,死活不过. configure.ac:32: error: possib ...

  6. 【转】SVN的dump文件导入

    转载地址:http://erniu.sz.blog.163.com/blog/static/11517292220103282813176/ 把SVN的dump文件导入SVN数据库的方法: 在SVN ...

  7. Linux常用调优配置

    cenos 6.5 文件句柄和网络端口 修改系统所有进程可用句柄数,vi /etc/sysctl.conf fs.file-max=655360net.ipv4.ip_local_port_range ...

  8. 关于token的杂记

    http://www.cnblogs.com/xiekeli/p/5607107.html https://www.oschina.net/question/1264088_220768 token作 ...

  9. 开发板上使用core文件调试

    转载:http://www.nginx.cn/1521.html 如果开发板的操作系统也是linux,core调试方法依然适用.如果开发板上不支持gdb,可将开发板的环境(依赖库).可执行文件和cor ...

  10. XDocument 获取包括第一行的声明(版本、编码)的所有节点

    XDocument保存为xml文件的方法如下: XDocument doc = new XDocument( new XDeclaration("1.0","UTF-8& ...