1

I have written a code to insert an element in a binary tree in java. Here are the functions to do the same:

 public void insert(int data)
    {
        root = insert(root, data);

    }

    private Node insert(Node node, int data)
     {
         if (node == null)
             node = new Node(data);
         else
         {
             if (node.getRight() == null)
                 node.right = insert(node.right, data);
             else
                 node.left = insert(node.left, data);             
         }
         return node;
     } 

However when I traverse the tree, the answer I get is wrong. Here are the traversal functions (preorder):

public void preorder()
     {
         preorder(root);
     }
     private void preorder(Node r)
     {
         if (r != null)
         {
             System.out.print(r.getData() +" ");
             preorder(r.getLeft());             
             preorder(r.getRight());
         }
     }

Okay so as suggested here's the definition for the Node class:

public class Node {

    public int data;
    public Node left, right;

    /*  Constructor  */
    public Node() {
        left = null;
        right = null;
        data = 0;
    }
    /*  Constructor  */

    public Node(int d, Node l, Node r) {
        data = d;
        left = l;
        right = r;
    }

    //Constructor
    public Node(int d) {
        data = d;
    }

    /*  Function to set link to next Node  */

    public void setLeft(Node l) {
        left = l;
    }
    /*  Function to set link to previous Node  */

    public void setRight(Node r) {
        right = r;
    }
    /*  Function to set data to current Node  */

    public void setData(int d) {
        data = d;
    }
    /*  Function to get link to next node  */

    public Node getLeft() {
        return left;
    }
    /*  Function to get link to previous node  */

    public Node getRight() {
        return right;
    }
    /*  Function to get data from current Node  */

    public int getData() {
        return data;
    }
}

I have re-checked the algorithm for traversal many times, and it's working perfectly. I believe the problem is in the insertion algorithm. Any suggestions?

2 Answers2

2

If I understood correctly, you want to fill your binary tree in "layers". E.g. you want to put something into depth 4 only if depth 3 is "full binary tree".

Then the problem is whole logic of your insert algorithm that is DFS-based. In other words it inserts elements deeper and deeper on the one side instead of building full binary tree on both sides.

If you look closer to your insert algorithm you will see that once you skip "right" subtree, you will never return to it - even if the "left" subtree is already full binary tree. That leads to the tree that will be growing deeper and deeper on the left side but not growing on the right side.

Speaking in programming language. You do:

(node.right != null) && (node.left != null) => insert (node.left)

but you can't do this (start inserting node.left). What if node.left has both children and node.right has no children? You will attempt to insert to the left even you should do it in node.right.

So what you really need to do insertion BFS-based. That means you will traverse the tree for insertion "in layers". Queue should be your new friend here:-) (not the stack/recursion):

public void insert(int data) {
     if (root == null) {
         root = new Node(data);
         return;
     }

     Queue<Node> nodesToProcess = new LinkedList<>();
     nodesToProcess.add(root);

     while (true) {
         Node actualNode = nodesToProcess.poll();

         // Left child has precedence over right one
         if (actualNode.left == null) {
             actualNode.left = new Node(data);
             return;
         }
         if (actualNode.right == null) {
             actualNode.right = new Node(data);
             return;
         }

         // I have both children set, I will process them later if needed
         nodesToProcess.add(actualNode.left);
         nodesToProcess.add(actualNode.right);
     }
}
zdenda.online
  • 2,451
  • 3
  • 23
  • 45
1

Your method returns given node, but your method has to return inserted node which is node.right or node.left

PeerNet
  • 855
  • 1
  • 16
  • 31