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创建用户: CREATE USER 用户名 IDENTIFIED BY 口令 [ACCOUNT LOCK|UNLOCK] [注]LOCK|UNLOCK创建用户时是否锁定,默认为锁定状态.锁 ...

  2. JDBC连接mysql数据库,添加数据

    如下:其中添加/删除/修改只是sql字符串不同 //3.要执行的字符串 String sql="INSERT INTO t_student(NAME,age,email) VALUES('x ...

  3. Nginx安装注意事项

    因为nginx需要依赖pcre库.zlib库.openssl库,所以需要下载这三个库以及nginx源码.       下载以上文件到/usr/local/src/目录下     使用tar -zxvf ...

  4. Pyhton 学习总结 20 :执行系统命令

    在Python中执行系统命令有os.system().os.popen().commands.getstatusoutput().subprocess.Popen等     1.os.system() ...

  5. asp获取文件名和扩展名的函数代码

    <% '获取文件名(不含扩展名) Function getFilename(text)text = Left(text,inStrRev(text,".")-1)getFil ...

  6. OAuth2.0概述

    OAuth2.0较1.0相比,整个授权验证流程更简单更安全,也是未来最主要的用户身份验证和授权方式. 关于OAuth2.0协议的授权流程可以参考下面的流程图,其中Client指第三方应用,Resour ...

  7. js日期选择控件

    // 日期选择 // By Ziyue(http://www.web-v.com/) // 使用方法: // <script type="text/javascript" s ...

  8. 实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty 和Staff。

    (1)Person类中的属性有:姓名name(String类型),地址address(String类型), 电话号码telphone(String类型)和电子邮件地址email(String类型): ...

  9. C++开源大全

    程序员要站在巨人的肩膀上,C++拥有丰富的开源库,这里包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++ Standard Library:是一系列 ...

  10. R----plotly包介绍学习

    plotly包:让ggplot2的静态图片变得可交互 Plotly 是个交互式可视化的第三方库,官网提供了Python,R,Matlab,JavaScript,Excel的接口,因此我们可以很方便地在 ...