我有一个只有父母和孩子的 n 叉树结构。spantree 本身只包含一个节点,即根。然后创建与其他节点或根链接的节点。每个节点(包括根)最多允许有 MAXCHILDREN 个子节点。这是结构:
typedef struct node{
unsigned int id; //every node has different id
struct node* parent; //points to parent node
struct node* children[MAXCHILDREN]; //pointers to all children of this node
}node;
typedef struct spantree{
node root; //root node
}spantree;
视觉图:
root
___O
/ / \
O O O
/ \
O O
在我创建了我的树之后,我想释放整个事情,但我不确定用哪种方式去做。我不能从根开始解除分配,因为这样树就会被破坏。所以我想我必须从叶子开始,一直到根?但这意味着我必须先找到最深的叶子,对吧?我对如何开始感到很困惑。
我不认为这是必要的,但为了保险,这是我每次需要创建新节点时使用的:
node *newNode;
newNode=(node*)malloc(sizeof(node));
//then I modify it to my preferences