Tahani Alqadiry
Tahani Alqadiry

Reputation: 21

inserting in binary tree doesn't work using java

I'm currently learning about trees using java and i have some errors going on here in the insertion of items in a binary tree I don't why it doesn't work

this is the code: tree node:

public class TNode {
    int data;
    TNode left;
    TNode right;

    public TNode(int data) {
        this.data = data;
        left = null;
        right = null;
    }
}

tree class:

public class Tree {
    TNode root;

    public Tree(){
        root = null;
    }

    public TNode insertNode(TNode item, int d) {
        if (item == null) {
            return new TNode(d);
        }

         if (d < item.data) {
            item.left = insertNode(item, d);
        }

        if (d > item.data) {
            item.right = insertNode(item, d);
        } else {
            return item;
        }

        return item;
    }

    public void add(int d) {
        insertNode(root, d);
    }
}

Whenever I add an item the root remains null with no right or left items if someone can help I'll be really thankful

Upvotes: 1

Views: 229

Answers (3)

Srikanth Dasari
Srikanth Dasari

Reputation: 1

This Java code creates a binary tree and inserts nodes with char values. The BinaryTree class has methods to insert nodes and get the current and last inserted nodes. The Node class represents a single node in the tree, with a char value and references to its left and right children.

package binarytreecreation;

class Node {

    char value;
    Node leftChild;
    Node rightChild;

    public Node(char value) {
        this.value = value;
    }

}

class BinaryTree {

    private Node root;
    private Node current;

    public void insert(char value) {
        var node = new Node(value);

        if (root == null) {
            root = node;
            System.out.println(value + " Inserted");
            return;
        }

        current = root;
        while (true) {
            if (current.leftChild == null) {
                current.leftChild = node;
                break;
            }
            if (current.rightChild == null) {
                current.rightChild = node;
                break;
            }

            current = current.leftChild;

        }

        System.out.println(value + " Inserted");

    }

    public char getCurrent() {
        return current.value;

    }

    public char getLastInserted() {
        return current.leftChild.value;

    }
}

public class BinaryTreeCreation {

    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        bt.insert('D');
        bt.insert('A');
        bt.insert('E');
        bt.insert('B');
        bt.insert('C');
        bt.insert('F');
        bt.insert('G');
        System.out.println("current node = " + bt.getCurrent());
        System.out.println("last inserted node =" + bt.getLastInserted());
    }

}

Algorithm goes like this

  1. The Node class has a char value and references to its left and right children.
  2. The BinaryTree class has a private Node root and current references.
  3. The insert method creates a new Node with the given char value and inserts it into the tree. If the tree is empty, it sets the root to the new node. Otherwise, it traverses the tree to find the appropriate position for the new node.
  4. The getCurrent method returns the value of the current node.
  5. The getLastInserted method returns the value of the last inserted node (the left child of the current node).
  6. In the main method, a BinaryTree object bt is created, and nodes with values 'D', 'A', 'E', 'B', 'C', 'F', and 'G' are inserted.
  7. The current node and the last inserted node are printed.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

Fine code, but recursing does not step further

item.left = insertNode(item.left, d);
item.right = insertNode(item.right, d);

And the initial root is not updated:

root = insertNode(root, d);

The else part or final return is superfluous.


Something on the code style

insertNode has a node as input, and returns the updated node value, hence the call "pattern" should look like

X = insertNode(X, d); // X is some expression

This is because java can never assign to the passed argument expression: it has no pass-by-reference, but pass-by-value; f(x) never assigns to x.

Upvotes: 1

Ruslan Akhundov
Ruslan Akhundov

Reputation: 2216

root is always null because you never assign value to it.

you can add to the beginning of your method check and assign it

public TNode insertNode(TNode item, int d){
    if(item == null){
        TNode node = new TNode(d);
        if (root == null) { 
            root = node;
        }
        return node
    }
    ... rest of method isn't changed...

Also when you are recursing you should make a call with a proper child node instead of always calling with item, so for example first case would be:

    item.left = insertNode(item.left, d);

For second case you would just use item.right instead.

Upvotes: 1

Related Questions