Construct a Prime Binary Tree from given non-cyclic graph of N indices


In the area of programming and data structures, binary trees are widely used to store and retrieve data efficiently. In this article, we will explore the concept of constructing a prime binary tree from a given non-cyclic graph consisting of N indices using C++ code. The Binary tree can be constructed from the non-cyclic graph and the types of this graph are tree, directed acyclic graph and some more. A prime tree comes under the division of binary tree which returns the prime number by appending the two consecutive edges of the graph.

Construct a Prime Binary Tree from a given non-cyclic graph

A prime binary tree is an interesting variation of a regular binary search tree (BST). In a BST, each node has two children: one on its left with a lesser value and one on its right with a greater value. Prime binary trees employ this concept but come with an additional constraint: every node in the left subtree must be smaller than any node in the right subtree.


In the above binary tree, the root node is chosen as the node 0 and it has two child nodes as the left child node 1 and the right child node 2. The left child is further has two child with node 3 and node 4. As the prime binary tree constructed from it is 0 2 5.

Approach 1: C++ code to construct a prime binary tree from a given noncyclic graph of N indices

We Begin by including necessary standard C++ libraries such as iostream. Followed by creating classes for nodes and graphs to represent the structure efficiently.


  • Step 1 − Create a class Node with three members − index, leftChild, and rightChild.

  • Step 2 − Create a function createNode that takes an integer argument and returns a new node with the given index and null left and right children.

  • Step 3 − isprime() function is created to check whether the nodes are of prime numbers.

  • Step 4 − Create a function printTree that takes a pointer to the root node of a binary tree and prints the pre-order traversal of the tree.

  • Step 5 − Create a function constructPrimeBinaryTree that takes three arguments: a reference to the adjacency list of the graph, a reference to the visited array, and a pointer to the current node. The function recursively constructs the binary tree with prime numbers using an adjacency list and visited array. For each neighbor of the current node that has not been visited yet and whose index plus the current node’s index is prime, create a new node with the neighbor’s index and add it as the left child if the current node’s left child is null or as the right child otherwise. Then call constructPrimeBinaryTree() recursively on the new node.

  • Step 6 − In main −

    • Initialize variable n to 6 which is the number of nodes in the tree.

    • Initialize variable adjList to a vector of vectors that represents the adjacency list of the graph.

    • Add neighbors to each element of adjList.

    • Initialize variable visited to a vector that represents whether a node has been visited or not.

    • Set visited[0] to true since node 0 is the root.

    • Create variable root which is a pointer to the root node created using function createNode(0).

    • Call function constructPrimeBinaryTree(adjList, visited, root) to construct the binary tree with prime numbers using adjacency list and visited array.

    • Call function printTree(root) to print the tree in pre-order traversal.


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Node {
      int index;
      Node* leftChild;
      Node* rightChild;

      Node(int idx) : index(idx), leftChild(nullptr), rightChild(nullptr) {}

Node* createNode(int idx) {
   return new Node(idx);

bool isPrime(int n) {
   if (n <= 1)
      return false;
   for (int i = 2; i * i <= n; i++)
      if (n % i == 0)
         return false;
      return true;

void printTree(Node* root) {
   if (root == nullptr)

   cout << root->index << " ";

void constructPrimeBinaryTree(vector<vector<int>>& adjList, vector<bool>& visited, Node* curr) {
   for (int neighbor : adjList[curr->index]) {
      if (!visited[neighbor] && isPrime(curr->index + neighbor)) {
         visited[neighbor] = true;
         Node* newNode = createNode(neighbor);
         if (curr->leftChild == nullptr)
            curr->leftChild = newNode;
            curr->rightChild = newNode;
            constructPrimeBinaryTree(adjList, visited, newNode);
int main() {
   int n = 6;

   vector<vector<int>> adjList(n);

   vector<bool> visited(n, false);
   visited[0] = true;
   Node* root = createNode(0);

   constructPrimeBinaryTree(adjList, visited, root);


   return 0;


0 2 5


In this article, we explored how to construct a prime binary tree from a given non-cyclic graph of N indices using C++ code. By following the step-by-step approach mentioned above, programmers can efficiently implement this data structure in various applications. The code returns the prime binary tree using the recursion function.

Updated on: 25-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started