二进制树迭代遍历只能接一个分支
学习如何使用这些东西,例如二进制树。有一个问题...
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论