#include <stdio.h>
#include <stdlib.h>
// 定义结构体
struct Leaf {
int id;
struct Leaf *next;
};
struct Branch {
struct Leaf *leaves;
struct Branch *subBranch;
};
struct Tree {
struct Branch *root;
};
// 构建树形结构
struct Tree* createTree() {
struct Tree *tree = (struct Tree*)malloc(sizeof(struct Tree));
if (!tree) return NULL;
tree->root = (struct Branch*)malloc(sizeof(struct Branch));
if (!tree->root) {
free(tree);
return NULL;
}
tree->root->subBranch = (struct Branch*)malloc(2 * sizeof(struct Branch));
if (!tree->root->subBranch) {
free(tree->root);
free(tree);
return NULL;
}
int id = 1;
for (int i = 0; i < 2; ++i) {
tree->root->subBranch[i].leaves = (struct Leaf*)malloc(3 * sizeof(struct Leaf));
if (!tree->root->subBranch[i].leaves) {
// 释放之前分配的内存
for (int j = 0; j < i; ++j) {
free(tree->root->subBranch[j].leaves);
}
free(tree->root->subBranch);
free(tree->root);
free(tree);
return NULL;
}
for (int j = 0; j < 3; ++j) {
tree->root->subBranch[i].leaves[j].id = id++;
tree->root->subBranch[i].leaves[j].next = (j == 2)? NULL : &tree->root->subBranch[i].leaves[j + 1];
}
tree->root->subBranch[i].subBranch = NULL;
}
return tree;
}
// 计算所有Leaf节点id的总和
int sumLeafIds(struct Tree *tree) {
int sum = 0;
struct Branch *currentBranch = tree->root->subBranch;
for (int i = 0; i < 2; ++i) {
struct Leaf *currentLeaf = currentBranch[i].leaves;
while (currentLeaf) {
sum += currentLeaf->id;
currentLeaf = currentLeaf->next;
}
}
return sum;
}
// 释放整棵树占用的内存
void freeTree(struct Tree *tree) {
struct Branch *currentBranch = tree->root->subBranch;
for (int i = 0; i < 2; ++i) {
struct Leaf *currentLeaf = currentBranch[i].leaves;
struct Leaf *nextLeaf;
while (currentLeaf) {
nextLeaf = currentLeaf->next;
free(currentLeaf);
currentLeaf = nextLeaf;
}
}
free(tree->root->subBranch);
free(tree->root);
free(tree);
}
int main() {
struct Tree *tree = createTree();
if (tree) {
printf("Sum of Leaf IDs: %d\n", sumLeafIds(tree));
freeTree(tree);
}
return 0;
}
说明
- 构建树形结构:
- 使用
malloc
依次为Tree
、root
、两个subBranch
和六个Leaf
节点分配内存。在分配过程中,若任何一步失败,都要释放之前分配的内存以避免内存泄漏。
- 为每个
Leaf
节点的id
依次赋值并设置next
指针。
- 计算总和:
- 遍历每个
subBranch
下的Leaf
节点链表,累加id
值。
- 释放内存:
- 先释放每个
subBranch
下的Leaf
节点链表,再释放subBranch
数组,最后释放root
和Tree
。这样可以有效避免内存碎片化和泄漏问题。