Please write a nonrecursive procedure to print all nodes for

Please write a non-recursive procedure to print all nodes for a binary tree in a level order sequence.

Solution

#include <stdio.h>

#include <stdlib.h>

struct node

{

    int data;

    struct node* left, *right;

};

void printGivenLevel(struct node* root, int level);

int height(struct node* node);

struct node* newNode(int data);

void printLevelOrder(struct node* root)

{

    int h = height(root);

    int i;

    for (i=1; i<=h; i++)

        printGivenLevel(root, i);

}

void printGivenLevel(struct node* root, int level)

{

    if (root == NULL)

        return;

    if (level == 1)

        printf(\"%d \", root->data);

    else if (level > 1)

    {

        printGivenLevel(root->left, level-1);

        printGivenLevel(root->right, level-1);

    }

}

int height(struct node* node)

{

    if (node==NULL)

        return 0;

    else

    {

        int lheight = height(node->left);

        int rheight = height(node->right);

        if (lheight > rheight)

            return(lheight+1);

        else return(rheight+1);

    }

}

struct node* newNode(int data)

{

    struct node* node = (struct node*)

                        malloc(sizeof(struct node));

    node->data = data;

    node->left = NULL;

    node->right = NULL;

    return(node);

}

int main()

{

    struct node *root = newNode(1);

    root->left        = newNode(2);

    root->right       = newNode(3);

    root->left->left = newNode(4);

    root->left->right = newNode(5);

    printf(\"Level Order traversal of binary tree is \ \");

    printLevelOrder(root);

    return 0;

}

Please write a non-recursive procedure to print all nodes for a binary tree in a level order sequence.Solution#include <stdio.h> #include <stdlib.h>
Please write a non-recursive procedure to print all nodes for a binary tree in a level order sequence.Solution#include <stdio.h> #include <stdlib.h>

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site