简体   繁体   中英

C# Binary Search Tree Problem

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);
        }
    }
}

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?

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)

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#)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM