Question
FrequecyTester.java class MinHeap{ // array storing items in the min-heap int[] arr; int size; // heap size public MinHeap(int MAXSIZE){ // construct an array and
FrequecyTester.java
class MinHeap{ // array storing items in the min-heap int[] arr; int size; // heap size public MinHeap(int MAXSIZE){ // construct an array and set the size to 0 arr = new int[MAXSIZE]; size = 0; }
public int min(){ // return the minimum value in the heap return arr[0]; }
public int removeMin(){ // return and remove the minimum value int result = arr[0]; arr[0] = arr[size - 1]; size--; downHeap(0); return result; }
public void upHeap(int index){ int parentIndex = (index - 1) / 2; int temp; while (parentIndex >= 0 && arr[parentIndex] > arr[index]){ temp = arr[index]; arr[index] = arr[parentIndex]; arr[parentIndex] = temp; index = parentIndex; parentIndex = (index - 1) / 2; } }
public void downHeap(int index){ boolean done = false; int temp, leftIndex, rightIndex, targetIndex; while (!done){ leftIndex = 2*index + 1 ; rightIndex = 2*index + 2; if (leftIndex >= size){ done = true; } else { targetIndex = leftIndex; if (rightIndex
public void insert(int value){ // insert a new value in the heap arr[size] = value; upHeap(size); size++; }
public void printArray(){ // print the array representation of the heap for (int i=0; i FrequencyList.java Its to be in java language smallest.txt hello there, how are you? I am fine. Thank you.class FrequencyList { Node head; int count; FrequencyList() { head = null; count = 0; } //search for node in linked list that contains key. Returns a reference to node if found, else returns null. public Node search(String key) { Node curr=head; while (curr != null) { if (key.compareTo(curr.data.symbol) == 0) { break; } curr = curr.next; } return curr; } //print contents of linked list. public void printList() { Node curr; curr = head; while (curr != null) { //print each node curr.print(); curr = curr.next; } } //insert a new node if key is not in the linked list. Otherwise increment frequency by 1. public void insert(String key) { Node curr; curr = search(key); if (curr == null) { //key not in list, add it to front. curr = new Node(key); //insert new node at front of list curr.next = head; head = curr; count = count + 1; } else { //already in list. Increment the frequency by 1. curr.data.frequency = curr.data.frequency + 1; //increment frequency of symbol } } } //A node of the Linked List class FrequencyList. class Node { FrequencyData data; Node next; Node(String key) { data = new FrequencyData(key,1); } void print() { data.print(); } } //This class contains a symbol and its frequency class FrequencyData { String symbol; //using a string type will become handy when build the Huffman encoding tree. int frequency; FrequencyData (FrequencyData data) { symbol = data.symbol; frequency = data.frequency; } FrequencyData(String s, int freq) { symbol = s; frequency = freq; } //print content of symbol (which may contain more than one character //for debugging purposes only. void printSymbol() { int i; int a; String s=""; for (i = 0; i "; //instead of printing a newline, print
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started