I am trying to implement a class for the Node of a Tree in C++ in order to represent an HTML structure. It is not complete by a shot, but i'd still like an opinion.
TreeNode.h :
#ifndef TreeNode_H
#define TreeNode_H
#include <string>
#include <vector>
class TreeNode
{
private:
std::string textContent;
std::string tagName;
TreeNode *parent;
std::vector<TreeNode *> children;
int countNodesRec(TreeNode *root, int& count);
public:
TreeNode();
TreeNode(std::string iTextContent, std::string iTagName);
void appendChild(TreeNode *child);
void setParent(TreeNode *parent);
void popBackChild();
void removeChild(int pos);
bool hasChildren();
bool hasParent();
TreeNode* getParent();
TreeNode* getChild(int pos);
int childrenNumber();
int grandChildrenNum();
std::string getTextContent();
std::string getTagName();
};
#endif
TreeNode.cpp :
#include "TreeNode.h"
#include <string>
#include <vector>
#include <iostream>
TreeNode::TreeNode() {};
TreeNode::TreeNode(std::string iTextContent, std::string iTagName) :
textContent(iTextContent),
tagName(iTagName),
parent(NULL)
{}
int TreeNode::countNodesRec(TreeNode *root, int& count)
{
TreeNode *parent = root;
TreeNode *child = NULL;
for(int it = 0; it < parent->childrenNumber(); it++)
{
child = parent->getChild(it);
count++;
//std::cout<<child->getTextContent()<<" Number : "<<count<<std::endl;
if(child->childrenNumber() > 0)
{
countNodesRec(child, count);
}
}
return count;
}
void TreeNode::appendChild(TreeNode *child)
{
child->setParent(this);
children.push_back(child);
}
void TreeNode::setParent(TreeNode *theParent)
{
parent = theParent;
}
void TreeNode::popBackChild()
{
children.pop_back();
}
void TreeNode::removeChild(int pos)
{
if(children.size() > 0) {
children.erase(children.begin()+ pos);
}
else {
children.pop_back();
}
}
bool TreeNode::hasChildren()
{
if(children.size() > 0)
return true;
else
return false;
}
bool TreeNode::hasParent()
{
if(parent != NULL)
return true;
else
return false;
}
TreeNode * TreeNode::getParent()
{
return parent;
}
TreeNode* TreeNode::getChild(int pos)
{
if(children.size() < pos)
return NULL;
else
return children[pos];
}
int TreeNode::childrenNumber()
{
return children.size();
}
int TreeNode::grandChildrenNum()
{
int t = 0;
if(children.size() < 1)
{
return 0;
}
countNodesRec(this, t);
return t;
}
std::string TreeNode::getTextContent()
{
return textContent;
}
std::string TreeNode::getTagName()
{
return tagName;
}
Sample code :
#include <iostream>
#include <vector>
#include "TreeNode.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
TreeNode *itr = NULL;
TreeNode *node = new TreeNode("k", "p");
node->appendChild(new TreeNode("test1", "testag"));
node->appendChild(new TreeNode("test2", "testag"));
node->appendChild(new TreeNode("test3", "testag"));
itr = node->getChild(0);
itr->appendChild(new TreeNode("test1a", "testtag"));
itr->appendChild(new TreeNode("test1b", "testtag"));
itr->getChild(0)->appendChild(new TreeNode("test1aa", "testtag"));
itr = node->getChild(1);
itr->appendChild(new TreeNode("test2a", "testtag"));
itr->appendChild(new TreeNode("test2b", "testtag"));
itr->getChild(0)->appendChild(new TreeNode("test2aa", "testtag"));
std::cout<<node->grandChildrenNum();
return 0;
}
-Wall -Wextra -pedantic
to your compile flags in DevCpp to catch more warnings. – Emily L. 2 days ago