字体:
Here my answer is, you'll see how simple it is to support tree with any type T and any degree thanks to C++ template [#2875712@0 -ROLIA.NET 相约加拿大网上社区 之 枫下论坛 & 枫下部落, 枫下论坛主坛 ]

Here my answer is, you'll see how simple it is to support tree with any type T and any degree thanks to C++ template

by super_unknown (mailman) at 2006.3.31 09:50 (#2875712@0)
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.

该话题已在历史区内,不能被修改或回复。       收藏    投诉
关闭窗口