• Tutorials
  • Courses
  • Tracks

Top | MCQs on Queue Data Structure with Answers | Question 25

Last Updated :
Discuss
Comments

Consider the below program, and identify what the function is doing.

C++
#include <iostream>
#include <queue>

class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int item) {
        data = item;
        left = right = nullptr;
    }
};

void function(Node* root) {
    if (root == nullptr)
        return;
    std::queue<Node*> q;

    q.push(root);

    while (!q.empty()) {
        Node* node = q.front();
        q.pop();
        std::cout << node->data << " ";

        if (node->left != nullptr)
            q.push(node->left);

        if (node->right != nullptr)
            q.push(node->right);
    }
}
C
#include <stdio.h>
#include <stdlib.h>

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

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

void function(struct Node* root) {
    if (root == NULL)
        return;
    struct Node** q = (struct Node**)malloc(100 * sizeof(struct Node*));
    int front = 0, rear = 0;

    q[rear++] = root;

    while (front < rear) {
        struct Node* node = q[front++];
        printf("%d ", node->data);

        if (node->left != NULL)
            q[rear++] = node->left;

        if (node->right != NULL)
            q[rear++] = node->right;
    }
    free(q);
}
Java
import java.util.LinkedList;
import java.util.Queue;

class Node {
    int data;
    Node left, right;
    Node(int item) {
        data = item;
        left = right = null;
    }
}

void function(Node root) {
    if (root == null)
        return;
    Queue<Node> q = new LinkedList<>();

    q.add(root);

    while (!q.isEmpty()) {
        Node node = q.poll();
        System.out.print(node.data + " ");

        if (node.left != null)
            q.add(node.left);

        if (node.right != null)
            q.add(node.right);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

from collections import deque

def function(root):
    if root is None:
        return
    q = deque()

    q.append(root)

    while q:
        node = q.popleft()
        print(node.data, end=' ')

        if node.left is not None:
            q.append(node.left)

        if node.right is not None:
            q.append(node.right)
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

function function(root) {
    if (root === null)
        return;
    let q = [];

    q.push(root);

    while (q.length > 0) {
        let node = q.shift();
        console.log(node.data);

        if (node.left !== null)
            q.push(node.left);

        if (node.right !== null)
            q.push(node.right);
    }
}

In order traversal of a tree

Normal traversal of a tree

Level order traversal of  a tree

None

Share your thoughts in the comments