tags:

views:

58

answers:

3
int countNodes( TreeNode *root ) {
           // Count the nodes in the binary tree to which
           // root points, and return the answer.
        if ( root == NULL )
           return 0;  // The tree is empty.  It contains no nodes.
        else {
           int count = 1;   // Start by counting the root.
           count += countNodes(root->left);  // Add the number of nodes
                                            //     in the left subtree.
           count += countNodes(root->right); // Add the number of nodes
                                            //    in the right subtree.
           return count;  // Return the total.
        }
     } // end countNodes()

output........ ........... some thing like this

+2  A: 

Tinyxml is pretty easy to use and its free or if its a simple XML layout you can write your own.

http://www.grinninglizard.com/tinyxml

skimobear
+1  A: 

XML files are just text files with special formatting and control characters.

Without more information ragarding what exactly it is that you want or what c++ libraries or version that you're using it's difficult to advise you directly.

Are you after something like this for example?

<RootNode>
  <Node>
     <Property Name="Node1"/>
     <Node>
       <Property Name="Sub-node1">
     </Node>
  <Node/>
  <Node Name="Node2"/>
</RootNode>
ChrisBD
i m using viusal studio 9 and i want my program to generate stuff like above.
nomi
+1  A: 

Your question is unclear, so I guess you want to "output data into an XML file"

Outputing data into a file stream in an XML format could be something like:

#include <iotream>
#include <fstream>

int main(int argc, char * argv[])
{
   TreeNode * root = doWhataverGizmoYouWantToCreateThat() ;
   int count = countNodes(root) ;
   delete root ;

   std::fstream output("output.xml", std::ios_base::out)
   output << "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" ;
   output << "<count value=\"" << count << "\" />\n" ;

   return 0 ;
}

Please read the iostream help for more information about the fstream object and its uses:

http://www.cplusplus.com/reference/iostream/

Now, if you want to parse and modify an existing XML, you'll need an XML parser. You can google those parsers, for example with:

http://www.google.com/search?q=XML+c%2B%2B+parser

Edit:

After reading the comments :

XML is a kind of organized text file. Somewhat like HTML, if is about elements, attributes, and data. For example, this is an XML file content:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This is a comment -->
<!-- The first line will declare the XML file, as well as
         its version, and its encoding -->
<my_element>
<!-- this is an element. It can contain others elements,
         as well as text data and attributes -->
   <my_other_element my_attribute="some_value" />
   <!-- my_other_element has an attribute whose name is
         my_attribute, and whose value is some_value -->
   <my_another_element>Some text value</my_another_element>
   <!-- my_another_element has an attribute whose content
         is the following text "Some text value" -->
<my_element>
<!-- this is the end of my_element, closing it -->

For more information, read :

http://www.google.com/search?q=XML

paercebal
is this programe dynamically generate xml format for a tree or i have to hard code it?
nomi
You have to hard-code it. If you need to play with XML, then you have some days/weeks of learning to do. You won't get it with a simple SO question. Follow the links, play with tutorials, until you get it. Hard-coding as I did it is the wrong solution for a robust XML export unless you're very very proficient with XML (you know the dark corners, etc.), but it can help for some things. If you want to have a robust solution, you'll need to use a parser which is able to output the data. Xerces comes to mind, even if it is somewhat "large".
paercebal