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;
vector<int> adj[N+1];
// 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
adj[1].push_back(2);
adj[1].push_back(3);
adj[2].push_back(4);
adj[2].push_back(5);
adj[3].push_back(6);
adj[3].push_back(7);
int root = 1;
PrintSiblings(root, N, E, adj);
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,
int E, List<int> []adj)
{
// 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");
foreach (int s in adj[__nod])
{
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++)
adj[i] = new List<int>();
// 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
adj[1].Add(2);
adj[1].Add(3);
adj[2].Add(4);
adj[2].Add(5);
adj[3].Add(6);
adj[3].Add(7);
int root = 1;
PrintSiblings(root, N, E, adj);
}
}
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,
int E, Vector<Integer> adj[])
{
// 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
Queue<Integer> q = new LinkedList<>();
q.add(root);
q.add(-1);
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())
q.add(-1);
cont;
}
//Creating the node and also customizing its right sibling
System.out.print(__nod + " " +
q.peek() + "\n");
for (Integer s : adj[__nod])
{
if (!vis[s])
{
vis[s] = true;
q.add(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;
Vector<Integer> []adj = new Vector[N + 1];
for(int i = 0; i < N + 1; i++)
adj[i] = new Vector<Integer>();
// 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
adj[1].add(2);
adj[1].add(3);
adj[2].add(4);
adj[2].add(5);
adj[3].add(6);
adj[3].add(7);
int root = 1;
PrintSiblings(root, N, E, adj);
}
}
Output:

Example 4)
# Writing a Python program to print out the right siblings of all the __nods that are present in a tree
def PrintSiblings(root, N, E, adj):
# 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]))
for s in adj[__nod]:
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
adj[1].append(2)
adj[1].append(3)
adj[2].append(4)
adj[2].append(5)
adj[3].append(6)
adj[3].append(7)
root = 1
PrintSiblings(root, N, E, adj)
Output:

Example 5)
<script>
// Writing a Javascript program to print out the right siblings of all the __nods that are present in a tree
function PrintSiblings(root, N, E, adj)
{
// 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++)
{
if (!vis[adj[__nod][s]])
{
vis[adj[__nod][s]] = true;
q.puss(adj[__nod][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++)
adj[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
adj[1].push(2);
adj[1].push(3);
adj[2].push(4);
adj[2].push(5);
adj[3].push(6);
adj[3].push(7);
let root = 1;
PrintSiblings(root, N, E, adj);
</script>
Output:
