Print all Prime Levels of a Binary Tree

Given a Binary Tree, the task is to print all prime levels of this tree.

Any level of a Binary tree is said to be a prime level, if all nodes of this level are prime.

Examples:

Input: 
                 1
                /  \ 
              15    13 
             /     /   \ 
            11    7     29 
                   \    / 
                   2   3  
Output: 11 7 29
         2 3
Explanation: 
Third and Fourth levels are prime levels.

Input:
                  7
                /  \ 
              23     41 
             /  \      \
            31   16     3 
           / \     \    / 
          2   5    17  11    
                   /
                  23 
Output: 7
         23 41
         2 5 17 11
         23
Explanation: 
First, Second, Fourth and 
Fifth levels are prime levels.

Approach: In order to check if a level is Prime level or not,

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for printing a prime
// levels of binary Tree
  
#include <bits/stdc++.h>
using namespace std;
  
// A Tree node
struct Node {
    int key;
    struct Node *left, *right;
};
  
// Utility function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return (temp);
}
  
// Function to check whether node
// Value is prime or not
bool isPrime(int n)
{
    if (n == 1)
        return false;
  
    // Iterate from 2 to sqrt(n)
    for (int i = 2; i * i <= n; i++) {
  
        // If it has a factor
        if (n % i == 0) {
            return false;
        }
    }
  
    return true;
}
  
// Function to print a Prime level
void printLevel(struct Node* queue[],
                int index, int size)
{
    for (int i = index; i < size; i++) {
        cout << queue[i]->key << " ";
    }
  
    cout << endl;
}
  
// Function to check whether given level is
// prime level or not
bool isLevelPrime(struct Node* queue[],
                  int index, int size)
{
    for (int i = index; i < size; i++) {
        // Check value of node is
        // pPrime or not
        if (!isPrime(queue[index++]->key)) {
            return false;
        }
    }
  
    // Return true if for loop
    // iIterate completely
    return true;
}
  
// Utility function to get Prime
// Level of a given Binary tree
void findPrimeLevels(struct Node* node,
                     struct Node* queue[],
                     int index, int size)
{
    // Print root node value, if Prime
    if (isPrime(queue[index]->key)) {
        cout << queue[index]->key << endl;
    }
  
    // Run while loop
    while (index < size) {
        int curr_size = size;
  
        // Run inner while loop
        while (index < curr_size) {
            struct Node* temp = queue[index];
  
            // Push left child in a queue
            if (temp->left != NULL)
                queue[size++] = temp->left;
  
            // Push right child in a queue
            if (temp->right != NULL)
                queue[size++] = temp->right;
  
            // Increament index
            index++;
        }
  
        // If condition to check, level is
        // prime or not
        if (isLevelPrime(
                queue, index, size - 1)) {
  
            // Function call to print
            // prime level
            printLevel(queue, index, size);
        }
    }
}
  
// Function to find total no of nodes
// In a given binary tree
int findSize(struct Node* node)
{
    // Base condition
    if (node == NULL)
        return 0;
  
    return 1
           + findSize(node->left)
           + findSize(node->right);
}
  
// Function to find Prime levels
// In a given binary tree
void printPrimeLevels(struct Node* node)
{
    int t_size = findSize(node);
  
    // Create queue
    struct Node* queue[t_size];
  
    // Push root node in a queue
    queue[0] = node;
  
    // Function call
    findPrimeLevels(node, queue, 0, 1);
}
  
// Driver Code
int main()
{
    /*      10 
         /    \ 
        13     11 
            /  \ 
           19    23 
          / \    / \ 
         21 29 43 15
                  /
                 7 */
  
    // Create Binary Tree as shown
  
    Node* root = newNode(10);
    root->left = newNode(13);
    root->right = newNode(11);
  
    root->right->left = newNode(19);
    root->right->right = newNode(23);
  
    root->right->left->left = newNode(21);
    root->right->left->right = newNode(29);
    root->right->right->left = newNode(43);
    root->right->right->right = newNode(15);
    root->right->right->right->left = newNode(7);
  
    // Print Prime Levels
    printPrimeLevels(root);
  
    return 0;
}

chevron_right


Output:

13 11 
19 23 
7

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.