Advertisement
Vivianny

Heap

Apr 24th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Heap {
  3.  
  4.     static int vetor[] ={15,21,23,48,35,70,67};
  5.     static int lista = vetor.length;
  6.     static int l;
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner (System.in);
  10.         int op, tam=0, ind, num;
  11.  
  12.  
  13.         do{
  14.  
  15.             System.out.println("\n Menu de opções - HEAP MÁXIMO");
  16.             System.out.println("1-Números da lista");
  17.             System.out.println("2- Consultar o elemento de maior prioridade");
  18.             System.out.println("3- Remover o elemento de maior prioridade");
  19.             System.out.println("4-Consultar toda a lista");
  20.             System.out.println("5-Sair");
  21.             System.out.println("Digite a sua opção:");
  22.             op=sc.nextInt();
  23.  
  24.             if(op<1|| op>5){
  25.                 System.out.println("Opção inváloda!!!");
  26.  
  27.             }
  28.             if(op==1){
  29.                 if(tam < vetor.length-1){
  30.                     tam++;
  31.                     for( l = 0; l < lista; l++){
  32.                         System.out.println(vetor[l]);
  33.                     }
  34.                     ind=tam;
  35.                     while(ind>1 && vetor[ind]<vetor[l]){
  36.                         vetor[ind]=vetor[ind];
  37.                         ind=ind;
  38.                     }
  39.                     vetor[ind]=vetor[tam];
  40.                     System.out.println("Número inserido");
  41.                 }else{
  42.                     System.out.println("Lista de prioridades lotada");
  43.  
  44.                 }
  45.             }
  46.             if(op==2){
  47.                 if(tam==0){
  48.                     System.out.println("Lista de prioridades vazia!!");
  49.                 }else{
  50.                     System.out.println("Elemento de maior prioridade: " +vetor[1]);
  51.  
  52.                 }
  53.             }
  54.             if(op==3){
  55.                 if(tam==0){
  56.                     System.out.println("Lista de prioridades vazia");
  57.                 }else{
  58.                     int maior_prior=vetor[1];
  59.                     vetor[1]=vetor[tam];
  60.                     tam--;
  61.                     heap_fica(1,tam);
  62.                     System.out.println("O elemento rovido:" +maior_prior);
  63.                 }
  64.             }
  65.             if(op==4){
  66.                 if(tam==0){
  67.                     System.out.println("Lista de prioridade vazia!!");
  68.                 }else{
  69.                     System.out.println("\n todos os elementos da lista de prioridade:\n");
  70.                     for(int j=1;j<=tam;j++){
  71.                         System.out.println(vetor[j]+"");
  72.  
  73.                     }
  74.                 }
  75.             }
  76.             if(op==5){
  77.                 break;
  78.             }
  79.  
  80.         }while(op!=6);
  81.  
  82.  
  83.     }
  84.  
  85.     public static int pai (int x){
  86.         return x/2;
  87.     }
  88.     private static void heap_fica(int i, int qtde) {
  89.         int f_esq,f_dir,maior,aux;
  90.         maior=i;
  91.         if (2*i+1<=qtde){
  92.             f_esq=2*i+1;
  93.             f_dir=2*i;
  94.             if(vetor[f_esq]>= vetor[f_dir] && vetor[f_esq] > vetor[i]){
  95.                 maior= 2*i+1;
  96.  
  97.             }else if(vetor[f_dir]>vetor[f_esq] && vetor[f_dir]>vetor[i]){
  98.                 maior=2*i;
  99.  
  100.             }else if(maior!=i){
  101.                 aux=vetor[i];
  102.                 vetor[i]=vetor[maior];
  103.                 vetor[maior]=aux;
  104.                 heap_fica(maior,qtde);
  105.             }
  106.         }
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement