为什么我的包含方法中的if语句无法访问?

时间:2018-03-21 17:51:51

标签: c# algorithm binary-search-tree

当我运行我的程序时,我的'if'语句被编译为无法访问的代码,这反过来导致我的contains方法连续打印false,即使该数字存在于树中。我无法弄清楚为什么有人可以帮助我?我对编程很新。

这是我的节点类。

class Node<T> where T : IComparable
{

    private T data;
    private int balanceFactor = 0; //added for AVLTree
    public Node<T> Left, Right;

    public Node(T item)
    {
        data = item;
        Left = null;
        Right = null;
    }

    public T Data
    {
        set { data = value; }
        get { return data; }
    }
}

这是我的二叉搜索树类。

class BSTree<T> : BinTree<T> where T : IComparable
{  //root declared as protected in Parent Class – Binary Tree


    public BSTree()
    {
        root = null;
    }

    public void InsertItem(T item)
    {
        insertItem(item, ref root);
    }

    private void insertItem(T item, ref Node<T> tree)
    {
        if (tree == null)
            tree = new Node<T>(item);

        else if (item.CompareTo(tree.Data) < 0)
            insertItem(item, ref tree.Left);

        else if (item.CompareTo(tree.Data) > 0)
            insertItem(item, ref tree.Right);
    }


    public int Height()
    //Return the max level of the tree
    {
        return Height(root);
    }

    protected int Height(Node<T> current)
    {
        if (current == null) return 0;
        return 1 + Math.Max(Height(current.Left), Height(current.Right));
    }

    public int Count()
    //Return the number of nodes in the tree
    {
        return Height(root);
    }

    public int Count(ref Node<T> current)
    //Return the number of nodes in the tree
    {
        int counter = 0;

        if (current == null)
        {
            return 0;
        }
        else if (current.Left != null)
        {
            counter += Count(ref current.Left);
            counter++;
        }

        if (current.Right != null)
        {
            counter += Count(ref current.Right);
            counter++;
        }
        return counter;
    }

    public Boolean Contains(T item)
    //Return true if the item is contained in the BSTree, false       //otherwise.
    {
        Node<T> current = root;

        if (current.Data.CompareTo(item) == 0)
        {
            {
                return true;
            }
            if (current.Data.CompareTo(item) > 0)
            {
                current = current.Left;
            }
            if (current.Data.CompareTo(item) < 0)
            {
                current = current.Right;
            }
        }
        return false;
    }

    private T leastItem(Node<T> tree)
    {
        if (tree.Left == null)
            return tree.Data;
        else
            return leastItem(tree.Left);
    }

}

最后是我的主要课程。

class Program
{
    static void Main(string[] args)
    {

       BSTree<int> treeBranch = new BSTree<int>();

        treeBranch.InsertItem(77);
        treeBranch.InsertItem(20);
        treeBranch.InsertItem(37);
        treeBranch.InsertItem(15);
        treeBranch.InsertItem(22);
        treeBranch.InsertItem(30);

        Console.WriteLine(treeBranch.Count());
        Console.WriteLine(treeBranch.Height());
        Console.WriteLine(treeBranch.Contains(15));

        string InOrder = "in order :";
        treeBranch.InOrder(ref InOrder);
        Console.WriteLine(InOrder);
        Console.WriteLine("\n");
        Console.ReadKey();

    }
}

2 个答案:

答案 0 :(得分:5)

if (current.Data.CompareTo(item) == 0)
{
    { <==== here
        return true;
    }
    if (current.Data.CompareTo(item) > 0)
    {
        current = current.Left;
    }
    if (current.Data.CompareTo(item) < 0)
    {
        current = current.Right;
    }
}

return true;没有条件。将您的代码更改为:

if (current.Data.CompareTo(item) == 0)
{
    return true;
}
else if (current.Data.CompareTo(item) > 0)
{
    current = current.Left;
}
else if (current.Data.CompareTo(item) < 0)
{
    current = current.Right;
}

答案 1 :(得分:1)

此代码中的许多{}

if (current.Data.CompareTo(item) == 0)
{
    {
        return true;
    }