堆的操作

来源:互联网 发布:中国民航大学就业 知乎 编辑:程序博客网 时间:2024/04/29 04:56

package 堆;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*堆是一种完全二叉树,不是java或C++中的语言中的“堆”(程序员用new能得到计算机内存中的可用部分),
 若数组中节点的索引为x,则
 1.他的父节点的下标为(x-1)/2
 2.他的左子节点的下标为2*x+1
 3.他的右子节点的下标为2*x+2
 */
class  Node
{
   private int idata;
   public Node(int a)
   {
    idata=a;
   }
   public int getIdata() {
 return idata;
}
public void setIdata(int idata) {
 this.idata = idata;
}


}

class Heep
{
    private Node[] array;
    private int maxSize;
    private int currentSize;
    public Heep(int max)
    {
     maxSize=max;
     array=new Node[max];
     currentSize=0;
    }
    public Node remove()
    {
       Node root=array[0];
       array[0]=array[--currentSize];
       trickleDown(0);
       return root;
    }
    public boolean isEmpty()
    {
       return currentSize==0; 
    }
      
    public void insert(int key)
    {
     if(currentSize==maxSize)
      System.out.println("can not insert ");
     else
     {
      Node node=new Node(key);
      array[currentSize]=node;
         trickleUp(currentSize++);
     }
     
   
      
    }
    public void display()
    {
     System.out.print("heepArray:");
     for(int m=0;m<currentSize;m++)
     {
      if(array[m]!=null)
       System.out.print(array[m].getIdata()+" ");
      else
       System.out.print("--");
      
      
     }
     System.out.println();
     int nBlanks=32;
     int itemsPerRow=1;
     int column=0;
     int j=0;
     String dos=".........................................";
     System.out.println(dos+dos);
     while(currentSize>0)
     {
      if(column==0)
       for(int k=0;k<nBlanks;k++)
       {
        System.out.print(' ');
       }
     
     System.out.print(array[j].getIdata());
     if(++j==currentSize)
      break;
     if(++column==itemsPerRow)
     {
      nBlanks/=2;
      itemsPerRow*=2;
      column=0;
      System.out.println();
     }
     else
      for(int k=0;k<nBlanks*2-2;k++)
      {
       System.out.print(' ');
      }
     }
     System.out.print("/n"+dos+dos);    
    
     
    }
    public void trickleDown(int index)
    {
     int largerChild;
     Node top=new Node(index);
     while(index<currentSize/2)//只要这个节点不是叶子节点
     {
      int leftChild=2*index+1;
      int rightChild=leftChild+1;
      if(rightChild<currentSize&&array[leftChild].getIdata()<array[rightChild].getIdata())
              largerChild=rightChild;
      else
       largerChild=leftChild;
      if(top.getIdata()>=array[largerChild].getIdata())
       break;
       array[index]=array[largerChild];
       index=largerChild;
     }
     array[index]=top;
    }
    public void trickleUp(int index)
    {
     int parent=(index-1)/2;
     Node bottom=array[index];//此处出错了。
     while(index>0&&array[parent].getIdata()<bottom.getIdata())
     {
      array[index]=array[parent];
      index=parent;
      parent=(index-1)/2;
     }
     array[index]=bottom;
    }
}
public class heap
{
    public static void main(String[] args)
    {
     int value,value2;
     Heep heep=new Heep(31);
     boolean success;
     /*heep.insert(20);
     heep.insert(50);
     heep.insert(40);
     heep.insert(80);
     heep.insert(70);
     heep.insert(30);*/
     
     for(int i=0;i<10;i++)
     {
      int a=(int)(java.lang.Math.random()*99);
      System.out.print(a+" ");
      
      heep.insert(a);
     }
     heep.display();
     
    }
    public static String getString() throws IOException
    {
     InputStreamReader isr=new InputStreamReader(System.in);
     BufferedReader br=new BufferedReader(isr);
   
        return br.readLine();   
    
    }
    public static int getInt() throws IOException
    {
     String ss=getString();
     int a=Integer.parseInt(ss);
     return a;
    }
    public static char getChar() throws IOException
    {
     String ss=getString();
     return ss.charAt(0);
    }
}

原创粉丝点击