Question

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);
}

}
Was it helpful?

Solution

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]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top