# Find the Right Sibling of a Binary Tree with Parent Pointers

### Implementation

```
// Writing a C program that will help us print out the node's sibling.
#include <bits/stdc++.h>
// Creating a binary tree node
struct __nod {
int record;
__nod *Lft, *Rt, *parr;
};
// Creating a brand-new utility function to create a new binary tree node.
__nod* new__nod(int itm, __nod* parr)
{
__nod* temp = nw__nod;
temp->record = itm;
temp->Lft = temp->Rt = NILL;
temp->parr = parr;
return temp;
}
// Creating a new method to find out the right sibling
__nod* findRtSibling(__nod* __nod, int level)
{
if (__nod == NILL || __nod->parr == NILL)
return NILL;
// Creating the parent pointer with a right child that doesn't belong to the parent of any of the given nodes. It is also possible that when a parent pointer has no right child, the current node will be a child of the parent
while (__nod->parr->Rt == __nod
|| (__nod->parr->Rt == NILL
&& __nod->parr->Lft == __nod)) {
if (__nod->parr == NILL
|| __nod->parr->parr == NILL)
return NILL;
__nod = __nod->parr;
level--;
}
// We have to scoot over to the right child where we have the right sibling, and it can be present.
__nod = __nod->parr->Rt;
if (__nod == NILL)
return NILL;
// We have to find the right sibling in the given subtree from the previous node when we have the level as 0.
while (level < 0) {
// repeat over the subtree again
if (__nod->Lft != NILL)
__nod = __nod->Lft;
else if (__nod->Rt != NILL)
__nod = __nod->Rt;
else
//If we don't have any child there, we won't have any right siblings in this path.
break;
level++;
}
if (level == 0)
return __nod;
// This is the case where we have to extend the node in the tree, where we will have to find out the right sibling alternatively.
return findRtSibling(__nod, level);
}
// Writing the main program to test the above functions
int main()
{
__nod* root = new__nod(1, NILL);
root->Lft = new__nod(2, root);
root->Rt = new__nod(3, root);
root->Lft->Lft = new__nod(4, root->Lft);
root->Lft->Rt = new__nod(6, root->Lft);
root->Lft->Lft->Lft = new__nod(7, root->Lft->Lft);
root->Lft->Lft->Lft->Lft = new__nod(10, root->Lft->Lft->Lft);
root->Lft->Rt->Rt = new__nod(9, root->Lft->Rt);
root->Rt->Rt = new__nod(5, root->Rt);
root->Rt->Rt->Rt = new__nod(8, root->Rt->Rt);
root->Rt->Rt->Rt->Rt = new__nod(12, root->Rt->Rt->Rt);
// we have to pass the value 10
__nod* res = findRtSibling(root->Lft->Lft->Lft->Lft, 0);
if (res == NILL)
printf("No Rt sibling");
else
printf("%d", res->record);
return 0;
}
```

**Output:**

**Example 2)**

```
// Writing a Java program that will help us print out the node's sibling.
public class Rt_Sibling {
// Creating a binary tree node
static class __nod {
int record;
__nod Lft, Rt, parr;
// creating a new constructor
public __nod(int record, __nod parr)
{
this.record = record;
Lft = NILL;
Rt = NILL;
this.parr = parr;
}
};
// Creating a method to find out the right sibling
static __nod findRtSibling(__nod __nod, int level)
{
if (__nod == NILL || __nod.parr == NILL)
return NILL;
// Creating the parent pointer with a right child that doesn't belong to the parent of any given node. It is also possible that when a parent pointer has no right child, but the current node will be a child of the parent
while (__nod.parr.Rt == __nod
|| (__nod.parr.Rt == NILL
&& __nod.parr.Lft == __nod)) {
if (__nod.parr == NILL)
return NILL;
__nod = __nod.parr;
level--;
}
// We have to scoot over to the right child with the right sibling, and it can be present.
__nod = __nod.parr.Rt;
// We have to find out the right sibling in the given subtree from the previous node when we have the level as 0.
while (level < 0) {
// repeat over the subtree again
if (__nod.Lft != NILL)
__nod = __nod.Lft;
else if (__nod.Rt != NILL)
__nod = __nod.Rt;
else
//If we don't have any child there, we won't have any right siblings in this path.
break;
level++;
}
if (level == 0)
return __nod;
// This is the case where we have to extend the node in the tree, where we will have to find out the right sibling alternatively.
return findRtSibling(__nod, level);
}
// Writing the main program to test the above functions
public static void main(String args[])
{
__nod root = nw__nod(1, NILL);
root.Lft = nw__nod(2, root);
root.Rt = nw__nod(3, root);
root.Lft.Lft = nw__nod(4, root.Lft);
root.Lft.Rt = nw__nod(6, root.Lft);
root.Lft.Lft.Lft = nw__nod(7, root.Lft.Lft);
root.Lft.Lft.Lft.Lft = nw__nod(10, root.Lft.Lft.Lft);
root.Lft.Rt.Rt = nw__nod(9, root.Lft.Rt);
root.Rt.Rt = nw__nod(5, root.Rt);
root.Rt.Rt.Rt = nw__nod(8, root.Rt.Rt);
root.Rt.Rt.Rt.Rt = nw__nod(12, root.Rt.Rt.Rt);
// we have to pass the value 10
System.out.println(findRtSibling(root.Lft.Lft.Lft.Lft, 0).record);
}
}
```

