Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class s18840 {
- public static void main(String[] args) {
- String str;
- int freq;
- int sizeForHeap = 0;
- int total = 0;
- try {
- Scanner scanner = new Scanner(new File(args[0]));
- while (scanner.hasNextLine() && !scanner.equals(null)) {
- scanner.nextLine();
- sizeForHeap++;
- }
- //System.out.println(sizeForHeap);
- scanner.close();
- scanner = new Scanner(new File(args[0]));
- MinHeap minHeap = new MinHeap(sizeForHeap);
- while (scanner.hasNext()) {
- str = scanner.next();
- freq = scanner.nextInt();
- total += freq;
- minHeap.insert(new NodeTree(str, freq));
- }
- minHeap.minHeap();
- while (minHeap.getSize() >= 2) {
- NodeTree min1 = minHeap.delMin();
- NodeTree min2 = minHeap.delMin();
- NodeTree parent = new NodeTree(min1.getSign() + min2.getSign(), min1.getValue() + min2.getValue());
- parent.setLeft(min2);
- parent.setRight(min1);
- minHeap.insert(parent);
- }
- minHeap.print();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- class NodeTree {
- int value;
- String sign;
- NodeTree left;
- NodeTree right;
- public NodeTree(String sign, int value) {
- this.sign = sign;
- this.value = value;
- left = null;
- right = null;
- }
- public NodeTree(int value) {
- this.value = left.value + right.value;
- }
- public String getSign() {
- return sign;
- }
- public void setSign(String sign) {
- this.sign = sign;
- }
- public int getValue() {
- return value;
- }
- public NodeTree getLeft() {
- return left;
- }
- public NodeTree getRight() {
- return right;
- }
- public void setLeft(NodeTree left) {
- this.left = left;
- }
- public void setRight(NodeTree right) {
- this.right = right;
- }
- }
- class MinHeap {
- private NodeTree[] Heap;
- private int size;
- private int maxsize;
- private static final int FRONT = 1;
- public MinHeap(int maxsize) {
- this.maxsize = maxsize;
- this.size = 0;
- Heap = new NodeTree[this.maxsize + 1];
- Heap[0] = new NodeTree(null, Integer.MIN_VALUE);
- }
- public int getSize() {
- return size;
- }
- 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 && pos != 1) {
- return true;
- }
- return false;
- }
- private void swap(int fpos, int spos) {
- NodeTree tmp;
- tmp = Heap[fpos];
- Heap[fpos] = Heap[spos];
- Heap[spos] = tmp;
- }
- private void minHeapify(int pos) {
- // If the node is a non-leaf node and greater
- // than any of its child
- if (!isLeaf(pos) && size > 1) {
- if (Heap[pos].getValue() > Heap[leftChild(pos)].getValue()
- || Heap[pos].getValue() > Heap[rightChild(pos)].getValue()) {
- // Swap with the left child and heapify
- // the left child
- if (Heap[leftChild(pos)].getValue() < Heap[rightChild(pos)].getValue()) {
- swap(pos, leftChild(pos));
- minHeapify(leftChild(pos));
- }
- // Swap with the right child and heapify
- // the right child
- else {
- swap(pos, rightChild(pos));
- minHeapify(rightChild(pos));
- }
- }
- }
- }
- public void insert(NodeTree element) {
- if (size >= maxsize) {
- return;
- }
- Heap[++size] = element;
- int current = size;
- while (Heap[current].getValue() < Heap[parent(current)].getValue()) {
- swap(current, parent(current));
- current = parent(current);
- }
- }
- public void minHeap() {
- for (int pos = (size / 2); pos >= 1; pos--) {
- minHeapify(pos);
- }
- }
- public NodeTree delMin() {
- NodeTree popped = Heap[FRONT];
- Heap[FRONT] = Heap[size--];
- minHeapify(FRONT);
- return popped;
- }
- public void print() {
- for (int i = 1; i <= size; i++) {
- System.out.println(Heap[i].getSign() + " " + Heap[i].getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement