• Java优先队列的简单实现


    最近在学习最小生成树时,用到了优先队列这个结构,琢磨这自己也来写下,搞了半天终于写出来了,于是就记录下
    import java.util.ArrayList;
    
    class MyHeap<Type  extends Comparable<Type>>{
        
      private ArrayList<Type> data;
      private int MaxSize;
      private int size;
      
      public MyHeap() {
          this.MaxSize=0;
          this.size=0;
      }
      
      public boolean add(Type Elem) {
          if(this.size>=this.MaxSize) {
              MaxSize=MaxSize+((MaxSize>>1)>1?(MaxSize>>1):1);
              ArrayList<Type>temp=new ArrayList<Type>(MaxSize);
              for(int i=0;i<this.size;++i) {
                  temp.add(i, data.get(i));
              }
              data=temp;
          }
          data.add(Elem);
          int childIndex=this.size;
          Type childNode=data.get(size);
          while(childIndex>=0) {
              int parentIndex=(childIndex-1)>>1;
              if(parentIndex<0) {
                 break;
              }
              if(data.get(parentIndex).compareTo(childNode)<0) {
                  data.remove(childIndex);
                  data.add(childIndex, data.get(parentIndex));
              }else {
                  break;
              }
              childIndex=parentIndex;
          }
          data.remove(childIndex);
          data.add(childIndex, childNode);
          this.size+=1;
          return true;
      }
      public boolean showHeap() {
          for(int i=0;i<this.size;++i) {
              System.out.print(data.get(i)+" ");
          }
          return true;
      }
    
      public boolean delete(){
          if(this.size>=1){
            this.size-=1;   
            data.add(1, data.get(this.size));
            data.remove(0);
            int parentIndex=0;
            Type parentNode=data.get(parentIndex);
            int Index=(parentIndex<<1)+1;
            while(Index+1<this.size) {
                if(data.get(Index).compareTo(data.get(Index+1))<0) {
                    Index++;
                }
                if(data.get(Index).compareTo(parentNode)>0) {
                    data.add(parentIndex, data.get(Index));
                    data.remove(parentIndex+1);
                }
                else {
                    break;
                }
                parentIndex=Index;
                Index=(parentIndex<<1)+1;     
              }
           data.add(parentIndex, parentNode);
           data.remove(parentIndex+1);
           return true;
          }    
          else {
              return false;
          }
          
        }
    }
    
    public class Main {
       public static void main(String[] args) {
        MyHeap<Integer>space=new MyHeap<Integer>();
        space.add(12);
        space.add(4);
        space.add(17);
        space.add(56);
        space.add(20);
        space.add(30);
        space.add(546);
        space.add(53);
        space.delete();
        space.showHeap();
        }
    }
  • 相关阅读:
    webSocket
    Spring中注解大全和应用
    原生js替换jQuery各种方法-中文版
    正则表达式-基础知识Review
    Swoole 4.1.0 正式版发布,支持原生 Redis/PDO/MySQLi 协程化
    Javascript 模块化指北
    精读《sqorn 源码》
    nodejs源码—初始化
    ubuntu显卡驱动安装
    iOS推断当前控制器是否在显示
  • 原文地址:https://www.cnblogs.com/z2529827226/p/11620666.html
Copyright © 2020-2023  润新知