I would like help with fixing this code so the output is right public class Main {     /*      * tree.txt should produce this tree:      * 16      * / \      * 6 Z      * / \      * A D      *      * A (1), D (5), Z (10)      *      * Codes:      * A: 00      * D: 01      * Z: 1      *      * Preorder Traversal:      * 16, 6, 1 (A), 5 (D), 10 (Z)      */   }   import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;   public class Huffman {     private Tree root;     private Map characterCodes;       public Huffman() {         characterCodes = new HashMap();     }       public void buildTreeFromFile(String filePath) throws FileNotFoundException {         List nodes = new ArrayList();         populateNodesFromFile(filePath, nodes);         root = buildTreeFromNodes(nodes);           buildCharacterCodes(root, "");     }       private void populateNodesFromFile(String filePath, List nodes) throws FileNotFoundException {         try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {             String line = reader.readLine();             String[] parts = line.split(",");             char ch = parts[0].charAt(0);             int freq = Integer.parseInt(parts[1]);             nodes.add(new Tree(null, null, ch, freq));                 } catch (IOException ex) {             System.err.format("IOException: %s%n", ex);         }     }       private Tree buildTreeFromNodes(List nodes) {         while (nodes.size() > 1) {             Collections.sort(nodes);             Tree left = nodes.remove(0);             Tree right = nodes.remove(0);             Tree parent = new Tree(left, right, '\0', left.getFrequency() + right.getFrequency());             nodes.add(parent);         }         return nodes.get(0);     }       private void buildCharacterCodes(Tree tree, String code) {         if (tree == null) {             return;         }         if (tree.getLeft() == null && tree.getRight() == null) {             characterCodes.put(tree.getCharecter(), code);             return;         }         buildCharacterCodes(tree.getLeft(), code + "0");         buildCharacterCodes(tree.getRight(), code + "1");     }       public void printTree() {         if (root != null) {             root.printTree();         }     }       public String getCode(char ch) {         String code = characterCodes.get(ch);             if (code == null) {             return "";         }         return code;     }       public String encode(String value) {         StringBuilder result = new StringBuilder();         for (int i = 0; i < value.length(); i++) {             char ch = value.charAt(i);             result.append(getCode(ch));         }         return result.toString();     }       public String decode(String code) {         StringBuilder result = new StringBuilder();         Tree node = root;         for (int i = 0; i < code.length(); i++) {             char ch = code.charAt(i);             if (ch == '0') {                 node = node.getLeft();             } else {                 node = node.getRight();             }             if (node.getCharecter() != '\0') {                 result.append(node.getCharecter());                 node = root;             }         }         return result.toString();     } }   public class Tree implements Comparable {         private Tree left;     private Tree right;     private char charecter;     private int frequency;       public Tree(Tree left, Tree right, char charecter, int frequency) {         this.left = left;         this.right = right;         this.charecter = charecter;         this.frequency = frequency;     }       public Tree getLeft() {         return left;     }       public Tree getRight() {         return right;     }       public int getFrequency() {         return frequency;     }       public char getCharecter() {         return charecter;     }        public void printTree() {         _printTree(this);     }       public void _printTree(Tree n) {         if (n != null) {             System.out.print(n.toString() + ",");             _printTree(n.getLeft());             _printTree(n.getRight());         }     }       @Override     public String toString() {         return String.format("%s (%2d)", getCharecter(), getFrequency());     }       @Override     public int compareTo(Tree other) {         if (this.frequency == other.frequency) {             return Integer.compare(this.frequency, other.frequency);         }         return Character.compare(this.charecter, other.charecter);     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I would like help with fixing this code so the output is right

public class Main {

    /*

     * tree.txt should produce this tree:

     * 16

     * / \

     * 6 Z

     * / \

     * A D

     *

     * A (1), D (5), Z (10)

     *

     * Codes:

     * A: 00

     * D: 01

     * Z: 1

     *

     * Preorder Traversal:

     * 16, 6, 1 (A), 5 (D), 10 (Z)

     */

  }

 

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

public class Huffman {

    private Tree root;

    private Map<Character, String> characterCodes;

 

    public Huffman() {

        characterCodes = new HashMap<Character, String>();

    }

 

    public void buildTreeFromFile(String filePath) throws FileNotFoundException {

        List<Tree> nodes = new ArrayList<Tree>();

        populateNodesFromFile(filePath, nodes);

        root = buildTreeFromNodes(nodes);

 

        buildCharacterCodes(root, "");

    }

 

    private void populateNodesFromFile(String filePath, List<Tree> nodes) throws FileNotFoundException {

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {

            String line = reader.readLine();

            String[] parts = line.split(",");

            char ch = parts[0].charAt(0);

            int freq = Integer.parseInt(parts[1]);

            nodes.add(new Tree(null, null, ch, freq));

       

        } catch (IOException ex) {

            System.err.format("IOException: %s%n", ex);

        }

    }

 

    private Tree buildTreeFromNodes(List<Tree> nodes) {

        while (nodes.size() > 1) {

            Collections.sort(nodes);

            Tree left = nodes.remove(0);

            Tree right = nodes.remove(0);

            Tree parent = new Tree(left, right, '\0', left.getFrequency() + right.getFrequency());

            nodes.add(parent);

        }

        return nodes.get(0);

    }

 

    private void buildCharacterCodes(Tree tree, String code) {

        if (tree == null) {

            return;

        }

        if (tree.getLeft() == null && tree.getRight() == null) {

            characterCodes.put(tree.getCharecter(), code);

            return;

        }

        buildCharacterCodes(tree.getLeft(), code + "0");

        buildCharacterCodes(tree.getRight(), code + "1");

    }

 

    public void printTree() {

        if (root != null) {

            root.printTree();

        }

    }

 

    public String getCode(char ch) {

        String code = characterCodes.get(ch);

            if (code == null) {

            return "";

        }

        return code;

    }

 

    public String encode(String value) {

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < value.length(); i++) {

            char ch = value.charAt(i);

            result.append(getCode(ch));

        }

        return result.toString();

    }

 

    public String decode(String code) {

        StringBuilder result = new StringBuilder();

        Tree node = root;

        for (int i = 0; i < code.length(); i++) {

            char ch = code.charAt(i);

            if (ch == '0') {

                node = node.getLeft();

            } else {

                node = node.getRight();

            }

            if (node.getCharecter() != '\0') {

                result.append(node.getCharecter());

                node = root;

            }

        }

        return result.toString();

    }

}

 

public class Tree implements Comparable<Tree> {

   

    private Tree left;

    private Tree right;

    private char charecter;

    private int frequency;

 

    public Tree(Tree left, Tree right, char charecter, int frequency) {

        this.left = left;

        this.right = right;

        this.charecter = charecter;

        this.frequency = frequency;

    }

 

    public Tree getLeft() {

        return left;

    }

 

    public Tree getRight() {

        return right;

    }

 

    public int getFrequency() {

        return frequency;

    }

 

    public char getCharecter() {

        return charecter;

    }

 

     public void printTree() {

        _printTree(this);

    }

 

    public void _printTree(Tree n) {

        if (n != null) {

            System.out.print(n.toString() + ",");

            _printTree(n.getLeft());

            _printTree(n.getRight());

        }

    }

 

    @Override

    public String toString() {

        return String.format("%s (%2d)", getCharecter(), getFrequency());

    }

 

    @Override

    public int compareTo(Tree other) {

        if (this.frequency == other.frequency) {

            return Integer.compare(this.frequency, other.frequency);

        }

        return Character.compare(this.charecter, other.charecter);

    }

}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Mergesort
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education