二进制树迭代遍历只能接一个分支

发布于 2025-02-12 12:32:24 字数 2887 浏览 1 评论 0原文

学习如何使用这些东西,例如二进制树。有一个问题...

    public class ProgramTest
    {

        public class TestBinaryTree : Program.BinaryTree
        {
            public TestBinaryTree(int value) : base(value)
            {
            }

            public TestBinaryTree Insert(List<int> values)
            {
                return Insert(values, 0);
            }

            public TestBinaryTree Insert(List<int> values, int i)
            {
                if (i >= values.Count) return null;

                List<TestBinaryTree> queue = new List<TestBinaryTree>();
                queue.Add(this);
                while (queue.Count > 0)
                {
                    TestBinaryTree current = queue[0];
                    queue.RemoveAt(0);
                    if (current.left == null)
                    {
                        current.left = new TestBinaryTree(values[i]);
                        break;
                    }
                    queue.Add((TestBinaryTree)current.left);
                    if (current.right == null)
                    {
                        current.right = new TestBinaryTree(values[i]);
                        break;
                    }
                    queue.Add((TestBinaryTree)current.right);
                }
                Insert(values, i + 1);
                return this;
            }
        }
    }
    public class Program
    {
        public class BinaryTree
        {
            public int value;
            public BinaryTree left;
            public BinaryTree right;

            public BinaryTree(int value)
            {
                this.value = value;
                this.left = null;
                this.right = null;
            }
        }

        public static List<int> BranchSums(BinaryTree root)
        {

            Stack<BinaryTree> myStack = new Stack<BinaryTree>();
            myStack.Push(root);
            while(myStack.Count > 0)
            {
                var current = myStack.Pop();

                Console.WriteLine(current.value);
                if(current.right != null) myStack.Push(current.left);
                if (current.left != null) myStack.Push(current.left);
            }
            return new List<int>();
        }

        static void Main(string[] args)
        {
            TestBinaryTree tree = new TestBinaryTree(1).Insert(new List<int>(){2, 3, 4, 5, 6, 7, 8, 9, 10});
            BranchSums(tree);
        }
    }

重现它,只需运行main()即可。 内部的二进制树看起来像:

           1
        /     \
       2       3
     /   \    /  \
    4     5  6    7
  /   \  /
 8    9 10
1
2
4
8
8
4
8
8
2
4
8
8
4
8
8

它似乎仅在首先打印出第一个左分支:1 2 4 8由于某种原因,它不会进一步进一步:( 就我而言,它应该穿过所有分支,而不是一个

learning how to use those things like binary trees. Have one issue ...

    public class ProgramTest
    {

        public class TestBinaryTree : Program.BinaryTree
        {
            public TestBinaryTree(int value) : base(value)
            {
            }

            public TestBinaryTree Insert(List<int> values)
            {
                return Insert(values, 0);
            }

            public TestBinaryTree Insert(List<int> values, int i)
            {
                if (i >= values.Count) return null;

                List<TestBinaryTree> queue = new List<TestBinaryTree>();
                queue.Add(this);
                while (queue.Count > 0)
                {
                    TestBinaryTree current = queue[0];
                    queue.RemoveAt(0);
                    if (current.left == null)
                    {
                        current.left = new TestBinaryTree(values[i]);
                        break;
                    }
                    queue.Add((TestBinaryTree)current.left);
                    if (current.right == null)
                    {
                        current.right = new TestBinaryTree(values[i]);
                        break;
                    }
                    queue.Add((TestBinaryTree)current.right);
                }
                Insert(values, i + 1);
                return this;
            }
        }
    }
    public class Program
    {
        public class BinaryTree
        {
            public int value;
            public BinaryTree left;
            public BinaryTree right;

            public BinaryTree(int value)
            {
                this.value = value;
                this.left = null;
                this.right = null;
            }
        }

        public static List<int> BranchSums(BinaryTree root)
        {

            Stack<BinaryTree> myStack = new Stack<BinaryTree>();
            myStack.Push(root);
            while(myStack.Count > 0)
            {
                var current = myStack.Pop();

                Console.WriteLine(current.value);
                if(current.right != null) myStack.Push(current.left);
                if (current.left != null) myStack.Push(current.left);
            }
            return new List<int>();
        }

        static void Main(string[] args)
        {
            TestBinaryTree tree = new TestBinaryTree(1).Insert(new List<int>(){2, 3, 4, 5, 6, 7, 8, 9, 10});
            BranchSums(tree);
        }
    }

To reproduce it, just run Main().
Binary tree inside looks like:

           1
        /     \
       2       3
     /   \    /  \
    4     5  6    7
  /   \  /
 8    9 10
1
2
4
8
8
4
8
8
2
4
8
8
4
8
8

And it seems, that it only prints out only first left branch: 1 2 4 8 For some reason it is not going further :(
In my case, it should go through all the branches, not the just one

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文