Jack L.
Jack L.

Reputation: 415

Binary Trees and NullPointerException

This method takes a tree of integers and constructs a String of the following:

(The data at the root, String of left subtree, Stringof right subtree)

For example, if a variable tree stores a reference to the following tree:

          +---+
          | 2 |
          +---+
         /     \
     +---+     +---+
     | 8 |     | 1 |
     +---+     +---+
    /         /     \
+---+     +---+     +---+
| 0 |     | 7 |     | 6 |
+---+     +---+     +---+
         /               \
     +---+               +---+
     | 4 |               | 9 |
     +---+               +---+

It should return a String of:

"(2, (8, 0, empty), (1, (7, 4, empty), (6, empty, 9)))"

The method should return "empty" for an empty tree. For a leaf node, it should return the data in the node as a String. For a branch node, it should return a parenthesized String that has three elements separated by commas:

With my method, it sometimes works, but sometimes, it generates a NullPointerException. Can anyone point out where and why this is happening?

Here is what I have:

 private IntTreeNode overallRoot; // first node; linked to other nodes

 // post: returns the string of all data in leaves. For a branch node, return a parenthesized String
 //       w/ three elementsseparated by commas. return empty for empty tree
 public String toString2() {
     if (overallRoot == null) { // If empty tree
         return "empty";
     } else {
         return toString2(overallRoot);
     }
 }

 // helper for toString
 private String toString2(IntTreeNode root) {
     String value = "";
     if (root.left != null && root.right != null) { // If both branches exist
         value += "(" + root.data + ", " + toString2(root.left)+ ", " + toString2(root.right) + ")";  
     } else if (root.left != null && root.right == null) { // if right branch is empty
         value += "(" + root.data + ", " + toString2(root.left) + ", empty)";
     } else if (root.left == null && root.right != null) { // if left branch is empty
         value += "(" + root.data + ", empty, " + toString2(root.left) + ")";
     } else { // If at a leaf
         return "" + root.data;
     }
     return value;
 }

Here is the node class:

public class IntTreeNode {
     public int data;
     public IntTreeNode left;
     public IntTreeNode right;

     // constructs a leaf node with given data
     public IntTreeNode(int data) {
         this(data, null, null);
     }

     // constructs a branch node with given data, left subtree,
     // right subtree
     public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {
         this.data = data;
         this.left = left;
         this.right = right;
     }
}

Upvotes: 1

Views: 2467

Answers (1)

Pham Trung
Pham Trung

Reputation: 11284

The error is here:

else if (root.left == null && root.right != null) { // if left branch is empty
         value += "(" + root.data + ", empty, " + toString2(root.left) + ")";
}

It should be...

else if (root.left == null && root.right != null) { // if left branch is empty
         value += "(" + root.data + ", empty, " + toString2(root.right) + ")";
}

..., passing root.right rather than root.left to toString2.

Upvotes: 4

Related Questions