zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    C# 文件夹的常用操作
    c语言学习笔记---预编译
    linux学习笔记
    c语言学习笔记---符号
    vim 编辑基础使用-----linux编程
    用pywinauto进行win32应用程序的测试
    Python中下划线的使用方法
    创建可维护的自动化验收测试
    Appium+Robotframework实现Android应用的自动化测试-1:Appium在Windows中的安装
    在中文windows下使用pywinauto进行窗口操作
  • 原文地址:https://www.cnblogs.com/z2529827226/p/11620666.html
Copyright © 2011-2022 走看看