我有一个结构 s :
struct s{
int x;
/********************************************************************
* NoOfchild doesn't represent maximum no of children for node s .
* This represent no of children node s have at any given instance .
*********************************************************************/
int NoOfChild;
s **child;
}
我想使用 ** 来动态声明指针数组。节点 s 被一个一个地添加到 array 中。有任何方法可以实现这一点。这棵树将用于 FpGrowth Algorithm .
*(0)
|
_____________________________________________________________
| | |
[* (1) *(2) *(3)]
| | |
_______________ _________________ __________________________
| | | | | | | | | | | | |
[* * * *] [* * *] [* * * * * *]
** 代表节点 s 。我不想declare all children of a node at the same time
即我想添加子节点 one by one
什么时候是required
.例如o 作为根添加,然后节点 1 作为根的子节点添加(如果需要),然后添加节点 2,依此类推。[* * * * ] 表示节点 x 的子节点。
编辑:
人们假设 NoOfChild 为 maximum no of a child
对于不正确的给定节点,这里 NoOfChild 表示一个节点在给定实例中有多少个子节点,它可能会根据要求或时间而变化。
解释:
最初节点 0 已初始化,因此它有零(0)个子节点。
然后将节点 1 添加为节点 0 的子节点,因此 o->NoOfChild = 1 和 1 ->NoOfChild = 0 ;
然后将节点 [*] 添加为节点 1 的子节点,因此 0->NoOfChild = 1 和 1 ->NoOfChild = 1 ;
然后将 2 添加为节点 0 的子节点,因此 0->NoOfChild = 2 和 1 ->NoOfChild = 1 ;
等等。
编辑:
终于用上了vector<s*> child
.
最佳答案
对于一般的树数据结构,您可以使用:-
struct tree{
int element;
struct tree *firstchild;
struct tree *nextsibling;
};
元素包含要在节点处插入的数据。
FirstChild 包含节点的第一个 child 。
nextsibling 包含同一父节点的另一个子节点。 示例:-
A
B C D
EF G H
然后
A->firstchild = B;
B->nextsibling=C;
C->nextsibling=D;
B->第一个 child =E;
E->nextsibling=F;
C->第一个 child =g;
D->firstchild=H;
其他未指定的值可以取NULL;
关于c++ - 我如何在 C++ 中将双(**)指针用于一般树数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102323/