Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive function implementation for Binary search tree

I am trying to implement a basic Binary search tree.

I was able to create a Node and I have problems with the AddNode() function. It is supposed to add a new node to the existing tree but it replaces it.

Any ideas what should be done in the AddNode() function ?

class Node
{
    public int value { get; set; }

    public Node left { get; set; }

    public Node right { get; set; }

    public Node(int i)
    {
        this.value = i;
        this.left = null;
        this.right = null;
    }

}

class Tree
{
    public Node root { get; set; }

    public Tree(Node n)
    {
        root = n;
    }

    public void AddNode(int valueToBeInserted)
    {
        if (this.root == null)
        {
            this.root = new Node(valueToBeInserted); 
            // problem here : existing tree is destroyed. 
            // a new one is created. 
            // instead it should add a new node to the end of the tree if its null
        }

        if (valueToBeInserted < this.root.value)
        {
            this.root = this.root.left;
            this.AddNode(valueToBeInserted);
        }

        if (valueToBeInserted > this.root.value)
        {
            this.root = this.root.right;
            this.AddNode(valueToBeInserted);
        }
    }

    public void printTree()
    {
        // print recursively the values here.
    }
}

class TreeTest
{
    public void Test()
    {
        var tree = new Tree(new Node(100));
        tree.AddNode(20);
        tree.AddNode(100);
    }
}

Thanks.

like image 337
now he who must not be named. Avatar asked Aug 16 '26 11:08

now he who must not be named.


1 Answers

These lines replace the root:

this.root = this.root.left;
this.root = this.root.right;

You should instead pass a parameter to your recursive function.

You can also remove the this quantifiers - they're only necessary when you have a local variable with the same name or perhaps in some other corner cases.

Adding a helper function is useful / necessary since the root must be catered for separately.

Updated code:

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

private void AddNode(int valueToBeInserted, Node current)
{
    if (valueToBeInserted < current.value)
    {
        if (current.left == null)
            current.left = new Node(valueToBeInserted);
        else
            AddNode(valueToBeInserted, current.left);
    }

    if (valueToBeInserted > current.value)
    {
        if (current.right == null)
            current.right = new Node(valueToBeInserted);
        else
            AddNode(valueToBeInserted, current.right);
    }
}
like image 50
Bernhard Barker Avatar answered Aug 18 '26 01:08

Bernhard Barker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!