惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 一个人的合唱

手写简易docker synchronized 泛型学习 反码与补码 解决128位秘钥长度限制的方法 CAP学习 ThreadLocal的学习 读书笔记脑图列表 a=a++和a=++a centos7下安装python3 深入理解jvm虚拟机笔记(二) AtomicInteger的使用 volatile学习 echarts重新绘制的时候数据未更新 深入理解jvm虚拟机笔记(一) elasticsearch学习笔记 使用fiddler模拟post请求 二叉平衡树 二叉查找树
二叉堆
一个人的合唱 · 2016-03-11 · via 博客园 - 一个人的合唱

1.构造原理:权值小的节点应该处于树的下面,他们必是作为孩子,因此选择权值小的节点作为子节点自底向上构造树。根据二叉树的特点,两个节点合并后对于剩下来说他们相当于一个节点而已,所以计算权值和把其当作一个新的节点放到未构造的节点集中。不断构造合并,最后便会使kmp最小

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 刘逗逼 on 2016/3/11.
 */
public class MaxHeap<T extends Comparable<T>> {
    private List<T> head;
    public MaxHeap(){
        this.head=new ArrayList<T>();
    }
    protected void filterup(int start){          //向上调整,用于插入
        int child=start;
        int parent=(child-1)/2;                 //因为数组下标从0开始
        T tmp=head.get(child);
        while(true){
            int cmp=head.get(parent).compareTo(tmp);
            if(cmp>0){
                break;
            }else {             //换位
                head.set(child,head.get(parent));
                child=parent;
                parent=(parent-1)/2;
            }
        }
        head.set(child,tmp);
    }
    protected void filterdown(int start,int end){       //end是判断是否出界的
        int parent=start;
        int left=2*parent+1;
        T tmp=head.get(parent);
        while(left<=end){
            int cmp=head.get(left).compareTo(head.get(left+1));
            if(cmp<0)left=+1;
            head.set(parent,head.get(left));
            cmp=head.get(parent).compareTo(head.get(left));
            if(cmp>0){
                break;
            }else {
                head.set(parent,head.get(left));
                parent=left;
                left=left*2+1;
            }
        }
        head.set(parent,tmp);
    }
      /*
       * 删除最大堆中的data
       *
       * 返回值:
       *      0,成功
       *     -1,失败
       */

    protected int remove(T data){
        if(head.isEmpty()){
            return -1;
        }
        int index=head.indexOf(data);
        if(index==-1)return -1;
        int size=head.size();
        head.set(index,head.get(size-1));
        head.remove(size-1);
        if(head.size()>1){
            filterdown(index,head.size()-1);
        }
        return 0;
    }
    protected void insert(T data){
        head.add(data);
        filterup(head.size()-1);
    }
    @Override
    public String toString(){
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<head.size();i++){
            stringBuilder.append(head.get(i)+" ");
        }
        return stringBuilder.toString();
    }
    public static void main(String[] args){
        int i;
        int a[]={10,40,30,60,90,70,20,50,80};
        MaxHeap<Integer> tree=new MaxHeap();
        System.out.println("== 依次添加: ");
        for( i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
            tree.head.add(a[i]);
        }
        System.out.println("");
        System.out.println("==添加最大堆  "+tree.toString());
        i=85;
        tree.insert(i);
        System.out.println("==添加元素: "+i);
        System.out.println("==最大堆     "+tree.toString());
        i=90;
        tree.remove(i);
        System.out.println("==删除元素: "+i);
        System.out.println("==最大堆     "+tree.toString());
    }
}