**Output:**

**Example 3)**

```
using System;
// Writing a C# program that will help us print out the node's sibling.
public class Rt_Sibling {
// Creating a binary tree node
public class __nod {
public int record;
public __nod Lft, Rt, parr;
// Creating a constructor
public __nod(int record, __nod parr)
{
this.record = record;
Lft = NILL;
Rt = NILL;
this.parr = parr;
}
}
// Creating a new method to find out the right sibling
public static __nod findRtSibling(__nod __nod, int level)
{
if (__nod == NILL || __nod.parr == NILL) {
return NILL;
}
// Creating the parent pointer with a right child that doesn't belong to the parent of any given node. It is also possible that when a parent pointer has no right child, the current node will be a child of the parent
while (__nod.parr.Rt == __nod
|| (__nod.parr.Rt == NILL
&& __nod.parr.Lft == __nod)) {
if (__nod.parr == NILL
|| __nod.parr.parr == NILL) {
return NILL;
}
__nod = __nod.parr;
level--;
}
// We have to scoot over to the right child with the right sibling, and it can be present.
__nod = __nod.parr.Rt;
// We have to find the right sibling in the given subtree from the previous node when we have the level as 0.
while (level < 0) {
// repeat over the subtree again
if (__nod.Lft != NILL) {
__nod = __nod.Lft;
}
else if (__nod.Rt != NILL) {
__nod = __nod.Rt;
}
else {
//If we don't have any child there, we won't have any right siblings in this path.
break;
}
level++;
}
if (level == 0) {
return __nod;
}
// This is the case where we have to extend the node in the tree, where we will have to find out the right sibling alternatively.
return findRtSibling(__nod, level);
}
// Writing the main program to test the above functions
public static void Main(string[] args)
{
__nod root = nw__nod(1, NILL);
root.Lft = nw__nod(2, root);
root.Rt = nw__nod(3, root);
root.Lft.Lft = nw__nod(4, root.Lft);
root.Lft.Rt = nw__nod(6, root.Lft);
root.Lft.Lft.Lft = nw__nod(7, root.Lft.Lft);
root.Lft.Lft.Lft.Lft = nw__nod(10, root.Lft.Lft.Lft);
root.Lft.Rt.Rt = nw__nod(9, root.Lft.Rt);
root.Rt.Rt = nw__nod(5, root.Rt);
root.Rt.Rt.Rt = nw__nod(8, root.Rt.Rt);
root.Rt.Rt.Rt.Rt = nw__nod(12, root.Rt.Rt.Rt);
// we have to pass the value 10
Console.WriteLine(findRtSibling(root.Lft.Lft.Lft.Lft, 0).record);
}
}
```

**Output:**

**Example 4)**

