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. SignalR实时推送

    SignalR 的实现机制与 .NET WCF 或 Remoting 是相似的,都是使用远程代理来实现.在具体使用上,有两种不同目的的接口:PersistentConnection 和 Hubs,其中 ...

  2. struts2数据校验与国际化

    数据校验: Action里的validate()方法能校验action类所有的方法,如果有错,如:addFieldError,会自动返回到workflow校验拦截器不向下继续进行,不用return i ...

  3. SharpZipLib 文件/文件夹压缩

    一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...

  4. cxf(3.1.1) 客户端异常 请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。

    最近项目使用webService 于是就使用了最新版本 3.1.1 . cxf 客户端调用时老是出现这个错误,综合网上各种资料修改如下,问题解决 "@XmlType.name 和 @XmlT ...

  5. 【实践】jquery实现滑动动画及轮播

    在做这个之前我已经模仿了一遍大神的轮播动画做了一次其切换模式就是一张图片隐藏另一张图片显示过渡效果是渐入(fadein),但是目前的动画切换大多数都是用图片的左右切换动作的于是在网上看了一些关于这类型 ...

  6. Mac下面的SecureCRT(附破解方案) 更新到最新的8.0.2

    继续更新到8.0.2的破解,整体的破解方案都发生了的变化首先还是去 http://macabc.com/detail.htm?app_id=24 下载最新的8.0.2介于很多小白说替换之后说文件损坏, ...

  7. Prince2的七大原则(6)

    Prince2科普_Prince2的七大原则(6) 按照惯例我们先来回顾一下,PRINCE2七大原则分别是指:持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 今天讲第六个 ...

  8. 浙江理工2015.12校赛-F Landlocked

    Landlocked Time Limit: 5 Sec Memory Limit: 128 MB Submit: 288 Solved: 39 Description Canada is not a ...

  9. webapi 解决ajax跨域请求问题

    webapi在配置文件中加入这几句就可以解决ajax(同源策略是JavaScript里面的限制,其他的编程语言,比如在C#,Java或者iOS等其他语言中是可以调用外部的WebService,也就是 ...

  10. filebeat 多行日志的处理

    配置文件位于/etc/filebeat/filebeat.yml,就是filebeat的主配置文件 打开文件,搜索multiline:,默认是注释的,常用的有如下三个配置: multiline: pa ...