2015-10-06 96 views
0

我想弄清楚如何打印第一个孩子下一个兄弟姐妹树。我想是这样的:如何打印第一个孩子 - 兄弟姐妹结构

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; } 
} 

现在有没有人如何打印出来?

在此先感谢!

+0

只是一个小例子如何打印一棵树 – theMaster

+0

我在上面描述了它如何打印出来 – theMaster

+0

我的不好,我没有仔细阅读,对不起(删除我的评论)。 – Amessihel

回答

0

我选用合并TreeNodeGeneralTree这样:

public class TreeNode<T> 
{ 

    public T data; 
    public List<TreeNode<T>> childs; 

    public TreeNode<T> firstChild() 
    {return childs.get(0);} 

    public void appendChild(TreeNode<T> child) 
    {childs.add(child);} 

    public void print() {/* ... */} 

    /* ... */ 

    public static void main(String args[]) 
    { /* ... */} 
} 

然后,方法写print()递归:

public void print() 
    { 
     print(0);  
    } 

    public void print(int offset) 
    { 
     if (node == null) return; // nothing to print anymore 

     System.out.println(this.data); // printing the root data 

     TreeNode<T> lastChild=null; 
     String output = ""; 
     for(Iterator<TreeNode<T>> i = childs.iterator(); i.hasNext();) 
     { 
      lastChild = i.next(); 
      if (output != "") output += " - "; 
      output += lastChild.data; 
     } 

     // length will be the next line offset 
     // (size of the current line output minus last item length 

     int length = output.length()-lastChild.toString().length; 
     // use a repeat() string function like this one : 
     output = org.apache.commons.lang.StringUtils.repeat(" ", length) + (length>0?"|":"") + output; 
     System.out.println (output); 
     lastChild.print(length); 
    } 

} 

不幸的是我无法验证我的代码现在,如果你有问题,请让我知道。

+0

为了记录,我使用C#编码。我认为我的代码在结构上是正确的,但我想知道的下一件事是如何像第一个孩子下一个兄弟树一样打印它。如果我打印tree.root.FirstChild.NextSibling.Data我得到正确的兄弟姐妹,所以我认为代码不是问题,只有如何打印出来的方式 – theMaster

+0

是的,你只需要检查打印循环。我的理解是,你想打印所有的兄弟姐妹,然后**最后兄弟姐妹的孩子。对?如果你理解过程'(next offset = length(current_line) - length(lastSibling))',你应该可以编写你自己的函数。 – Amessihel

+0

但在你的情况下,每个节点可能有2个以上的节点,因为你使用一个列表来存储它的所有孩子。这不是如何构建二叉树。 – theMaster