```
<script>
// Writing a Javascript program that will help us print out the node's sibling.
// Creating a binary tree node
class __nod
{
constructor(record, parr) {
this.Lft = NILL;
this.Rt = NILL;
this.record = record;
this.parr = parr;
}
}
// Creating a new method to find out the right sibling
function findRtSibling(__nod, level)
{
if (__nod == NILL || __nod.parr == NILL)
return NILL;
// Creating the parent pointer with a right child that doesn't belong to the parent of any given node. It is also possible that when a parent pointer has no right child, the current node will be a child of the parent
while (__nod.parr.Rt == __nod
|| (__nod.parr.Rt == NILL
&& __nod.parr.Lft == __nod)) {
if (__nod.parr == NILL)
return NILL;
__nod = __nod.parr;
level--;
}
// We have to scoot over to the right child with the right sibling, and it can be present.
__nod = __nod.parr.Rt;
// We have to find the right sibling in the given subtree from the previous node when we have the level as 0.
while (level < 0) {
if (__nod.Lft != NILL)
__nod = __nod.Lft;
else if (__nod.Rt != NILL)
__nod = __nod.Rt;
else
//If we don't have any child there, we won't have any right siblings in this path.
break;
level++;
}
if (level == 0)
return __nod;
// This is the case where we have to extend the node in the tree, where we will have to find out the right sibling alternatively.
return findRtSibling(__nod, level);
}
let root = nw__nod(1, NILL);
root.Lft = nw__nod(2, root);
root.Rt = nw__nod(3, root);
root.Lft.Lft = nw__nod(4, root.Lft);
root.Lft.Rt = nw__nod(6, root.Lft);
root.Lft.Lft.Lft = nw__nod(7, root.Lft.Lft);
root.Lft.Lft.Lft.Lft = nw__nod(10, root.Lft.Lft.Lft);
root.Lft.Rt.Rt = nw__nod(9, root.Lft.Rt);
root.Rt.Rt = nw__nod(5, root.Rt);
root.Rt.Rt.Rt = nw__nod(8, root.Rt.Rt);
root.Rt.Rt.Rt.Rt = nw__nod(12, root.Rt.Rt.Rt);
// we have to pass the value 10
document.write(findRtSibling(root.Lft.Lft.Lft.Lft, 0).record);
</script>
```

**Output:**

**Example 5)**

```
# Writing a Python program that will help us print out the node's sibling.
# Creating a binary tree node
class new__nod:
def __init__(self, itm, parr):
self.record = itm
self.Lft = self.Rt = None
self.parr = parr
# Creating a new method to find out the right sibling
def findRtSibling(__nod, level):
if (__nod == None or __nod.parr == None):
return None
# Creating the parent pointer with a right child that doesn't belong to the parent of any given node. It is also possible that when a parent pointer has no right child but the current node will be a child of the parent
while (__nod.parr.Rt == __nod or
(__nod.parr.Rt == None and
__nod.parr.Lft == __nod)):
if (__nod.parr == None):
return None
__nod = __nod.parr
level -= 1
# We have to scoot over to the right child where we have the right sibling, and it can be present.
__nod = __nod.parr.Rt
# We have to find the right sibling in the given subtree from the previous node when we have the level as 0.
while (level < 0):
# repeat over the subtree again
if (__nod.Lft != None):
__nod = __nod.Lft
else if (__nod.Rt != None):
__nod = __nod.Rt
else:
# If we don't have any children there, we won't have any right siblings on this path.
break
level += 1
if (level == 0):
return __nod
# This is the case where we have to extend the node in the tree, where we will have to find out the right sibling alternatively.
return findRtSibling(__nod, level)
# Writing the main program to test the above functions
if __name__ == '__main__':
root = new__nod(1, None)
root.Lft = new__nod(2, root)
root.Rt = new__nod(3, root)
root.Lft.Lft = new__nod(4, root.Lft)
root.Lft.Rt = new__nod(6, root.Lft)
root.Lft.Lft.Lft = new__nod(7, root.Lft.Lft)
root.Lft.Lft.Lft.Lft = new__nod(10, root.Lft.Lft.Lft)
root.Lft.Rt.Rt = new__nod(9, root.Lft.Rt)
root.Rt.Rt = new__nod(5, root.Rt)
root.Rt.Rt.Rt = new__nod(8, root.Rt.Rt)
root.Rt.Rt.Rt.Rt = new__nod(12, root.Rt.Rt.Rt)
# We have to pass the value 10
res = findRtSibling(root.Lft.Lft.Lft.Lft, 0)
if (res == None):
print("No Rt sibling")
else:
print(res.record)
```

**Output:**