Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

import java.util.*;
import java.io.*;

public class HuffmanCode {
     private Queue<HuffmanNode> queue;
        private HuffmanNode overallRoot;
        

        public HuffmanCode(int[] frequencies) {
                queue = new PriorityQueue<HuffmanNode>();
                
                for (int i = 0; i < frequencies.length; i++) {
                        if (frequencies[i] > 0) {
                                HuffmanNode node = new HuffmanNode(frequencies[i]);
                                node.ascii = i;
                                queue.add(node);
                        }
                }
                overallRoot = buildTree();      
        }
        

        public HuffmanCode(Scanner input) {
                overallRoot = new HuffmanNode(-1);
                while (input.hasNext()) {
                        int asciiValue = Integer.parseInt(input.nextLine());
                        String code = input.nextLine();
                        overallRoot = reconstructTree(overallRoot, code, asciiValue);
                }
        }
        

        private HuffmanNode buildTree() {
                if (queue.size() == 1) {
                        return queue.remove();
                } else {
                        HuffmanNode nodeLeft = queue.remove();
                        HuffmanNode nodeRight = queue.remove();
                        HuffmanNode nodeNew = new HuffmanNode(nodeLeft.frequency + nodeRight.frequency);
                        nodeNew.left = nodeLeft;
                        nodeNew.right = nodeRight;
                        queue.add(nodeNew);
                        return buildTree();
                }
        }
        

        private HuffmanNode reconstructTree(HuffmanNode root, String code, int asciiValue) {
                if (code.isEmpty()) {
                        root.ascii = asciiValue;
                        return root;
                } else {
                        char next = code.charAt(0);
                        if (next == '0') {
                                if (root.left == null) {
                                        root.left = new HuffmanNode(-1);
                                }
                                root.left = reconstructTree(root.left, code.substring(1), asciiValue);
                        } else if (next == '1') {
                                if (root.right == null) {
                                        root.right = new HuffmanNode(-1);
                                }
                               root.right = reconstructTree(root.right, code.substring(1), asciiValue);
                        }
                }
                return root;
        }
        

        public void save(PrintStream output) {
                save(output, overallRoot, "");
        }

        private void save(PrintStream output, HuffmanNode root, String code) {
                if (root != null) {
                        if (root.ascii != -1) {
                                output.println(root.ascii);
                                output.println(code);
                        }
                        save(output, root.left, code + "0");
                        save(output, root.right, code + "1");
                }
        }
        

        public void translate(BitInputStream input, PrintStream output) {
                while (input.hasNextBit()) {
                        translate(overallRoot, input, output);
                }
        }
        

        private void translate(HuffmanNode curr, BitInputStream input, PrintStream output) {
                if (curr.left == null && curr.right == null) {
                        output.write(curr.ascii);
                } else {
                        int token = input.nextBit();
                        if (token == 0) {
                                translate(curr.left, input, output);
                        } else {
                                translate(curr.right, input, output);
                        }
                }
        }
        
  
        private static class HuffmanNode implements Comparable<HuffmanNode> { 
                public int frequency;
                public int ascii;
                public HuffmanNode left;
                public HuffmanNode right;
                        
               
                public HuffmanNode(int frequency) {
                        this.frequency = frequency;
                        this.ascii = -1;
                }

  
                public int compareTo(HuffmanNode other) {
                        return this.frequency - other.frequency;
                }
        }
}

 

 

*code is provided and the error is in the picture, please help with how to fix the error* 

Failed:
You did not upload your secretmessage.short and/or
secretmessage.code properly
Hide stacktrace
org.opentest4j.AssertionFailedError:
You did not upload your secretmessage.short and/or secretmessage.code
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39)
at org.junit.jupiter.api. Assertions.fail(Assertions.java:109)
at HuffmanCodeTest.failFormatted(HuffmanCodeTest.java:54)
at HuffmanCodeTest.testFilesExistence (HuffmanCodeTest.java:270)
expand button
Transcribed Image Text:Failed: You did not upload your secretmessage.short and/or secretmessage.code properly Hide stacktrace org.opentest4j.AssertionFailedError: You did not upload your secretmessage.short and/or secretmessage.code at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39) at org.junit.jupiter.api. Assertions.fail(Assertions.java:109) at HuffmanCodeTest.failFormatted(HuffmanCodeTest.java:54) at HuffmanCodeTest.testFilesExistence (HuffmanCodeTest.java:270)
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education