简体   繁体   中英

Finding node value in Binary Search Tree C#

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Node
    {
        public int Value { get; set; }

        public Node Left { get; set; }

        public Node Right { get; set; }

        public Node(int value, Node left, Node right)
        {
            Value = value;
            Left = left;
            Right = right;
        }
    }

    public class BinarySearchTree
    {
        public static bool Contains(Node root, int value)
        {
            bool status = false;
            if (root == null)
            {
                status = false;
            }
            else if (root.Value == value)
            {
                status = true;
                return true;
            }
            else if (root.Value < value)
            {
                Contains(root.Right, value);
            }
            else
            {
                Contains(root.Left, value);
            }
            return status;

        }

        public static void Main(string[] args)
        {
            Node n1 = new Node(1, null, null);
            Node n3 = new Node(3, null, null);
            Node n2 = new Node(2, n1, n3);

            Console.WriteLine(Contains(n2, 3));
            Console.ReadLine();
        }
    }
}

Question: I have tried debugging this code too! But it keeps displaying false as answer. Let me know where I am going wrong. When I debug the code, status changes to true but it still executes other stuff and finally changes to false. Let me know where I am going

In your recursive call, you calculate Contains for child nodes, but you do nothing with it. You should assign result value to status :

public static bool Contains(Node root, int value)
{
    bool status = false;
    if (root == null)
    {
        status = false;
    }
    else if (root.Value == value)
    {
        status = true;
        return true;
    }
    else if (root.Value < value)
    {
        status = Contains(root.Right, value);
    }
    else
    {
        status = Contains(root.Left, value);
    }
    return status;
}

Better is to avoid this status variable and directly use return statement:

public static bool Contains(Node root, int value)
{
    if (root == null)
    {
        return false;
    }
    else if (root.Value == value)
    {
        return true;
    }
    else if (root.Value < value)
    {
        return  Contains(root.Right, value);
    }
    else
    {
        return Contains(root.Left, value);
    }
}

It appears you are missing the status assignment from the Contains calls:

status = Contains(root.Right, value);

and

status = Contains(root.Left, value);

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