Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class s18840 {
  6.  
  7.     public static void main(String[] args) {
  8.         String str;
  9.         int freq;
  10.         int sizeForHeap = 0;
  11.         int total = 0;
  12.  
  13.         try {
  14.             Scanner scanner = new Scanner(new File(args[0]));
  15.  
  16.             while (scanner.hasNextLine() && !scanner.equals(null)) {
  17.                 scanner.nextLine();
  18.                 sizeForHeap++;
  19.             }
  20.             //System.out.println(sizeForHeap);
  21.             scanner.close();
  22.  
  23.             scanner = new Scanner(new File(args[0]));
  24.             MinHeap minHeap = new MinHeap(sizeForHeap);
  25.             while (scanner.hasNext()) {
  26.                 str = scanner.next();
  27.                 freq = scanner.nextInt();
  28.  
  29.                 total += freq;
  30.  
  31.                 minHeap.insert(new NodeTree(str, freq));
  32.  
  33.             }
  34.             minHeap.minHeap();
  35.             while (minHeap.getSize() >= 2) {
  36.                 NodeTree min1 = minHeap.delMin();
  37.                 NodeTree min2 = minHeap.delMin();
  38.                 NodeTree parent = new NodeTree(min1.getSign() + min2.getSign(), min1.getValue() + min2.getValue());
  39.                 parent.setLeft(min2);
  40.                 parent.setRight(min1);
  41.                 minHeap.insert(parent);
  42.  
  43.             }
  44.  
  45.  
  46.             minHeap.print();
  47.  
  48.  
  49.         } catch (FileNotFoundException e) {
  50.             e.printStackTrace();
  51.         }
  52.  
  53.     }
  54.  
  55. }
  56.  
  57. class NodeTree {
  58.     int value;
  59.     String sign;
  60.     NodeTree left;
  61.     NodeTree right;
  62.  
  63.     public NodeTree(String sign, int value) {
  64.         this.sign = sign;
  65.         this.value = value;
  66.         left = null;
  67.         right = null;
  68.     }
  69.  
  70.     public NodeTree(int value) {
  71.         this.value = left.value + right.value;
  72.     }
  73.  
  74.     public String getSign() {
  75.         return sign;
  76.     }
  77.  
  78.     public void setSign(String sign) {
  79.         this.sign = sign;
  80.     }
  81.  
  82.     public int getValue() {
  83.         return value;
  84.     }
  85.  
  86.     public NodeTree getLeft() {
  87.         return left;
  88.     }
  89.  
  90.     public NodeTree getRight() {
  91.         return right;
  92.     }
  93.  
  94.     public void setLeft(NodeTree left) {
  95.         this.left = left;
  96.     }
  97.  
  98.     public void setRight(NodeTree right) {
  99.         this.right = right;
  100.     }
  101. }
  102.  
  103. class MinHeap {
  104.     private NodeTree[] Heap;
  105.     private int size;
  106.     private int maxsize;
  107.     private static final int FRONT = 1;
  108.  
  109.     public MinHeap(int maxsize) {
  110.         this.maxsize = maxsize;
  111.         this.size = 0;
  112.         Heap = new NodeTree[this.maxsize + 1];
  113.         Heap[0] = new NodeTree(null, Integer.MIN_VALUE);
  114.     }
  115.  
  116.     public int getSize() {
  117.         return size;
  118.     }
  119.  
  120.     private int parent(int pos) {
  121.         return pos / 2;
  122.     }
  123.  
  124.     private int leftChild(int pos) {
  125.         return (2 * pos);
  126.     }
  127.  
  128.     private int rightChild(int pos) {
  129.         return (2 * pos) + 1;
  130.     }
  131.  
  132.     private boolean isLeaf(int pos) {
  133.         if (pos >= (size / 2) && pos <= size && pos != 1) {
  134.             return true;
  135.         }
  136.         return false;
  137.     }
  138.  
  139.     private void swap(int fpos, int spos) {
  140.         NodeTree tmp;
  141.         tmp = Heap[fpos];
  142.         Heap[fpos] = Heap[spos];
  143.         Heap[spos] = tmp;
  144.     }
  145.  
  146.     private void minHeapify(int pos) {
  147.  
  148.         // If the node is a non-leaf node and greater
  149.         // than any of its child
  150.         if (!isLeaf(pos) && size > 1) {
  151.             if (Heap[pos].getValue() > Heap[leftChild(pos)].getValue()
  152.                     || Heap[pos].getValue() > Heap[rightChild(pos)].getValue()) {
  153.                 // Swap with the left child and heapify
  154.                 // the left child
  155.                 if (Heap[leftChild(pos)].getValue() < Heap[rightChild(pos)].getValue()) {
  156.                     swap(pos, leftChild(pos));
  157.                     minHeapify(leftChild(pos));
  158.                 }
  159.  
  160.                 // Swap with the right child and heapify
  161.                 // the right child
  162.                 else {
  163.                     swap(pos, rightChild(pos));
  164.                     minHeapify(rightChild(pos));
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.     public void insert(NodeTree element) {
  171.         if (size >= maxsize) {
  172.             return;
  173.         }
  174.         Heap[++size] = element;
  175.         int current = size;
  176.  
  177.         while (Heap[current].getValue() < Heap[parent(current)].getValue()) {
  178.             swap(current, parent(current));
  179.             current = parent(current);
  180.         }
  181.     }
  182.  
  183.     public void minHeap() {
  184.         for (int pos = (size / 2); pos >= 1; pos--) {
  185.             minHeapify(pos);
  186.         }
  187.     }
  188.  
  189.     public NodeTree delMin() {
  190.         NodeTree popped = Heap[FRONT];
  191.         Heap[FRONT] = Heap[size--];
  192.         minHeapify(FRONT);
  193.         return popped;
  194.     }
  195.  
  196.     public void print() {
  197.         for (int i = 1; i <= size; i++) {
  198.             System.out.println(Heap[i].getSign() + " " + Heap[i].getValue());
  199.         }
  200.     }
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement