【问题标题】:Generate Binary Tree form N aire Tree- recursive method doesn't work从 N aire Tree 生成二​​叉树 - 递归方法不起作用
【发布时间】:2012-06-08 23:25:46
【问题描述】:

我正在尝试将通用树(具有一个或多个子级的树)转换为二叉树。我的通用树由 XML 文件名“1.xml”表示,其中包含:

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

所以我可以像这样表示二叉树:

现在要将这棵树转换为二叉树,我使用以下方法:

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

(#(DIESE)的个数指给定节点的兄弟节点个数) 最右边的节点是树的根。

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

更清楚的二叉树是这样的图片

为此,我编写了这段代码:

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



}

我得到这个结果(我把 DIESE(父节点的名称)而不是 # ):

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

有树缺少节点 C,E,F 所以我不知道为什么? 是递归方法中的问题 NaireTreeToBinaryTree

【问题讨论】:

  • 您可能想用 dom 替换 jdom 标记...这不是 JDOM 相关问题(不是 xml 解析问题...)。

标签: java tree xml-parsing binary-tree jdom


【解决方案1】:

如您所见,复制错误会用节点替换新子树。

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

【讨论】:

  • @WassimSboui 很高兴看到一个基本正确的紧凑型解决方案。我也喜欢优雅的描绘。只有 C-sharpish 拼写会伤害 ;)。
猜你喜欢
  • 1970-01-01
  • 2013-06-05
  • 2020-09-29
  • 1970-01-01
  • 2020-02-10
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多