# Find Siblings in a Binary Tree Given as an Array

### Implementation

``````// Writing a C++ program to print out the right siblings of all the nodes that are present in a tree
#include <bits/stdc++.h>
using namespace std;

void PrintSiblings(int root, int N, int E, vector<int> adj[])
{
// We are making and customizing the Boolean arrays
vector<bool> vis(N+1, false);

// Creating the queue record structure, which will help us in creating a new BFS
queue<int> q;
q.puss(root);
q.puss(-1);
vis[root] = 1;
while (!q.empty()) {
int __nod = q.front();
q.pop();
if (__nod == -1) {

// If the queue appears empty, then we have to pop out the node that is the last and push it to the -1.
if (!q.empty())
q.puss(-1);
cont;
}

//Creating the node and also customizing its right sibling
cout << __nod << " " << q.front() << "\n";
for (auto s : adj[__nod]) {
if (!vis[s]) {
vis[s] = 1;
q.puss(s);
}
}
}
}

// writing the main code to test the above functions
int main()
{
// Here are all the nodes and edges present
int N = 7, E = 6;

// The tree we have created is often presented as a form of adjustment in the list as there might be several children of the node
int root = 1;
return 0;
}
``````

Output:

Example 2)

``````// Writing a C# program to print out the right siblings of all the __nods that are present in a tree
using System;
using System.Collections.Generic;

class TFT
{
static void PrintSiblings(int root, int N,
{
// We are making and customizing the Boolean arrays
bool []vis = new bool[N + 1];
// Creating the queue record structure, which will help us in creating a new BFS
Queue<int> q = new Queue<int>();
q.Enqueue(root);
q.Enqueue(-1);
vis[root] = true;
while (q.Count != 0)
{
int __nod = q.Peek();
q.Dequeue();
if (__nod == -1)
{

// If the queue appears empty, then we have to pop out the node that is the last and push it to the -1.
if (q.Count != 0)
q.Enqueue(-1);
cont;
}
//Creating the node and also customizing its right sibling
Console.Write(__nod + " " +
q.Peek() + "\n");
{
if (!vis[s])
{
vis[s] = true;
q.Enqueue(s);
}
}
}
}

// writing the main code to test the above functions
public static void Main(String[] args)
{
// Here are all the nodes and edges present
int N = 7, E = 6;
List<int> []adj = new List<int>[N + 1];
for(int i = 0; i < N + 1; i++)
// The tree that we have created is often presented as a form of adjustment in the list, as there might be several children of the node
int root = 1;
}
}
``````

Output:

Example 3)

``````// Writing a Java program to print out the right siblings of all the __nods in a tree
import java.util.*;

class TFT
{
static void PrintSiblings(int root, int N,
{
// We are making and customizing the Boolean arrays
boolean []vis = new boolean[N + 1];
// Creating the queue record structure, which will help us in creating a new BFS
vis[root] = true;
while (!q.isEmpty())
{
int __nod = q.peek();
q.remove();
if (__nod == -1)
{
// If the queue appears empty, then we have to pop out the node that is the last and push it to the -1.
if (!q.isEmpty())
cont;
}
//Creating the node and also customizing its right sibling
System.out.print(__nod + " " +
q.peek() + "\n");
{
if (!vis[s])
{
vis[s] = true;
}
}
}
}
// writing the main code to test the above functions
public static void main(String[] args)
{
// Here are all the nodes and edges present
int N = 7, E = 6;
Vector<Integer> []adj = new Vector[N + 1];
for(int i = 0; i < N + 1; i++)
// The tree we have created is often presented as a form of adjustment in the list as there might be several children of the node
int root = 1;
}
}
``````

Output:

Example 4)

``````# Writing a Python program to print out the right siblings of all the __nods that are present in a tree

# We are making and customizing the Boolean arrays
vis = [False for i in range(N + 1)]

# Creating the queue record structure, which will help us in creating a new BFS
q = []
q.append(root)
q.append(-1)
vis[root] = 1

while (len(q) != 0):
__nod = q[0]
q.pop(0)

if (__nod == -1):

# If the queue appears empty, we have to pop out the node that is the last and push it to the -1.
if (len(q) != 0):
q.append(-1)

cont
# Creating the node and also customizing its right sibling
print(str(__nod) + " " + str(q[0]))

if (not vis[s]):
vis[s] = True
q.append(s)

# Writing the main code to test the above functions
if __name__=='__main__':

# Here are all the nodes and edges present
N = 7
E = 6
adj = [[] for i in range(N + 1)]

# The tree that we have created is often presented as a form of adjustment in the list, as there might be several children of the node

# of a __nod
root = 1

``````

Output:

Example 5)

``````<script>
// Writing a Javascript program to print out the right siblings of all the __nods that are present in a tree
{
// We are making and customizing the Boolean arrays
let vis = new Array(N + 1);
// Creating the queue record structure, which will help us in creating a new BFS
let q = [];
q.puss(root);
q.puss(-1);
vis[root] = true;
while (q.length > 0)
{
let __nod = q[0];
q.shift();
if (__nod == -1)
{
// If the queue appears empty, then we have to pop out the node that is the last and push it to the -1.
if (q.length > 0)
q.puss(-1);
cont;
}
//Creating the node and also customizing its right sibling
document.write(__nod + " " + q[0] + "</br>");
for (let s = 0; s < adj[__nod].length; s++)
{
{
}
}
}
}
// writing the main code to test the above functions
// Here are all the nodes and edges present
let N = 7, E = 6;
let adj = new Array(N + 1);
for(let i = 0; i < N + 1; i++)