C# 二叉树有问题-尝试在C中按级别检查某些内容#

C# 二叉树有问题-尝试在C中按级别检查某些内容#,c#,binary-tree,C#,Binary Tree,我有一个二叉树,当每个叶子由以下部分组成: BinNode<Tuple<int,int>> BinNode 第一项是叶的值,第二项是级别 我正在尝试创建一个函数来获取这个二叉树,并检查每个双级(0,2,4..)的值是否按从左到右的升序排列,以及每个非双级(1,3,5..)的值是否按从左到右的降序排列 我为tree做了一个示例,该函数需要返回true: BinNode类具有以下功能: T GetValue() BinNode GetLeft() BinNode Ge

我有一个二叉树,当每个叶子由以下部分组成:

 BinNode<Tuple<int,int>>
BinNode
第一项是叶的值,第二项是级别

我正在尝试创建一个函数来获取这个二叉树,并检查每个双级(0,2,4..)的值是否按从左到右的升序排列,以及每个非双级(1,3,5..)的值是否按从左到右的降序排列

我为tree做了一个示例,该函数需要返回true:

BinNode类具有以下功能:

T GetValue()

BinNode GetLeft()

BinNode GetRight()

布尔·哈斯勒夫()

布尔·哈斯莱特()

我尝试了一些东西,但没有成功:

public static bool CheckTheTree(BinNode<Tuple<int, int>> t)
        {
        Queue < BinNode <Tuple<int, int>>> queue = new Queue<BinNode<Tuple<int, int>>>();
        queue.Insert(t);

        BinNode<Tuple<int, int>> last = null;
        while (!queue.IsEmpty())
        {
            if (last == null)
            {
                last = queue.Remove();
            }
            else
            {
                BinNode<Tuple<int, int>> current = queue.Remove();

                int current_level = current.GetValue().Item2;
                int last_level = last.GetValue().Item2;

                int current_value = current.GetValue().Item1;
                int last_value = last.GetValue().Item1;

                if (current_level == last_level)
                {
                    if ((current_level % 2 == 0 && current_value < last_value)
                        || (current_level % 2 == 1 && current_value > last_value)){
                        return false;
                    }
                    if (current.HasLeft())
                        queue.Insert(current.GetLeft());
                    if (current.HasRight())
                        queue.Insert(current.GetRight());
                }
                last = current;
            }
        }

        return true;
    }
publicstaticbool检查树(BinNode t)
{
队列队列=新队列();
队列。插入(t);
BinNode last=null;
而(!queue.IsEmpty())
{
if(last==null)
{
last=queue.Remove();
}
其他的
{
BinNode current=queue.Remove();
int current_level=current.GetValue().Item2;
int last_level=last.GetValue().Item2;
int current_value=current.GetValue().Item1;
int last_value=last.GetValue().Item1;
如果(当前水平==最后水平)
{
if((当前\u级别%2==0&&当前\u值<上次\u值)
||(当前\u级别%2==1&¤t\u value>last\u value)){
返回false;
}
if(current.hasleet())
Insert(current.GetLeft());
if(current.HasRight())
Insert(current.GetRight());
}
last=当前值;
}
}
返回true;
}
主要问题是:

    static void Main(string[] args)
    {

        // (8,0)
        BinNode<Tuple<int,int>> b1 = new BinNode<Tuple<int, int>>(new Tuple<int, int>(8, 0));

        // (9,1)
        b1.SetRight(new BinNode<Tuple<int, int>>(new Tuple<int, int>(9, 1)));

        // (10, 1)
        b1.SetLeft(new BinNode<Tuple<int, int>>(new Tuple<int, int>(10, 1)));


        // (-3,2)
        b1.GetLeft().SetLeft(new BinNode<Tuple<int, int>>(new Tuple<int, int>(-3, 2)));


       Console.WriteLine(CheckTheTree(b1) );
        
    }
static void Main(字符串[]args)
{
// (8,0)
BinNode b1=新的BinNode(新元组(8,0));
// (9,1)
b1.SetRight(新的BinNode(新的元组(9,1));
// (10, 1)
b1.SetLeft(新的BinNode(新的元组(10,1));
// (-3,2)
b1.GetLeft().SetLeft(新的BinNode(新的元组(-3,2));
控制台。写入线(选中树(b1));
}

您应该在纸上浏览算法,并查看遍历树的顺序。看起来你是从右向左走,也许应该从左向右走。此外,API提供hasLeft()和hasRight()也是有原因的,因此您可能需要在调用getLeft()或GetRight()之前调用它们。

您需要使用BFS。(广度优先搜索)是一种按顺序或其级别循环树节点的算法。此算法使用队列。在您的问题中,您需要按照节点的顺序查看每个级别。查看当前节点,如果最后一个节点和当前节点具有相同的级别,则检查它们的顺序,具体取决于级别是否为奇数

为了简单起见,我在BinNode对象中使用了值和级别属性。将其更改为binnode.GetValue().Value1表示值,将其更改为binnode.GetValue().Value2表示级别

是这样的:

public static bool CheckTheTree(BinNode<Tuple<int, int>> t){
    Queue<BinNode<Tuple<int, int>> queue = new Queue<BinNode<Tuple<int, int>>();
    queue.Enqueue(t);

    BinNode<Tuple<int, int>> last = null;
    while(queue.Any()){
        if(last = null){
            last = queue.Dequeue();
        }
        else{
            BinNode<Tuple<int, int>> current = queue.Dequeue();
            if(current.Level == last.Level){
                if((current.Level % 2 == 0 && current.Value < last.Value)
                    || (current.Level % 2 == 1 && current.Value > last.Value){
                    return false;
                }

            }

            last = current;
        }

        if(last.HasLeft())
            queue.Enqueue(last.GetLeft());
        if(last.HasRight())
            queue.Enqueue(last.GetRight());
    }

    return true;
}  
publicstaticbool检查树(BinNode t){

你到底有什么问题?你尝试了什么?你的图片和文本不匹配-图片有第一个值,第二个值level@WhileTrueSleep谢谢,我修复了这个问题。@nocodename我不知道如何生成函数。@MattBurland我只负责构建算法,以测试树是否与示例类似。@i不知道这样一个树的目的是什么,为什么不呢?请发布一个错误的输入示例,这样我现在就可以调试itI调试了,错误是:首先,last=null,所以它进入if(if(last==null))在此之后,它不会执行else并返回while,但队列是空的,因此它已完成并始终返回trueI fixed。我总是在last!=nullNow时执行Enquence。我在:if(current.HasRight())中有错误,因为在else中定义了当前值,所以“current”在当前上下文中不存在。在:last.Enqueue中(last.GetRight());因为last不是队列,所以他没有排队函数。is Queue.Enque(last.GetRight())。这是代码中的错误。抱歉