So I'm trying to create a n-ary data tree. The tree is composed of TreeNodes, which store some sort of data. Each TreeNode has a vector of children; this allows me to have any number of children coming from a tree.
What I'm trying to do is allow every TreeNode to have a unique type of data. My biggest problem so far is that using templates forces me to define a data type, when I have no clue about what kind of data will be there. This is what I have so far, and naturally it's not working:
//BEGIN TREENODE DEFINITION
template<class T> class TreeNode{
TreeNode *parent;
vector<TreeNode*> child; //Make a vector of children
T data;
public:
template<class T> void TreeNode<T>(TreeNode &parent, vector<TreeNode*> &child) {//constructor
this.parent = parent;
this.child = child;
}
};
//END TREENODE DEFINITION
//BEGIN TREEMAP DEFINITION
class NTree {
TreeNode<T> root = new TreeNode<T> (NULL, new vector<TreeNode* child); //HELP! What if I don't want to define a data type just yet?
int size;
public:
void NTree(TreeNode<T> root){};
//add, etc. down here...
};
So, my questions are:
*Is there a way I can allow each node to have a unique data type?
*Can I delay type assignment until later on?
TreeNode<T> root = new TreeNode<T> (NULL, new vector<TreeNode* child); //HELP! What if I don't want to define a data type just yet?
I think you cannot use TreeNode<T> withour any data type,
TreeNode<T> root = new TreeNode<T>
there is a way to avoid the problem, by using tamplates for NTree also
1 2 3 4 5 6 7 8
templeates<class T> class NTree {
TreeNode<T> root = new TreeNode<T> (NULL, new vector<TreeNode* child); //HELP! What if I don't want to define a data type just yet?
int size;
public:
void NTree(TreeNode<T> root){};
//add, etc. down here...
};