Home > Net >  Print sum of nodes' level of a n-ary tree in C
Print sum of nodes' level of a n-ary tree in C

Time:12-20

I need to find a solution for this problem: I have a n-ary tree structured in this way:

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

typedef struct kTreeVertex* kTree;

I can't use another implementation of n-ary tree. 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. The n-ary tree passed to my function is not empty (not null) by pre-condition.

sumLevels(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?

CodePudding user response:

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;
}
  • Related