简体   繁体   English

生成二叉树形式N aire树 - 递归方法不起作用

[英]Generate Binary Tree form N aire Tree- recursive method doesn't work

Im trying to convert a general tree (tree that have one or more children ) to a binary tree .My general tree is represented by an XML file name "1.xml" that contain : 我试图将一般树(具有一个或多个孩子的树)转换为二叉树。我的一般树由XML文件名“1.xml”表示,其中包含:

<A>
    <B/>
    <C>
        <E/>
        <F/>
    </C>
    <D/>
 </A>

so i can represent the binary tree like this : 所以我可以像这样表示二叉树:

在此输入图像描述

Now to convert this tree to an a binary tree i use the following method: 现在要将此树转换为二叉树,我使用以下方法:

 A ---- # -------- # ------ # 
        |          |        |
        B    C--#--#        D 
                |  |
                E  F

(the number of # (DIESE) refer to the number of sibling of a given node ) the right-most node is the root of the tree. (#(DIESE)的数量是指给定节点的兄弟节点数)最右边的节点是树的根。

  A <---- # <-------- # <------ # 
          |           |         |
          B   C<--#<--#         D 
                  |   |
                  E   F

more clearly the binary tree is like this picture 更清楚的是二叉树就像这张图片

在此输入图像描述

to do that i writing this code : 为此我写这段代码:

public static Node NaireTreeToBinaryTree (Node node,Document d)
{

    if (isLeaf(node))
    {
        return node;            
    }
    else 
    {
        List<Element> liste = GetChildren(node);
        Node tmp= d.createElement(node.getNodeName());          
        for (int i=0;i<liste.size();i++)
        {
            Element root = d.createElement("DIESE");
            root.appendChild(tmp);

            Element child2 = d.createElement(NaireTreeToBinaryTree(liste.get(i),d).getNodeName());
            root.appendChild(child2);
            tmp=root;

        }
      return tmp;
    }

}




public static void WritingIntoXML (Node node ,Document d)
{
    try{

        d.appendChild(node);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(d);


        // Output to console for testing
        StreamResult result2 = new StreamResult(System.out);

        transformer.transform(source, result);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

public static void main(String[] args) {

    Node root = GetNodeParent("1.xml"); // Get the node parent


    try{
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    Node a =NaireTreeToBinaryTree (root, doc);

    WritingIntoXML (a ,doc);


    }
    catch (Exception e )
    {
        e.printStackTrace();
    }



}

im getting this result (im puting DIESE(the name of a parent node) instead of # ) : 我得到这个结果(我把DIESE(父节点的名称)而不是#):

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <DIESE>
    <DIESE>
           <DIESE>
              <A/>
              <B/>
          </DIESE>
       <DIESE/>
    </DIESE>
    <D/>
</DIESE>

There are tree missing node C,E,F so i don't know why ? 树有缺失的节点C,E,F所以我不知道为什么? is the problem in the recursive methode NaireTreeToBinaryTree 是递归方法NaireTreeToBinaryTree中的问题

As you'll see, a copy error which replaces a new subtree by a node. 正如您将看到的,复制错误替换了节点的新子树。

public static Node naireTreeToBinaryTree (Node node,Document d)
{
    if (isLeaf(node))
    {
        //-return node;            
        return d.createElement(node.getNodeName());
    }
    else 
    {
        List<Element> liste = getChildren(node);
        Node tmp= d.createElement(node.getNodeName());          
        for (int i=0;i<liste.size();i++)
        {
            Element root = d.createElement("DIESE");
            root.appendChild(tmp);

            //-Element child2 = d.createElement(naireTreeToBinaryTree(liste.get(i),d).getNodeName());
            Node child2 = naireTreeToBinaryTree(liste.get(i),d);
            root.appendChild(child2);
            tmp=root;

        }
      return tmp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM