Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help figuring out what is wrong with this code? It reads a txt file and p

ID: 3662425 • Letter: I

Question

I need help figuring out what is wrong with this code? It reads a txt file and populates a Binary Tree so it can be displayed in JTree.

The JTree only shows the first name in the list.

The code is copied below.


package binarytreedisplay;

import java.util.*;
import javax.swing.*;
import java.io.*;
import javax.swing.tree.*;

public class BinaryTreeDisplay {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{

BinaryTree bTree = new BinaryTree();
bTree.buildTreeFromFile();

JFrame myFrame = new JFrame("Binary Tree Example");
myFrame.add(bTree.getJTreeView());
myFrame.pack();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);

}
  
}

class BinaryTree
{
static class Node
{
private String name;
private Node left, right;
private Node(String name)
{
this.name = name;
left = null; right = null;
}
private Node(String name, Node left, Node right)
{
this.name = name;
this.left = left;
this.right = right;
}
}
private Node root = null;

/**
*
* @return the JTree corresponding to this binary tree
*/
JTree getJTreeView()
{
DefaultMutableTreeNode dMutableTreeNodeRoot = getTreeNodeRoot(root);
DefaultTreeModel treeModel = new DefaultTreeModel(dMutableTreeNodeRoot);
return new JTree(treeModel);
}

/**
*
* @param tree: A node that is the root of some subtree of a binary tree
* @return The DefaultMutableTreeNode object corresponding to tree.
*/
static DefaultMutableTreeNode getTreeNodeRoot(Node tree)
{
if (tree == null) return null;
DefaultMutableTreeNode mTreeNode = new DefaultMutableTreeNode(tree.name);
if (tree.left != null)
mTreeNode.add(getTreeNodeRoot(tree.left));
if (tree.right != null)
mTreeNode.add(getTreeNodeRoot(tree.right));
return mTreeNode;
}

/**
* Builds the binary tree from data stored in a file.
* The user is asked to choose the file.
* @throws IOException
*/
void buildTreeFromFile() throws IOException
{
// Use a map to help in building the binary tree
// The name for a node is used a key in the map to locate the node
Map<String, Node> nodeMap = new HashMap<String, Node>();

// Ask the user for the file containing the binary tree's adjacency list
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
if (result != JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "No file selected, program will exit.");
System.exit(1);
}
File file = chooser.getSelectedFile();
// Read the file data using a scanner and build the tree
Scanner sc = new Scanner(file);
int numberVertices = sc.nextInt();
sc.nextLine();
  
// Read the data for each node
for (int k = 0; k < numberVertices; k++)
{
String inputLine = sc.nextLine();
  
String [] parts = inputLine.split(" ");
Node [] treeNodes = new Node[parts.length];
for (int i = 0; i < parts.length; i++)
treeNodes[i] = getNode(parts[i], nodeMap);

// Set the children of the first node on this line of input
// if needed
if (parts.length >= 2) treeNodes[0].left = treeNodes[1];
if (parts.length == 3) treeNodes[0].right = treeNodes[2];
if (k == 0) root = treeNodes[0];
  
  
}
}
/**
* Gets or creates a node with a given name
* Looks up the node in the given map, and returns it if found.
* If the node is not found in the map, a new node with that name
* is created, stored in the map, and returned.
* @param name
* @param nodeMap
* @return a node in the map that has the given name
*/
static Node getNode(String name, Map<String, Node> nodeMap)
{
Node aNode = nodeMap.get(name);
if (aNode == null)
{
//Create a new mode with this name, store it in the map
aNode = new Node(name);
nodeMap.put(name, aNode);
return aNode;
}
return aNode;
}
}

Explanation / Answer

Possible errors are


Exception in thread "main" java.util.InputMismatchException
   at java.util.Scanner.throwFor(Unknown Source)
   at java.util.Scanner.next(Unknown Source)
   at java.util.Scanner.nextInt(Unknown Source)
   at java.util.Scanner.nextInt(Unknown Source)
   at asd.BinaryTree.buildTreeFromFile(BinaryTreeDisplay.java:90)
   at asd.BinaryTreeDisplay.main(BinaryTreeDisplay.java:16)

Shows errors in this line
int numberVertices = sc.nextInt();

   Shows errors in this line
   bTree.buildTreeFromFile();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote