すべてのノードにカスタム数の「息子」を持つツリーを構築する必要があります(ノードの「息子」へのポインタを持つ動的テーブル):
class node{
public
string data;
int number_of_sons;
struct node *father, **sons; // **sons is table of pointers to the "sons" of the node;
node(string s, int n) : data(s), number_of_sons(n){
}//constructor
};
そしてリストクラス:
class tree{
public:
node *root;
tree() : root(NULL){
}
};
この方法でツリーとツリーのノードを作成します。
tree t1 = new tree();
node n1 = new node("example1", 1);
node n2 = new node("example2", 2);
node n3 = new node("example3", 1);
node n4 = new node("example4", 3);
そして、私はそれらをツリーに「手動で」挿入しようとしていますが、これは機能していません:
n1->father = NULL;
t1->root = n1;
//adding the second object to the tree:
n2->father = root;
t1->root->sons[0] = n2;
空のツリーに「n1」を追加すると機能しますが、2 番目の操作は正しくありません。この種の木の扱い方について誰かアドバイスをもらえますか?ルートに新しいノードを追加する方法は?