Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印第一个子同级结构_Java_Recursion_Binary Tree_Nodes_Siblings - Fatal编程技术网

Java 如何打印第一个子同级结构

Java 如何打印第一个子同级结构,java,recursion,binary-tree,nodes,siblings,Java,Recursion,Binary Tree,Nodes,Siblings,我正试图找出如何打印第一个孩子的下一个兄弟姐妹树。我想要的是: root | firstChild - sibling - sibling | child - sibling - sibling 我有以下代码来添加孩子和兄弟姐妹: class Program { static void Main(string[] args) { GeneralTree<strin

我正试图找出如何打印第一个孩子的下一个兄弟姐妹树。我想要的是:

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; }
}
类程序
{
静态void Main(字符串[]参数)
{
GeneralTree=新的GeneralTree();
tree.root=新树节点
{
Data=“Root”
};
TreeNode child=tree.addChild(tree.root,“child”);
树。添加兄弟姐妹(子,“兄弟姐妹”);
tree.print(tree.root);
}
}
类GeneralTree
{
树根;
公共树节点addChild(树节点父节点,T数据)
{
parent.FirstChild=新树节点
{
数据=数据,
NextSibling=parent.FirstChild
};
返回parent.FirstChild;
}
公共树节点添加同级(树节点同级,T数据)
{
sibling.NextSibling=新树节点
{
数据=数据,
FirstChild=sibling.NextSibling
};
返回sibling.NextSibling;
}
整数计数=0;
公共作废打印(TreeNode节点)
{
如果(节点!=null)
{
Console.WriteLine(Node.Data);
打印(Node.FirstChild);
++计数;
控制台写入线(计数);
打印(Node.NextSibling);
}
}
}
三烯类
{
公共T数据{get;set;}
公共树节点FirstChild{get;set;}
公共树节点NextSibling{get;set;}
}
现在有人知道怎么打印出来了吗


提前谢谢

我选择以这种方式合并
TreeNode
GeneralTree

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[])
    { /* ... */}
}

不幸的是,我现在无法验证我的代码,如果您有问题,请告诉我。

上面介绍了如何打印树的一个小示例,我想打印出记录,我使用C来编码。我假设我的代码是正确的结构,但接下来我想知道的是如何像第一个孩子下一个兄弟树一样打印它。如果我打印tree.root.FirstChild.NextSibling.Data,我就得到了正确的同级,所以我认为代码不是问题所在,只是打印的方式是的,你只需要检查打印循环。我的理解是,您希望打印所有兄弟姐妹,然后打印最后一个兄弟姐妹的孩子。正当如果您了解流程
(next offset=length(current_line)-length(lastssibling))
,您应该能够编写自己的函数。但在您的情况下,每个节点可能有两个以上的节点,因为您使用一个列表来存储其所有子节点。这不是二叉树的构造方式。那么,为什么在打印样本中设置了三个孩子(前两个兄弟姐妹)?无论如何,您可以轻松地将我的
列表
替换为您想要的内容(
左节点
右节点;
)。请记住,主要部分是
print
算法。(另外:我用Java编写,因为您同意标记。)
    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);
    }

}