2

I have programmed a binary search tree with method Add(). But it is not working. When I add number to the tree, root is still empty. Why?

EDIT: Code on pastebin, here I am havig some problems with displaying http://pastebin.com/jvP0WwhR

using System;

namespace bst
{
    public class Node
    {
        public int value;
        public Node Right = null;
        public Node Left = null;

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



    public class BST
    {
        public Node Root = null;

        public BST()
        {
        }

        public void Add(int new_value)
        {
            if(Search(new_value))
            {
                Console.WriteLine("Zadaná value (" + new_value + ") se ve stromu už nachází");
            }
            else
            {
                AddNode(this.Root,new_value);
            }

        }

        public void AddNode(Node Actual, int new_value)
        {
                if(Actual == null)
                {
                    Actual = new Node(new_value);
                }
                else if(new_value < Actual.value)
                {
                    AddNode(Actual.Left,new_value);
                }
                else if(new_value > Actual.value)
                {
                    AddNode(Actual.Right,new_value);
                }

        }


        public bool Search(int hledane)
        {
            Node Actual = this.Root;

            while(Actual != null)
            {
                if(hledane < Actual.value)
                {
                    Actual = Actual.Left;
                }
                else if(hledane > Actual.value)
                {
                    Actual = Actual.Right;
                }
                else
                {
                    return true;
                }
            }
            return false;

        }

        public void Display()
        {
            DisplayUndertree(this.Root,0);
        }

        public void DisplayUndertree(Node EnterNode, int deep)
        {
            if(EnterNode != null)
            {
                for(int i=1; i<=deep; i++)
                {
                    Console.Write("\t");
                }
                Console.WriteLine(EnterNode.value);
            }

            if(EnterNode.Left != null)
            {
                DisplayUndertree(EnterNode.Left,deep+1);
            }

            if(EnterNode.Right != null)
            {
                DisplayUndertree(EnterNode.Right,deep+1);
            }
        }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            BST strom = new BST();
            Console.WriteLine(strom.Search(5));
            strom.Add(5);
            Console.WriteLine(strom.Search(5));
            Console.WriteLine(strom.Root.value);


            //strom.Display();

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
2
  • I got it fixed for you, welcome to www.stackoverflow.com Commented Jan 4, 2011 at 18:08
  • Thank you Richard, but what was the reason it was badly formatted? I just pasted it into the code tag. Commented Jan 4, 2011 at 18:24

3 Answers 3

3

Because the method:

public void PridejNode(Node Actual, int new_value)

might change the value of Actual, this parameter needs to be passed by reference:

public void PridejNode(ref Node Actual, int new_value)

EDIT Since I posed my answer, the method names changed from Pridej to Add.... same basic idea. Is that Czech?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it is, I was quickly renaming them, but i forgot to finish that. Sorry.
2

Because Root is never assigned.

EDIT: I'll give credit to the other answer by abelenky. You need to pass by Node by reference.

public void PridejNode(ref Node Actual, int new_value)

Comments

0

You're passing "this.Root" by "value" not by "reference". Inside Pridejnode where you do "Actual = new Node(new_value)" you're effectively changing a local variable called "Actual" that originally had the value passed in to the function. This doesn't pass the value back out of the function so it's only a temporary local change that gets lost.

I'd read up on passing by reference - Passing Parameters (C#)

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.