Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
3.9 kB
10
Indexable
Never
Here is Solution for above program ::

 import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;  class Richest {     public static void main (String[] args) {         try {             int heapSize=10;   ////////////Here As per problem size should be 10000,please provide 10000 data into file             MaxHeap maxHeap = new MaxHeap(heapSize);            // args[0]="irs.txt";             System.out.println("File Name is :: "+args[0]);             File myObj = new File(args[0]);              Scanner myReader = new Scanner(myObj);             System.out.println("Input Data From File is :: ");             while (myReader.hasNextLine()) {                 String data = myReader.nextLine();                 maxHeap.insert(Integer.parseInt(data));                 System.out.print(data+" ");             }             myReader.close();               Richest(maxHeap,heapSize);          } catch (FileNotFoundException e) {             System.out.println("Error in File Not Found");             e.printStackTrace();         }     }       static void Richest(MaxHeap maxHeap,int size)     {         System.out.println("\nRichest Person's List :: ");          try {             File myObj = new File("richestOutput.txt");             if (myObj.createNewFile()) {                 System.out.println("File created!!");             } else {                 System.out.println("File already exists!!");             }             FileWriter myWriter = new FileWriter(myObj);             for(int i=0;i<size;i++) {                 myWriter.write(maxHeap.extractMax()+" ");             }             System.out.println("Data Successfully Added!");             myWriter.close();         } catch (IOException e) {             System.out.println("An error occured while output file creation");             e.printStackTrace();         }       } } class MaxHeap {     private int[] Heap;     private int size;     private int maxsize;          public MaxHeap(int maxsize)      {         this.maxsize=maxsize;         this.size=0;         Heap=new int[this.maxsize+1];         Heap[0]=Integer.MAX_VALUE;     }          private int parent(int pos)      {         return pos/2;     }     private int leftChild(int pos)      {         return (2*pos);     }      private int rightChild(int pos)      {         return (2*pos)+1;     }     private boolean isLeaf(int pos)      {         if (pos>=(size/2) && pos<=size)         {             return true;         }         return false;     }      private void swap(int a, int b)      {         int tmp;         tmp = Heap[a];         Heap[a] = Heap[b];         Heap[b] = tmp;     }          private void maxHeapify(int pos)      {         if (isLeaf(pos))             return;          if (Heap[pos] < Heap[leftChild(pos)] || Heap[pos] < Heap[rightChild(pos)])         {              if(Heap[leftChild(pos)] > Heap[rightChild(pos)])              {                 swap(pos, leftChild(pos));                 maxHeapify(leftChild(pos));             } else                  {                 swap(pos, rightChild(pos));                 maxHeapify(rightChild(pos));             }         }     }      public void insert(int element)      {         Heap[++size]=element;                  int current=size;         while (Heap[current] > Heap[parent(current)])          {             swap(current, parent(current));             current = parent(current);         }     }           public int extractMax()      {         int popped=Heap[1];         Heap[1]=Heap[size--];         maxHeapify(1);         return popped;     } }
Here is Some Test Cases ::

Input

./java Rechest irs.txt

put data in

irs.txt -->> 1 2 3 4 5 6 7 8 9 10

Output ::

Input Data From File is ::
1 2 3 4 5 6 7 8 9 10
Richest Person's List ::
File created!!
Data Successfully Added!

rechestOutput.txt will be created and inside that data willl be -->>
Leave a Comment