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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
Attack and Defense Labs
Attack and Defense Labs
Jina AI
Jina AI
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
H
Help Net Security
V
Visual Studio Blog
宝玉的分享
宝玉的分享
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
Latest news
Latest news
T
Tor Project blog
I
Intezer
美团技术团队
GbyAI
GbyAI
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
Y
Y Combinator Blog
博客园 - 司徒正美
T
Tenable Blog
O
OpenAI News
N
News and Events Feed by Topic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
Vulnerabilities – Threatpost
P
Palo Alto Networks Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
Google Online Security Blog
Google Online Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
Help Net Security
Help Net Security

博客园 - 一个人的合唱

手写简易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());
    }
}