-
双亲表示法(Parent Representation):
- 在双亲表示法中,每个节点存储指向其父节点的指针。这意味着每个节点都直接指向其父节点,从而构成了树的层次结构。
- 优点:容易找到节点的父节点。
- 缺点:相对较高的空间复杂度,因为每个节点都需要额外存储父节点的指针。在查找兄弟节点或者子节点时可能需要遍历整个树。
#include <stdio.h> #define MAX_NODES 100 struct TreeNode { int data; int parentIndex; // Index of parent node in array }; struct TreeNode tree[MAX_NODES]; void initTree() { for (int i = 0; i < MAX_NODES; i++) { tree[i].data = -1; // Initialize data as -1 to indicate empty node tree[i].parentIndex = -1; // Initialize parentIndex as -1 } } void addChild(int parentIndex, int childData) { int i = 0; while (tree[i].data != -1) { i++; } tree[i].data = childData; tree[i].parentIndex = parentIndex; } void displayTree() { for (int i = 0; i < MAX_NODES; i++) { if (tree[i].data != -1) { printf("Node: %d, Parent: %d\n", tree[i].data, tree[i].parentIndex); } } } int main() { initTree(); addChild(-1, 1); // Adding root node addChild(0, 2); // Adding child node with parent index 0 displayTree(); return 0; }
-
孩子表示法(Child Representation):
- 在孩子表示法中,每个节点存储指向其第一个孩子节点的指针,而其他孩子节点通过它们的兄弟关系链接起来。
- 优点:简单且易于理解,适用于需要频繁查找孩子节点的应用场景。
- 缺点:不容易直接获取节点的父节点。
#include <stdio.h> #define MAX_NODES 100 struct TreeNode { int data; struct TreeNode *firstChild; struct TreeNode *nextSibling; }; struct TreeNode tree[MAX_NODES]; void initTree() { for (int i = 0; i < MAX_NODES; i++) { tree[i].data = -1; // Initialize data as -1 to indicate empty node tree[i].firstChild = NULL; tree[i].nextSibling = NULL; } } void addChild(int parentIndex, int childData) { int i = 0; while (tree[i].data != -1) { i++; } tree[i].data = childData; if (tree[parentIndex].firstChild == NULL) { tree[parentIndex].firstChild = &tree[i]; } else { struct TreeNode *current = tree[parentIndex].firstChild; while (current->nextSibling != NULL) { current = current->nextSibling; } current->nextSibling = &tree[i]; } } void displayTree(struct TreeNode *node, int depth) { if (node != NULL) { for (int i = 0; i < depth; i++) { printf("\t"); } printf("%d\n", node->data); displayTree(node->firstChild, depth + 1); displayTree(node->nextSibling, depth); } } int main() { initTree(); addChild(0, 1); // Adding root node addChild(0, 2); // Adding child node with parent index 0 addChild(1, 3); // Adding child node with parent index 1 displayTree(&tree[0], 0); return 0; }
-
孩子双亲表示法(Child-Parent Representation):
- 孩子双亲表示法结合了孩子表示法和双亲表示法的优点,每个节点同时存储指向其第一个孩子节点和其父节点的指针。
- 优点:可以快速找到节点的父节点和孩子节点。
- 缺点:相对较高的空间复杂度,因为每个节点需要额外存储指向父节点的指针。
#include <stdio.h> #define MAX_NODES 100 struct TreeNode { int data; int parentIndex; struct TreeNode *firstChild; }; struct TreeNode tree[MAX_NODES]; void initTree() { for (int i = 0; i < MAX_NODES; i++) { tree[i].data = -1; // Initialize data as -1 to indicate empty node tree[i].parentIndex = -1; tree[i].firstChild = NULL; } } void addChild(int parentIndex, int childData) { int i = 0; while (tree[i].data != -1) { i++; } tree[i].data = childData; tree[i].parentIndex = parentIndex; if (tree[parentIndex].firstChild == NULL) { tree[parentIndex].firstChild = &tree[i]; } else { struct TreeNode *current = tree[parentIndex].firstChild; while (current->nextSibling != NULL) { current = current->nextSibling; } current->nextSibling = &tree[i]; } } void displayTree() { for (int i = 0; i < MAX_NODES; i++) { if (tree[i].data != -1) { printf("Node: %d, Parent: %d\n", tree[i].data, tree[i].parentIndex); } } } int main() { initTree(); addChild(-1, 1); // Adding root node addChild(0, 2); // Adding child node with parent index 0 displayTree(); return 0; }
-
孩子兄弟表示法(Child-Sibling Representation):
- 孩子兄弟表示法是一种树的表示方法,每个节点存储指向其第一个孩子节点和下一个兄弟节点的指针。
- 优点:相对简单的存储结构和快速的查找节点的孩子节点和兄弟节点的速度。
- 缺点:在插入和删除节点时需要维护孩子节点和兄弟节点之间的关系,以及不容易直接获取节点的父节点。
#include <stdio.h> struct TreeNode { int data; struct TreeNode *firstChild; struct TreeNode *nextSibling; }; struct TreeNode *createNode(int data) { struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); node->data = data; node->firstChild = NULL; node->nextSibling = NULL; return node; } void addChild(struct TreeNode *parent, int data) { struct TreeNode *child = createNode(data); if (parent->firstChild == NULL) { parent->firstChild = child; } else { struct TreeNode *sibling = parent->firstChild; while (sibling->nextSibling != NULL) { sibling = sibling->nextSibling; } sibling->nextSibling = child; } } void displayTree(struct TreeNode *root, int depth) { if (root != NULL) { for (int i = 0; i < depth; i++) {
我是比较倾向于使用孩子兄弟表示法,这简直是天才的想法,找个孩子王站在左边,右边全是兄弟,无论是遍历还是查找都非常简单,更重要的是方便完全二叉树的实现。
树的表示方法(双亲、孩子、孩子双亲、孩子兄弟)
最新推荐文章于 2024-08-24 16:03:16 发布