0

This method suppose to add Node to a Binary Tree.

I dont understand why it doesnt do it and where I have mistake.

root is stay on null anytime.

 public void add(int num)
        {
            add(num, root);
        }

        private void add(int num, Node t)
        {

            if (t == null)
                t = new Node(num);

            else if (t.getLeftSon() == null)
                t.setLeftSon(new Node (num));



 }
zcbd
  • 85
  • 10
  • 2
    How about you take a debugger and debug it? Execute it statement by statement and compare the state with your expectations. – zerkms Mar 15 '15 at 21:12
  • This question is tagged for JavaScript but your code is Java. – lwalden Mar 15 '15 at 21:14
  • What precisely is the problem? Do you know _where_ it doesn't work right? How does your output differ with expected output? – James Mar 15 '15 at 21:19
  • hey, I am trying to run debugger.. I noticed that the root always stay "null" after every add action. (when it finished the root return to null) – zcbd Mar 15 '15 at 21:22

2 Answers2

1

The problem comes from:

private void add(int num, Node t)
{
    if (t == null)
        t = new Node(num);
    //...
 }

Assuming this is a method of BTree class and you initialise root to null, when you call:

add(1, root);

root won't contain the new created node when the method exits. You can do:

public void add(int num)
{
    if (root == null) {
        root = new Node(num);
    }
    else {
        add(num, root);
    }
}

and remove the if (t == null) in the add(int num, Node t) method.

  • @zcbd Question has been answered, accept the answer. If you have a new question, make a new one. – m0skit0 Mar 15 '15 at 22:34
1

The problem is that the root node is passed to the add method by value and not by reference. Java passes objects by value and not by reference. Check out the link below for further explantion Is Java "pass-by-reference" or "pass-by-value"?.

Modify the add methods of your code as follows

public void add(int num) {
    root = add(num, root);
}

private Node add(int num, Node t) {
    if (t == null)
        return t = new Node(num);
    else if (t.getLeftSon() == null)
        t.setLeftSon(new Node(num));
    else if (t.getRightSon() == null)
        t.setRightSon(new Node(num));
    else if (Math.abs(height(t.getLeftSon()) - height(t.getRightSon())) <= 1)
        t.setLeftSon(add(num, t.getLeftSon()));
    else
         t.setRightSon(add(num, t.getRightSon()));
    return t;
}
Community
  • 1
  • 1
Anwuna
  • 1,077
  • 11
  • 18
  • Thanks a lot. I have another question please.. I Need to write a method that: Method receive a certain amount and returns a string that represents the nodes track the amount of which is the sum. For example, for the tree at the top, if the system will receive the sum of 16, it will return the string "9,2,5" how can I do that please ? – zcbd Mar 15 '15 at 22:21
  • @zcbd You should actually ask that as another question to get sample code. But a relatively simple approach (albeit not the best) would be to traverse the tree, then store all the numbers in a linear list (like an array list), then use a double for loop to sum each of the values in the array list, storing intermediate values that make up the sum. – Anwuna Mar 15 '15 at 23:25