dStyn
dStyn

Reputation: 28

Binary Search Tree in C# Search Method

public bool Searchtree(Node root, int target)
    {
        if (root == null) return false;

        else if (target < root.data)
            Searchtree(root.left, target);

        else if (target > root.data)
            Searchtree(root.right, target);

        if (target == root.data)
        {
            //Console.WriteLine("Found: "+root.data);
            return true;
        }

        else
            return false;
    }

This is a method to search an integer in a Binary Search Tree

Method call would be: binary.Searchtree(binary.root, 2);

But it Always returns False, even though it prints the Writeline.

Upvotes: 1

Views: 1021

Answers (1)

LeffeBrune
LeffeBrune

Reputation: 3477

return the result of calls to Searchtree:

public bool Searchtree(Node root, int target)
{
    if (root == null)
        return false;
    else if (target < root.data)
        return Searchtree(root.left, target);
    else if (target > root.data)
        return Searchtree(root.right, target);

    return true;
}

Upvotes: 4

Related Questions