Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:42:57+00:00 2026-06-05T05:42:57+00:00

Im trying to convert a general tree (tree that have one or more children

  • 0

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 :

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

so i can represent the binary tree like this :

enter image description here

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.

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

more clearly the binary tree is like this picture

enter image description here

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 # ) :

 <?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 ?
is the problem in the recursive methode NaireTreeToBinaryTree

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:42:58+00:00Added an answer on June 5, 2026 at 5:42 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to convert a general tree (unlimited child nodes) associate to an XML
I have an XML file and I'm trying convert it into a (table( HTML
I am trying to convert xsd file to the .net class. I have searched
I'm trying convert xml from one format to other using the XslCompiledTransform in c#.
Trying to convert a perl script to php. In perl I have a hash
Trying to convert some VB to C#... (learning C#, too). I have some code
I am trying to make my java application more standards compliant and one of
I'm trying to write a method that compares 2 objects. The problem I have
I have some existing code in C# that I'm trying to move to an
I have a C++ DLL that I am trying to interface with using C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.