3

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.

2
  • What is the point of writing public int value { get; set; } instead of public int value;? Commented Nov 15, 2013 at 7:22
  • @PaulDraper: ya, no special case. thanks for bringing that up. But, do you know how I should write the function for AddNode ? Commented Nov 15, 2013 at 7:25

2 Answers 2

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@nowhewhomustnotbenamed. How you tested it? clearly, there is no function "AddRoot" here, it should be "AddNode". Isn't it?
@RajeshMishra Fixed.
1

This statement will only be true the first time you run your code.

if (this.root == null)
{
    this.root = new Node(valueToBeInserted);
}

Nowhere does the this.root get set to null again... Normally you would code the add like this:

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.