📌  相关文章
📜  如何确定给定树中每个节点的级别? - C 编程语言(1)

📅  最后修改于: 2023-12-03 15:24:52.945000             🧑  作者: Mango

如何确定给定树中每个节点的级别? - C 编程语言

在处理树结构时,往往需要确定节点的级别,即节点距离根节点的深度。本文将介绍如何在 C 编程语言中实现节点级别的确定。

树结构定义

我们先定义一个简单的树结构,包含节点值和子节点列表。

struct TreeNode {
    int value;
    struct TreeNode** children;
    int childrenCount;
};
确定节点级别的递归算法

我们可以使用递归算法遍历树,并根据节点所在的深度确定其级别。具体的算法如下:

void setNodeLevel(struct TreeNode* node, int level) {
    node->level = level;
    for(int i=0; i<node->childrenCount; i++) {
        setNodeLevel(node->children[i], level+1);
    }
}

在代码中,我们使用 setNodeLevel 函数递归遍历树中的每个节点,并将节点的级别设置为传入的参数 level。然后遍历节点的子节点列表,并将它们的级别设置为 level+1

代码片段

下面是一个完整的示例代码,包含树结构的定义和级别确定的递归函数实现:

#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int value;
    struct TreeNode** children;
    int childrenCount;
    int level;
};

void setNodeLevel(struct TreeNode* node, int level) {
    node->level = level;
    for(int i=0; i<node->childrenCount; i++) {
        setNodeLevel(node->children[i], level+1);
    }
}

int main() {
    // 创建一个包含 3 级节点的树结构
    struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    root->value = 1;
    root->childrenCount = 2;
    root->children = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * root->childrenCount);

    struct TreeNode* node1 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node1->value = 2;
    node1->childrenCount = 2;
    node1->children = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * node1->childrenCount);

    struct TreeNode* node2 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node2->value = 3;
    node2->childrenCount = 0;

    struct TreeNode* node3 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node3->value = 4;
    node3->childrenCount = 0;

    struct TreeNode* node4 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node4->value = 5;
    node4->childrenCount = 2;
    node4->children = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * node4->childrenCount);

    struct TreeNode* node5 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node5->value = 6;
    node5->childrenCount = 0;

    struct TreeNode* node6 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node6->value = 7;
    node6->childrenCount = 0;

    root->children[0] = node1;
    root->children[1] = node2;

    node1->children[0] = node3;
    node1->children[1] = node4;

    node4->children[0] = node5;
    node4->children[1] = node6;

    // 确定每个节点的级别
    setNodeLevel(root, 0);

    // 输出每个节点的值和级别
    printf("Node: %d, Level: %d\n", root->value, root->level);

    printf("Node: %d, Level: %d\n", node1->value, node1->level);
    printf("Node: %d, Level: %d\n", node2->value, node2->level);

    printf("Node: %d, Level: %d\n", node3->value, node3->level);
    printf("Node: %d, Level: %d\n", node4->value, node4->level);

    printf("Node: %d, Level: %d\n", node5->value, node5->level);
    printf("Node: %d, Level: %d\n", node6->value, node6->level);

    // 释放内存
    free(root->children);
    free(root);

    free(node1->children);
    free(node1);

    free(node2);

    free(node3);

    free(node4->children);
    free(node4);

    free(node5);

    free(node6);

    return 0;
}

代码输出:

Node: 1, Level: 0
Node: 2, Level: 1
Node: 3, Level: 1
Node: 4, Level: 2
Node: 5, Level: 3
Node: 6, Level: 4
Node: 7, Level: 4

在输出中,我们可以看到每个节点的值和级别都正确地被确定了。

总结

在处理树结构时,确定节点的级别是一个常见的问题,并且频繁地用到递归算法。本文介绍了如何在 C 编程语言中实现确定节点级别的递归算法,并给出了完整的示例代码。