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. 简单的bat命令去获取wsdl的源码

    @echo off cd C:\Program Files\Java\jdk1..0_45\bin for /R "D:\wqcCode\company\emtproj\02 Require ...

  2. [Ubuntu] Profile error when launching google-chrome

    Whenever I launch google-chrome, a window is displayed which contains this message: Your profile cou ...

  3. oracle触发器及异常处理 简单例子

    create sequence person_seq start with 1 increment by 1 order                     --按顺序 nocycle       ...

  4. IBM DB2 For Linux安装指南(转)

    一.安装前准备工作: 1.对于Linux系统,需要安装以下软件包: 2.Linux内核设置: 编辑/etc/sysctl.conf文件,加入如下内容: 3.创建相应用户以及组: 官方文档给出必须创建三 ...

  5. 【Jersey】IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务

    本文参考以下内容: 使用Jersey实现RESTful风格的webservice(一) Starting out with Jersey & Apache Tomcat using Intel ...

  6. 微信OAuth2.0网页受权php

    www.MyException.Cn 网友分享于:2014-01-19 浏览:2504次 微信OAuth2.0网页授权php示例 1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的 ...

  7. 公历和农历转换JS代码

    <!-- function CalConv(M) { FIRSTYEAR = 1936; LASTYEAR = 2031; LunarCal = [ new tagLunarCal(23, 3, ...

  8. Mac OS下pip安装 pillow

    sudo pip install pil报错如下:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Develo ...

  9. asp.net 微信支付 错误解决方案

    asp.net 微信支付 错误解决方案 在网上看到有人解决方案为: 解决方法 出现这种错误网上查出现有的原因是: 订阅号没有相关的权限 账号没有认证,没有相关的权限 那么这里遇到问题两种都不是.开发账 ...

  10. 执行大量的Redis命令,担心效率问题?用Pipelining试试吧~

    参考的优秀文章 Request/Response protocols and RTT 来源 原来,系统中一个树结构的数据来源是Redis,由于数据增多.业务复杂,查询速度并不快.究其原因,是单次查询的 ...