Hi everyone. For a project for my class I am given a phylogenetic tree that is in an xml file. We are to use RapidXML to parse the data in this tree and to build a multiway tree in memory so that it can be searched in different ways. I know how to traverse the tree breadth first using a queue to create nodes from the xml node data, but I'm getting stuck on how to create relationships between these nodes. That is how to add a child node to a parent. I've posted my code below. Any help in the right direction would be greatly appreciated! I'm really stumped
class Node
{
public:
std::string name;
std::vector<Node *> children; //vector of pointers to the child nodes
Node(std::string n);
void add(Node* n) {children.push_back(n);}
Node* get(size_t i) {return children[i];}
size_t size() const {return children.size();}
};
Node::Node(std::string n)
{
name = n;
}
class Tree
{
public:
//pointer to the root node
Node *root;
Tree();
bool empty() const {return root == NULL;}
void setRoot(Node *n) {root = n;}
};
Tree::Tree()
{
root = NULL;
}
//helper method to convert xml node to a node
Node *buildNode(xml_node<> *n)
{
Node *node;
xml_node<> *name = n->first_node("name");
//check for Null name and set name of the node
if(name !=0)
{
node = new Node(name->value());
}
else
{
node = new Node("NULL");
}
return node;
}
void breadthFirst(xml_node<> *xml)
{
Tree *tree = new Tree();
//Initialize queue
queue<xml_node<>* > q;
//push the root node onto the queue
q.push(xml);
while(!q.empty())
{
//visit the xml node and create a node
Node *treeNode = buildNode(q.front());
//special case for root
if(tree->empty())
{
tree->root = treeNode;
}
//remove from queue
q.pop();
//loop through the children of the node and add to the queue
for(xml_node<> * kids = q.front()->first_node("species"); kids; kids = kids->next_sibling())
{
q.push(kids);
}
//Continue the above while there is still something in the queue
}
}
int main()
{
xml_document<> doc;
ifstream theFile ("File.xml");
vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
buffer.push_back('\0');
doc.parse<0>(&buffer[0]);
xml_node<> * root = doc.first_node("MyJournal");
return 0;
}
For the purposes of just figuring out how to build the tree, I'm using a simplified version of the phylogenetic xml file with a format like this: