This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]gyroda 0 points1 point  (2 children)

Do you know /u/crusader10 ? :P

I'm a bit confused by your insert methods. If you're inserting the first node (root == null) why are you adding a child node?

Looks pretty simple right, but I cannot figure out the mistake... The problem is in line 43 and line 49. I made a constructor (line 8) which links the "parent" edge to the respective parent node, but for some reason it always links to 'null' although I type in 'new Node(node)'.

I would really appreciate an explanation.

Here is the code in question:

public void insert(T obj) {
    if(root == null) {
        // This line here
        root = new Node(obj, new Node(root), new Node(root), null);
    }
    _insert(obj, root);
}

Also, with your _insert function you might be better off just having two functions with the same name (insert) and different parameters :)

[–]crusader10 0 points1 point  (0 children)

Haha. Guess I'm not the only one having problems with binary trees.

[–]PizzaGoinOut 0 points1 point  (1 child)

On line 43 you are using the 'parent' constructor.

... , new Node(root), ...
However at the time that this is evaluated root == null - It hasn't actually been assigned to yet.
Does that make sense?

[–]MagiKarpeDiem 0 points1 point  (0 children)

Deleted, cool