简体   繁体   English

在 C 中打印 n 元树节点级别的总和

[英]Print sum of nodes' level of a n-ary tree in C

I need to find a solution for this problem: I have a n-ary tree structured in this way:我需要找到这个问题的解决方案:我有一个以这种方式构造的 n 叉树:

struct kTreeVertex {
    int                  key;
    struct kTreeVertex*  child;
    struct kTreeVertex*  sibling;
};

typedef struct kTreeVertex* kTree;

I can't use another implementation of n-ary tree.我不能使用 n-ary 树的另一个实现。 My goal is to print for each level the sum of nodes.我的目标是为每个级别打印节点总和。 My function take the pointer to the root of my n-ary tree.我的函数将指针指向我的 n 元树的根。 The n-ary tree passed to my function is not empty (not null) by pre-condition.根据前提条件,传递给我函数的 n 叉树不为空(非空)。

sumLevels(kTree t)总和(kTree t)

I can't find a way to complete this exercise.我找不到完成此练习的方法。 Below my solution, but it's not correct.在我的解决方案下方,但它不正确。

int sumLevels(kTree t){
    if(t->child == NULL){
        return t->key;
    }
    else{
        int sum = t->key;
        kTree c = t->child->sibling;
        while(c != NULL){
            sum += sumLevels(c);
            c = c->sibling;
        }
        printf("%d\n", sum);
    }  
}

if I have this tree:如果我有这棵树:

10
    5
        8
    3
    2
        1
    7

solution should be:
level 0: 10
level 1: 17
level 2: 9

Any ideas?有任何想法吗?

There several ways to approach this.有几种方法可以解决这个问题。 One is to perform a breadth-first traversal over the tree, so that you actually visit the nodes by their level.一种是对树执行广度优先遍历,以便您实际上按级别访问节点。

For this you need to collect tree nodes in an array.为此,您需要在数组中收集树节点。 You could for instance use a stack for this.例如,您可以为此使用堆栈。 Here is the structure and functions you would need for working with a stack:以下是使用堆栈所需的结构和函数:

struct stack {
    struct kTreeVertex* node;
    struct stack *next; 
};

void push(struct stack **stack, struct kTreeVertex* node) {
    struct stack* item = malloc(sizeof(struct stack));
    item->node = node;
    item->next = *stack;
    *stack = item;
}

struct kTreeVertex* pop(struct stack **stack) {
    struct stack* item = *stack;
    *stack = (*stack)->next;
    struct kTreeVertex *node = item->node;
    free(item);
    return node;
}

By using two stacks -- one for the current level, and one for the next -- you can get it working:通过使用两个堆栈——一个用于当前关卡,一个用于下一个关卡——你可以让它工作:

int sumLevels(struct kTreeVertex* root) {
    struct stack *level = NULL;
    struct stack *nextLevel;
    struct kTreeVertex* node;
    int depth = 0;
    
    push(&level, root);
    while (level != NULL) {
        int sum = 0;
        nextLevel = NULL;
        while (level != NULL) {
            node = pop(&level);
            sum += node->key;
            for (kTree child = node->child; child != NULL; child = child->sibling) { 
                push(&nextLevel, child);
            }
        }
        printf("level %d: %d\n", depth, sum);
        level = nextLevel;
        depth++;
    }
}

Driver code to see it work on the example tree:查看它在示例树上工作的驱动程序代码:

struct kTreeVertex* createNode(int key) {
    struct kTreeVertex* node = malloc(sizeof(*node));
    node->key = key;
    node->child = NULL;
    node->sibling = NULL;
    return node;
}

int main(int argc, char *argv[]) {
    kTree root = createNode(10);
    root->child = createNode(5);
    root->child->sibling = createNode(3);
    root->child->sibling->sibling = createNode(2);
    root->child->sibling->sibling->sibling = createNode(7);
    root->child->child = createNode(8);
    root->child->sibling->sibling->child = createNode(1);
    
    sumLevels(root);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM