本文发表在 rolia.net 枫下论坛template <int X, typename T>
struct Node {
T value;
Node* children[X];
void initChildren() {
for(int i=0; i<X; i++) {
children[i]=NULL;
}
}
Node(T tmpvalue) : value(tmpvalue) {
initChildren();
}
Node() {
initChildren();
}
};
template <int X, typename T>
class Tree {
private :
typedef Node<X,T> treeNode;
treeNode header;
/*
add whatever methods here, whose logic is about the same as the countper part in C, insert, delete, traverse etc
*/
};
This is the basic look of the tree class, which supports any type of element T
and any degree X. I didn't bother finishing the rest of the code as
it is quite straightforward. in your case, you want degree up to 4, and suppose you want a tree of int, then just instantiate it like this
typedef Tree<4,int> DegreeFourIntTree; // typedef to make it less confusing
DegreeFourIntTree tmptree;
yes, just this simple
BTW, as silly as it may sound, when I first read your question, I didn't know what the term "degree" is :-), and was confused. But seriously, your question is really easy , just as you said.更多精彩文章及讨论,请光临枫下论坛 rolia.net
struct Node {
T value;
Node* children[X];
void initChildren() {
for(int i=0; i<X; i++) {
children[i]=NULL;
}
}
Node(T tmpvalue) : value(tmpvalue) {
initChildren();
}
Node() {
initChildren();
}
};
template <int X, typename T>
class Tree {
private :
typedef Node<X,T> treeNode;
treeNode header;
/*
add whatever methods here, whose logic is about the same as the countper part in C, insert, delete, traverse etc
*/
};
This is the basic look of the tree class, which supports any type of element T
and any degree X. I didn't bother finishing the rest of the code as
it is quite straightforward. in your case, you want degree up to 4, and suppose you want a tree of int, then just instantiate it like this
typedef Tree<4,int> DegreeFourIntTree; // typedef to make it less confusing
DegreeFourIntTree tmptree;
yes, just this simple
BTW, as silly as it may sound, when I first read your question, I didn't know what the term "degree" is :-), and was confused. But seriously, your question is really easy , just as you said.更多精彩文章及讨论,请光临枫下论坛 rolia.net