Question:
Error in Java execution (but it compiles)?
anonymous
2009-04-29 05:25:30 UTC
We are given a file with 99999 lines. Each line of the file represents a single node and it contains four integer: key, value, left child key, right child key (in the order specified). Key is a unique identifier assigned to each node. This identifier is just the line number. Value is the integer value stored in the node of the tree. Left and Right children are keys of the two appropriate children of the node. If a left or right child key is equal to zero, the appropriate child does not exist. The root node of the tree has key one.

Here are the first 10 lines, for example:
1 75 2 4
2 68 6 7
3 58 9 10
4 88 12 15
5 28 18 19
6 27 21 22
7 24 24 25
8 5 28 29
9 69 31 34
10 61 37 38

For example, the first line of the file is 1 75 2 4. It means that value of the node with key 1 is 75, key (line of file) of the left child of this node is 2, and key of right child is 4. Note: not all nodes in the file are part of the tree. Some lines in the file are there just to confuse you. What is the depth of this tree? (Depth is defined as the total number of nodes along the longest path.)

Now, I have some code I think will work. It compiles. It won't run though. Here is the code and below is what comes up in the command window:

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

class Depth
{
static Scanner sc;
static List dump = new ArrayList();
public static void main(String[]args) throws Exception
{
String filename = "src/data.txt";
try
{
sc = new Scanner(new File("tree.input"));
}
catch (FileNotFoundException e)
{
}
while(sc.hasNextLine())
{
int[] row = new int[4];
row[0] = sc.nextInt();
row[1] = sc.nextInt();
row[2] = sc.nextInt();
row[3] = sc.nextInt();
dump.add(row);
}
TreeNode top = new TreeNode(dump.get(0)[1]);
build(top, dump.get(0)[2], dump.get(0)[3]);
System.out.println("The depth of the tree is " + top.depth() + ".");
}
static TreeNode build(TreeNode n, int left_child_key, int right_child_key)
{
TreeNode leftNode = new TreeNode(left_child_key);
TreeNode rightNode = new TreeNode(right_child_key);
if(left_child_key != -1)
{
build(leftNode, getLeftChildKey(left_child_key), getRightChildKey(left_child_key));
n.setLeft(leftNode);
}
if(right_child_key != -1)
{
build(rightNode, getLeftChildKey(right_child_key), getRightChildKey(right_child_key));
n.setRight(rightNode);
}
return n;
}
static int getLeftChildKey(int key)
{
for(int[] row: dump)
{
if(row[0] == key)
return row[2];
}
return -1;
}
static int getRightChildKey(int key)
{
for(int[] row: dump)
{
if(row[0] == key)
return row[3];
}
return -1;
}
}

class TreeNode
{
int value;
TreeNode leftChild, rightChild;
TreeNode(int v)
{
value = v;
}
public void setLeft(TreeNode l)
{
leftChild = l;
}
public void setRight(TreeNode r)
{
rightChild = r;
}
public int depth()
{
int ld = 0, rd = 0;
if (leftChild != null) ld = leftChild.depth();
if (rightChild != null) rd = rightChild.depth();
return Math.max(ld, rd) + 1;
}
}



Exception in thread "main" java.util.NoSuchElementException
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 HW.main(HW.java:26)

Let me know what you guys think is wrong. Thanks!
Four answers:
anonymous
2009-04-29 06:04:50 UTC
Holy cow. I was doing the same problem (different numbers). I just had to take off an empty line in my file. It works now for me.
selvey
2016-10-18 10:56:36 UTC
basically a instruction manual, do no longer forget which you: javac mySpiffyJavaProgram.java yet you: java mySpiffyJavaProgram that's inaccurate to java mySpiffyJavaProgram.classification in case you already be conscious of this, and are executing this way, then that's a classpath concern. in case you desire to double-verify the classpath without doing it as an XP device variable, then you definately might java C:jdk1.6.1_umy_java_programshelloWorl... My suggestion, only for sanity sake, could be to acquire the unfastened NetBeans IDE. NetBeans shops the total course to your JRE and your classification folder internally. NetBeans is an extremely positive editor and could practice you lots of the improve constructive properties of java.
Snezzy
2009-04-29 05:46:23 UTC
You've got an object-oriented language, and you've avoided an object-oriented approach. I would expect to see objects such as tree and node, and methods such as left and right. Granted, I'm doing this from hand-waving, not from detailed analysis of the program you wrote nor of the problem. But could you give an OO approach a try, please? I'm betting the solution would be much shorter and contain fewer bugs.
Blackcompe
2009-04-29 05:55:03 UTC
I hope you get caught cheating. I am not telling you what the problem is.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...