1

I have a node class and am just trying to narrow down an issue when adding elements to my tree. I think my node class is just not properly creating the node to add in the tree. When I print each node, it is returning memory addresses, I believe. I cannot seem to get it to print anything else. Any help in determining if my node class is faulty would be great.

Example of what my program prints:

([C@f72003d)
([C@56dacb7)
([C@63662529)
([C@4711d9ba)

The TreeNode class:

public class TreeNode {

char[] data;
TreeNode left, right, parent;
// used for printing:
int column;
int row;
int center;


public TreeNode(char[] data) {
    this.data = data;
    left = right = parent = null;
}

@Override
public String toString() {
    return "(" + data + ")";
}
public int return_letter(){
    char x = data[0];
    int b = Character.getNumericValue(x);
    return b;        
}

public int findMaxDepth() {
    int lmax = 0;
    int rmax = 0;
    if (left != null) {
        lmax = left.findMaxDepth();
    }
    if (right != null) {
        rmax = right.findMaxDepth();
    }
    return 1 + Math.max(lmax, rmax);
}

}
4
  • 1
    You haven't really described your issue... please provide us with more information. A short but complete program demonstrating the problem would be helpful. Commented Mar 29, 2014 at 19:34
  • could you please post whatever error message you are getting? Commented Mar 29, 2014 at 19:34
  • 3
    @Override public String toString() {return "(" + data + ")";} will not print what you want. Use Arrays.toString(data) to get a String representation of the content of your array. Commented Mar 29, 2014 at 19:35
  • Thanks ZouZou, that was it! Commented Mar 29, 2014 at 19:48

1 Answer 1

1
public String toString() {
    return "(" + data + ")";
}

You're problem is here. toString() is returning the memory references of data and its contained elements. You can either use Arrays.toString(data) inside this method to get a string representation of the items in the array, or in your method that is printing out the data, do something like this:

for(int i = 0; i < data.length; i++){
    println(data[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.