0% found this document useful (0 votes)
24 views14 pages

Module 4

DATA STRUCTURE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Module 4

DATA STRUCTURE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Department of Computer Science and

Engineering
Preparing Better Computer Professionals for a Real World

Lecture Notes on

DATA STRUCTURES & APPLICATIONS


BCS304

Module 4

Compiled by :
Dr. Bhavanishankar K
Professor
Dept. of CSE
// demonstration of level order traversals
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *left;
struct Node *right;
};

struct Node *createNode (int data)


{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

void levelOrderTraversal (struct Node *root)


{
if (root == NULL)
return;

// create a queue and enqueue the root node


struct Node **queue =
(struct Node **) malloc (sizeof (struct Node *) * 100);
int front = 0, rear = 0;
queue[rear++] = root;

while (front < rear)


{
// dequeue a node from the queue
struct Node *current = queue[front++];

// process current node


printf ("%d ", current->data);

// enqueue the left child


if (current->left != NULL)
{
queue[rear++] = current->left;
}
// enqueue the right child
if (current->right != NULL)
{
queue[rear++] = current->right;
}
}
}

int main ()
{
struct Node *root = createNode (1);
root->left = createNode (2);
root->right = createNode (3);
root->left->left = createNode (4);
root->left->right = createNode (5);
root->right->left = createNode (6);
root->right->right = createNode (7);

printf ("Level order traversal of binary tree: ");


levelOrderTraversal (root);

return 0;
}

You might also like