简体   繁体   中英

Entering values to nodes in a binary tree

Although the problem may seem like not a problem at all to others, I am stuck to it, and I cannot solve it. There doesn't seem to be something alike on the Internet. I need to add values to the nodes in a regular binary tree. I am entering the number of nodes in the tree, then I enter the ordinal number of the root (zero) and the value in the root, and finally, I enter the ordinal number of the node (child), the value of the child, the information if it's left or right child, and the ordinal number of the parent. In the end, the binary tree is printed. There are two classes, one for the nodes, and one for the tree (the data member is root). The code compiles, and when I enter the number of nodes, the program throws an exception - java.lang.NumberFormatException: For input string: "" . And I am stuck here. Here is the code, and thanks for the help in advanced.

import java.util.Scanner;

public class BinaryTreeGeneral {
    public static void main( String[] args ) {
        Scanner in = new Scanner( System.in );

        int N = in.nextInt();
        BNode<String> nodes[] = new BNode[N];
        BTree<String> tree = new BTree<>();

        String[] input = in.nextLine().split(" ");
        nodes[Integer.parseInt(input[0])] = new BNode<String>(input[1]);
        tree.root = nodes[Integer.parseInt(input[0])];

        for (int i = 1; i < N; i++) {
            input = in.nextLine().split(" ");
            nodes[Integer.parseInt(input[0])] = new BNode<String>(input[1]);
            if (input[2].equals("LEFT")) 
                tree.addNode(nodes[Integer.parseInt(input[3])], BNode.LEFT, nodes[Integer.parseInt(input[0])]);
            else
                tree.addNode(nodes[Integer.parseInt(input[3])], BNode.RIGHT, nodes[Integer.parseInt(input[0])]);
        }

        tree.printTreeInorder();
    }
}

/*Sample Input:
  10
  0 10
  1 19 LEFT 0
  2 8 LEFT 1
  3 7 LEFT 2
  4 60 RIGHT 2
  5 5 RIGHT 1
  6 4 RIGHT 0
  7 13 RIGHT 6
  8 2 LEFT 7
  9 1 RIGHT 7*/

Also, I want to ask if this is a standard way of adding values to the nodes of a tree, or is there a more practical, favored way of doing this in real situations, not textbook examples?

The problem is that you are mixing nextInt and nextLine without fully understanding what they do.

  • The nextInt method extracts the next token and parses it as an integer.
  • The nextLine reads up to (and including) the next end-of-line marker and returns it with the end-of-line removed.

So ... if you call nextInt and the nextLine for a line that consists of one number, then the nextLine() call will return an empty String. (If you don't understand what I am saying, read the javadocs for the two methods carefully.)

Solutions include:

  1. Add a nextLine() call after the nextInt() to consume the rest of the line.
  2. Stop using nextLine() and split(...) and just use nextInt() and next() calls.
  3. Use nextLine().split(...) for the first line as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM