我想弄清楚如何打印第一个孩子下一个兄弟姐妹树。我想是这样的:如何打印第一个孩子 - 兄弟姐妹结构
root
|
firstChild - sibling - sibling
|
child - sibling - sibling
我有下面的代码添加孩子的兄弟姐妹:
class Program
{
static void Main(string[] args)
{
GeneralTree<string> tree = new GeneralTree<string>();
tree.root = new TreeNode<string>
{
Data = "Root"
};
TreeNode<string> child = tree.addChild(tree.root, "Child");
tree.addSibling(child, "Sibling");
tree.print(tree.root);
}
}
class GeneralTree<T>
{
public TreeNode<T> root;
public TreeNode<T> addChild(TreeNode<T> parent, T data)
{
parent.FirstChild = new TreeNode<T>
{
Data = data,
NextSibling = parent.FirstChild
};
return parent.FirstChild;
}
public TreeNode<T> addSibling(TreeNode<T> sibling, T data)
{
sibling.NextSibling = new TreeNode<T>
{
Data = data,
FirstChild = sibling.NextSibling
};
return sibling.NextSibling;
}
int count = 0;
public void print(TreeNode<T> Node)
{
if(Node !=null)
{
Console.WriteLine(Node.Data);
print(Node.FirstChild);
++count;
Console.WriteLine(count);
print(Node.NextSibling);
}
}
}
class TreeNode<T>
{
public T Data { get; set; }
public TreeNode<T> FirstChild { get; set; }
public TreeNode<T> NextSibling { get; set; }
}
现在有没有人如何打印出来?
在此先感谢!
只是一个小例子如何打印一棵树 – theMaster
我在上面描述了它如何打印出来 – theMaster
我的不好,我没有仔细阅读,对不起(删除我的评论)。 – Amessihel