Because you're writing in C++, it would make sense to have methods that operate on a class such as Tree
(using my suggested rename of item
) be member functions rather than separate functions. For example, when I re-implemented your code as an object, I got this:
class Tree {
public:
Tree(char val) : value(val), children() {}
Tree(const char src_data[][2], int len);
bool operator<(const Tree &other) const { return value < other.value; }
bool PushChildren(const char src_data[][2], int len);
bool PushChildrenforAParent(const char src_data[][2], int len);
std::ostream& printLevel(std::ostream &out, int level) const {
for (int i=0; i<level; ++i)
out << " ";
out << value << '\n';
++level;
for (const auto &kid : children)
kid.printLevel(out, level);
return out;
}
friend std::ostream& operator<<(std::ostream &out, const Tree &node) {
return node.printLevel(out, 0);
}
private:
char value;
std::list<Tree> children;
// a function to get the parent:
static char getTopParent(const char src_data[][2], int len) {
for (int i = 0; i < len; ++i)
if (src_data[i][0] == '\0')
return src_data[i][1];
return '\0';
}
};
Note that one of the things I added was a means to print out the tree. That kind of thing can be very handy for debugging.