【问题标题】:How to print first child - sibling structure如何打印第一个孩子 - 兄弟结构
【发布时间】:2016-01-03 11:05:09
【问题描述】:

我正在尝试弄清楚如何打印第一个孩子的下一个兄弟树。我想要的是以下内容:

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

现在有人可以打印出来吗?

提前致谢!

【问题讨论】:

  • 只是一个如何打印树的小例子
  • 我在上面描述了我想如何打印出来

标签: java recursion binary-tree nodes siblings


【解决方案1】:

我选择以这种方式合并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);
    }

}

很遗憾,我现在无法验证我的代码,如果您有问题,请告诉我。

【讨论】:

  • 为了记录,我使用 C# 进行编码。我假设我的代码是正确的 qua 结构,但我想知道的下一件事是如何像第一个孩子下一个兄弟树一样打印它。如果我打印 tree.root.FirstChild.NextSibling.Data 我得到了正确的兄弟,所以我认为代码不是问题,只是如何打印出来的方式
  • 是的,您只需要检查打印循环。我的理解是你想打印所有兄弟姐妹,然后最后一个兄弟姐妹的孩子。正确的?如果你理解(next offset = length(current_line) - length (lastSibling))的过程,你应该可以编写自己的函数。
  • 但在您的情况下,每个节点可能有超过 2 个节点,因为您使用列表来存储其所有子节点。这不是构建二叉树的方式。
  • 那么为什么要在打印样本中设置三个孩子(第一个 + 两个兄弟姐妹)?无论如何,您可以轻松地将我的List 替换为您想要的(例如Node leftNode right;)。请记住,主要部分是print 算法。 (另外:我用 Java 写是因为你同意这个标签。)
猜你喜欢
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 2016-07-19
  • 2011-12-27
  • 1970-01-01
相关资源
最近更新 更多