Reputation:
This is related to one of my previous questions. I'm trying to build a tree from an xml file. I plan to go through the xml file and create objects out of the data and create a vector of the objects in preorder and inorder so that I can construct the tree object. The xml file I'm using is much more dense than this(about 23 levels or so) but here is an example of the layout:
<?xml version="1.0" encoding="utf-8"?>
<MyJournal>
<species>
<name>Sea Creature</name>
<species>
<name>Fish</name>
<species>
<name>swordfish</name>
</species>
<species>
<name>grouper</name>
</species>
</species>
<species>
<name>Mammal</name>
<species>
<name>dolphin</name>
</species>
<species>
<name>whale</name>
</species>
</species>
</species>
<species>
<name>Land animal</name>
<species>
<name>Mammal</name>
<species>
<name>dog</name>
</species>
<species>
<name>cat</name>
</species>
</species>
<species>
<name>Bird</name>
<species>
<name>blue jay</name>
</species>
<species>
<name>robin</name>
</species>
</species>
</species>
</MyJournal>
My preorder and postorder methods. The root node "MyJournal" is passed in first.
void preOrder(xml_node<> *species) // add to left node
{
Node t1 = *new Node();
xml_node<> * name_node = species->first_node("name");
//If there is a value for name, set the name of the node to this name
if(name_node != 0)
{
t1.setName(name_node->value());
cout << t1.getName() << " ";
}
//If there is no name node, print the default name which is "ROOT"
if(name_node == 0)
{
cout << t1.getName() << " ";
}
for (xml_node<> * child_node = species->first_node("species"); child_node; child_node = child_node->next_sibling())
{
loop2(child_node); // add these to right node
}
}
void postOrder(xml_node<> *species) // add to left node
{
Node t1 = *new Node();
for (xml_node<> * child_node = species->first_node("species"); child_node; child_node = child_node->next_sibling())
{
loop3(child_node); // add these to right node
}
xml_node<> * name_node = species->first_node("name");
if(name_node != 0)
{
t1.setName(name_node->value());
cout << t1.getName() << " ";
}
if(name_node == 0)
{
cout << t1.getName() << " ";
}
}
So far, I'm able to go through the file and get the objects in preorder and postorder, but I can't figure out how to get the data in inorder since there are no left or right pointers to call the function recursively. Could anyone give me some guidance on how to go about this? Is it possible to get the data this way and successfully build a tree structure? Thank you for any help!
Upvotes: 0
Views: 639
Reputation: 68074
The concepts of pre,post and in-order traversal only apply to binary trees : Trees where each node has at most two children.
XML data (in general) doesn't have to follow this : each node can have any number of children.
If your XML file has to be interpeted as a binary tree, then just treat the first species node as the "left" and it's sibling (if present) as the "right".
Note that in this case, your loops through all siblings can't make any sense.
Upvotes: 1