# Print cousins of given node in a binary tree

Here given code implementation process.

``````/*
C Program
Print cousins of given node in a binary tree
*/
#include <stdio.h>

#include <stdlib.h>

//Binary Tree node
struct Node
{
int data;
struct Node *left, *right;
};
//This is creating a binary tree node and return this
struct Node *get_node(int data)
{
// Create dynamic node
struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
if (new_node != NULL)
{
//Set data and pointer values
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return new_node;
}
//Recursive function
//Display Inorder view of binary tree
void inorder(struct Node *node)
{
if (node)
{
inorder(node->left);
//Print node value
printf("  %d", node->data);
inorder(node->right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
struct Node *find_node_and_height(struct Node *root, int *height, int level, int key)
{
if (root == NULL)
{
//When get empty node
return NULL;
}
else if ((root->left != NULL && root->left->data == key) || (root->right != NULL && root->right->data == key))
{
// When given key exists in binary tree
// Get node height
*height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
struct Node *result = NULL;
result = find_node_and_height(root->left, height, level + 1, key);
if (result != NULL)
{
// If get a parent node of given key
return result;
}
return find_node_and_height(root->right, height, level + 1, key);
}
}
//Print cousin nodes in given level
void print_cousin_node(struct Node *root, int print_level, struct Node *parent, int level)
{
if (root == NULL || parent == root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
printf("  %d", root->data);
return;
}
//Recursively execute left and right subtree
print_cousin_node(root->left, print_level, parent, level + 1);
print_cousin_node(root->right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
void find_cousin_node(struct Node *root, int key)
{
if (root->data == key)
{
//There are no cousins of root node
printf("Cousin nodes of %d is : None \n", key);
return;
}
int height = -1;
//First get height and parent node of given key
struct Node *parent = find_node_and_height(root, &height, 0, key);
if (parent != NULL)
{
printf("Cousin nodes of %d is : ", key);
print_cousin_node(root, height, parent, 0);
printf("\n");
}
else
{
printf("Given key node [%d] is not exist  \n", key);
}
}
int main()
{
struct Node *root = NULL;
/*
Construct Binary Tree
-----------------------
1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11

*/
root = get_node(1);
root->left = get_node(6);
root->left->left = get_node(2);
root->right = get_node(8);
root->right->right = get_node(5);
root->right->left = get_node(7);
root->left->right = get_node(3);
root->left->left->left = get_node(9);
root->right->right->right = get_node(11);
//Display tree elements
printf("\nBinary Tree Nodes : ");
inorder(root);
printf("\n");
//Test Case
find_cousin_node(root, 2);
find_cousin_node(root, 1);
find_cousin_node(root, 5);
find_cousin_node(root, 4);
find_cousin_node(root, 9);
return 0;
}``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :   7  5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :   2  3
Given key node [4] is not exist
Cousin nodes of 9 is :   11``````
``````/*
Java Program
Print cousins of given node in a binary tree
*/
//Node of Binary Tree
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
//Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
public Node root;
public Node parent;
public int height;
public BinaryTree()
{
//Set initial tree root to null
this.root = null;
this.parent = null;
this.height = 0;
}
//Display Inorder view of binary tree
public void inorder(Node node)
{
if (node != null)
{
// Executing left subtree
inorder(node.left);
// Print node value
System.out.print("  " + node.data);
// Executing right subtree
inorder(node.right);
}
}

//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
public Node find_node_and_height(Node root, int level, int key)
{
if (root == null)
{
//When get empty node
return null;
}
else if ((root.left != null && root.left.data == key) || (root.right != null && root.right.data == key))
{
// When given key exists in binary tree
// Get node height
this.height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
Node result = null;

result = find_node_and_height(root.left, level + 1, key);

if (result != null)
{
// If get a parent node of given key
return result;
}
return find_node_and_height(root.right, level + 1, key);
}
}
//Print cousin nodes in given level
public void print_cousin_node(Node root, int print_level, Node parent, int level)
{
if (root == null || parent == root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
System.out.print(" " + root.data + "");
return;
}
//Recursively execute left and right subtree
print_cousin_node(root.left, print_level, parent, level + 1);
print_cousin_node(root.right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
public void find_cousin_node(int key)
{
this.parent = null;
if (this.root.data == key)
{
//There are no cousins of root node
System.out.print("Cousin nodes of " + key + " is : None \n");
return;
}
this.height = 0;
//First get height and parent node of given key
Node parent = find_node_and_height(this.root, 0, key);
if (parent != null)
{
System.out.print("Cousin nodes of " + key + " is : ");
print_cousin_node(root, height, parent, 0);
System.out.print("\n");
}
else
{
System.out.print("Given key node [" + key + "] is not exist \n");
}
}

public static void main(String[] args)
{
//Make object of Binary Tree
BinaryTree obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
obj.root = new Node(1);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(5);
obj.root.right.left = new Node(7);
obj.root.left.right = new Node(3);
obj.root.left.left.left = new Node(9);
obj.root.right.right.right = new Node(11);

//Display tree elements
System.out.print("\nBinary Tree Nodes : ");
obj.inorder(obj.root);
System.out.print("\n");

//Test Case
obj.find_cousin_node(2);
obj.find_cousin_node(1);
obj.find_cousin_node(5);
obj.find_cousin_node(4);
obj.find_cousin_node(9);
}
}``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11``````
``````//Include header file
#include <iostream>

using namespace std;

/*
C++ Program
Print cousins of given node in a binary tree
*/

//Node of Binary Tree
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int data)
{
//Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
public:
Node *root;
Node *parent;
int height;
BinaryTree()
{
//Set initial tree root to null
this->root = NULL;
this->parent = NULL;
this->height = 0;
}
//Display Inorder view of binary tree
void inorder(Node *node)
{
if (node != NULL)
{
// Executing left subtree
this->inorder(node->left);
// Print node value
cout << "  " << node->data;
// Executing right subtree
this->inorder(node->right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
Node *find_node_and_height(Node *root, int level, int key)
{
if (root == NULL)
{
//When get empty node
return NULL;
}
else if ((root->left != NULL && root->left->data == key) || (root->right != NULL && root->right->data == key))
{
// When given key exists in binary tree
// Get node height
this->height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
Node *result = NULL;
result = this->find_node_and_height(root->left, level + 1, key);
if (result != NULL)
{
// If get a parent node of given key
return result;
}
return this->find_node_and_height(root->right, level + 1, key);
}
}
//Print cousin nodes in given level
void print_cousin_node(Node *root, int print_level, Node *parent, int level)
{
if (root == NULL || parent == root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
cout << " " << root->data << "";
return;
}
//Recursively execute left and right subtree
this->print_cousin_node(root->left, print_level, parent, level + 1);
this->print_cousin_node(root->right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
void find_cousin_node(int key)
{
this->parent = NULL;
if (this->root->data == key)
{
//There are no cousins of root node
cout << "Cousin nodes of " << key << " is : None \n";
return;
}
this->height = 0;
//First get height and parent node of given key
Node *parent = this->find_node_and_height(this->root, 0, key);
if (parent != NULL)
{
cout << "Cousin nodes of " << key << " is : ";
this->print_cousin_node(this->root, this->height, parent, 0);
cout << "\n";
}
else
{
cout << "Given key node [" << key << "] is not exist \n";
}
}
};
int main()
{
//Make object of Binary Tree
BinaryTree obj = BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
obj.root = new Node(1);
obj.root->left = new Node(6);
obj.root->left->left = new Node(2);
obj.root->right = new Node(8);
obj.root->right->right = new Node(5);
obj.root->right->left = new Node(7);
obj.root->left->right = new Node(3);
obj.root->left->left->left = new Node(9);
obj.root->right->right->right = new Node(11);
//Display tree elements
cout << "\nBinary Tree Nodes : ";
obj.inorder(obj.root);
cout << "\n";
//Test Case
obj.find_cousin_node(2);
obj.find_cousin_node(1);
obj.find_cousin_node(5);
obj.find_cousin_node(4);
obj.find_cousin_node(9);
return 0;
}``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11``````
``````//Include namespace system
using System;

/*
C# Program
Print cousins of given node in a binary tree
*/

//Node of Binary Tree
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
//Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
public Node root;
public Node parent;
public int height;
public BinaryTree()
{
//Set initial tree root to null
this.root = null;
this.parent = null;
this.height = 0;
}
//Display Inorder view of binary tree
public void inorder(Node node)
{
if (node != null)
{
// Executing left subtree
inorder(node.left);
// Print node value
Console.Write("  " + node.data);
// Executing right subtree
inorder(node.right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
public Node find_node_and_height(Node root, int level, int key)
{
if (root == null)
{
//When get empty node
return null;
}
else if ((root.left != null && root.left.data == key) || (root.right != null && root.right.data == key))
{
// When given key exists in binary tree
// Get node height
this.height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
Node result = null;
result = find_node_and_height(root.left, level + 1, key);
if (result != null)
{
// If get a parent node of given key
return result;
}
return find_node_and_height(root.right, level + 1, key);
}
}
//Print cousin nodes in given level
public void print_cousin_node(Node root, int print_level, Node parent, int level)
{
if (root == null || parent == root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
Console.Write(" " + root.data + "");
return;
}
//Recursively execute left and right subtree
print_cousin_node(root.left, print_level, parent, level + 1);
print_cousin_node(root.right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
public void find_cousin_node(int key)
{
this.parent = null;
if (this.root.data == key)
{
//There are no cousins of root node
Console.Write("Cousin nodes of " + key + " is : None \n");
return;
}
this.height = 0;
//First get height and parent node of given key
Node parent = find_node_and_height(this.root, 0, key);
if (parent != null)
{
Console.Write("Cousin nodes of " + key + " is : ");
print_cousin_node(root, height, parent, 0);
Console.Write("\n");
}
else
{
Console.Write("Given key node [" + key + "] is not exist \n");
}
}
public static void Main(String[] args)
{
//Make object of Binary Tree
BinaryTree obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
obj.root = new Node(1);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(5);
obj.root.right.left = new Node(7);
obj.root.left.right = new Node(3);
obj.root.left.left.left = new Node(9);
obj.root.right.right.right = new Node(11);
//Display tree elements
Console.Write("\nBinary Tree Nodes : ");
obj.inorder(obj.root);
Console.Write("\n");
//Test Case
obj.find_cousin_node(2);
obj.find_cousin_node(1);
obj.find_cousin_node(5);
obj.find_cousin_node(4);
obj.find_cousin_node(9);
}
}``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11``````
``````<?php
/*
Php Program
Print cousins of given node in a binary tree
*/

//Node of Binary Tree
class Node
{
public \$data;
public \$left;
public \$right;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->left = null;
\$this->right = null;
}
}
class BinaryTree
{
public \$root;
public \$parent;
public \$height;

function __construct()
{
//Set initial tree root to null
\$this->root = null;
\$this->parent = null;
\$this->height = 0;
}
//Display Inorder view of binary tree
public	function inorder(\$node)
{
if (\$node != null)
{
// Executing left subtree
\$this->inorder(\$node->left);
// Print node value
echo "  ". \$node->data;
// Executing right subtree
\$this->inorder(\$node->right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
public	function find_node_and_height(\$root, \$level, \$key)
{
if (\$root == null)
{
//When get empty node
return null;
}
else if ((\$root->left != null && \$root->left->data == \$key) || (\$root->right != null && \$root->right->data == \$key))
{
// When given key exists in binary tree
// Get node height
\$this->height = \$level + 1;
//return parent node
return \$root;
}
else
{
//Used to detect key node in left subtree
\$result = null;
\$result = \$this->find_node_and_height(\$root->left, \$level + 1, \$key);
if (\$result != null)
{
// If get a parent node of given key
return \$result;
}
return \$this->find_node_and_height(\$root->right, \$level + 1, \$key);
}
}
//Print cousin nodes in given level
public	function print_cousin_node(\$root, \$print_level, \$parent, \$level)
{
if (\$root == null || \$parent == \$root || \$level > \$print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (\$print_level == \$level)
{
//Display cousin nodes
echo " ". \$root->data ."";
return;
}
//Recursively execute left and right subtree
\$this->print_cousin_node(\$root->left, \$print_level, \$parent, \$level + 1);
\$this->print_cousin_node(\$root->right, \$print_level, \$parent, \$level + 1);
}
//This are handling the request of finding cousin node of given key
public	function find_cousin_node(\$key)
{
\$this->parent = null;
if (\$this->root->data == \$key)
{
//There are no cousins of root node
echo "Cousin nodes of ". \$key ." is : None \n";
return;
}
\$this->height = 0;
//First get height and parent node of given key
\$parent = \$this->find_node_and_height(\$this->root, 0, \$key);
if (\$parent != null)
{
echo "Cousin nodes of ". \$key ." is : ";
\$this->print_cousin_node(\$this->root, \$this->height, \$parent, 0);
echo "\n";
}
else
{
echo "Given key node [". \$key ."] is not exist \n";
}
}
}

function main()
{
//Make object of Binary Tree
\$obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
\$obj->root = new Node(1);
\$obj->root->left = new Node(6);
\$obj->root->left->left = new Node(2);
\$obj->root->right = new Node(8);
\$obj->root->right->right = new Node(5);
\$obj->root->right->left = new Node(7);
\$obj->root->left->right = new Node(3);
\$obj->root->left->left->left = new Node(9);
\$obj->root->right->right->right = new Node(11);
//Display tree elements
echo "\nBinary Tree Nodes : ";
\$obj->inorder(\$obj->root);
echo "\n";
//Test Case
\$obj->find_cousin_node(2);
\$obj->find_cousin_node(1);
\$obj->find_cousin_node(5);
\$obj->find_cousin_node(4);
\$obj->find_cousin_node(9);
}
main();``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11``````
``````/*
Node Js Program
Print cousins of given node in a binary tree
*/

//Node of Binary Tree
class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
//Set initial tree root to null
this.root = null;
this.parent = null;
this.height = 0;
}
//Display Inorder view of binary tree
inorder(node)
{
if (node != null)
{
// Executing left subtree
this.inorder(node.left);
// Print node value
process.stdout.write("  " + node.data);
// Executing right subtree
this.inorder(node.right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
find_node_and_height(root, level, key)
{
if (root == null)
{
//When get empty node
return null;
}
else if ((root.left != null && root.left.data == key) || (root.right != null && root.right.data == key))
{
// When given key exists in binary tree
// Get node height
this.height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
var result = null;
result = this.find_node_and_height(root.left, level + 1, key);
if (result != null)
{
// If get a parent node of given key
return result;
}
return this.find_node_and_height(root.right, level + 1, key);
}
}
//Print cousin nodes in given level
print_cousin_node(root, print_level, parent, level)
{
if (root == null || parent == root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
process.stdout.write(" " + root.data + "");
return;
}
//Recursively execute left and right subtree
this.print_cousin_node(root.left, print_level, parent, level + 1);
this.print_cousin_node(root.right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
find_cousin_node(key)
{
this.parent = null;
if (this.root.data == key)
{
//There are no cousins of root node
process.stdout.write("Cousin nodes of " + key + " is : None \n");
return;
}
this.height = 0;
//First get height and parent node of given key
var parent = this.find_node_and_height(this.root, 0, key);
if (parent != null)
{
process.stdout.write("Cousin nodes of " + key + " is : ");
this.print_cousin_node(this.root, this.height, parent, 0);
process.stdout.write("\n");
}
else
{
process.stdout.write("Given key node [" + key + "] is not exist \n");
}
}
}

function main()
{
//Make object of Binary Tree
var obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
obj.root = new Node(1);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(5);
obj.root.right.left = new Node(7);
obj.root.left.right = new Node(3);
obj.root.left.left.left = new Node(9);
obj.root.right.right.right = new Node(11);
//Display tree elements
process.stdout.write("\nBinary Tree Nodes : ");
obj.inorder(obj.root);
process.stdout.write("\n");
//Test Case
obj.find_cousin_node(2);
obj.find_cousin_node(1);
obj.find_cousin_node(5);
obj.find_cousin_node(4);
obj.find_cousin_node(9);
}
main();``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11``````
``````#   Python 3 Program
#   Print cousins of given node in a binary tree

# Node of Binary Tree
class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None

class BinaryTree :

def __init__(self) :
# Set initial tree root to null
self.root = None
self.parent = None
self.height = 0

# Display Inorder view of binary tree
def inorder(self, node) :
if (node != None) :
#  Executing left subtree
self.inorder(node.left)
#  Print node value
print("  ", node.data, end = "")
#  Executing right subtree
self.inorder(node.right)

# Check whether given key node are exist in binary tree or not.
#  If key exists then return its parent node and calculate the height of key node
def find_node_and_height(self, root, level, key) :
if (root == None) :
# When get empty node
return None

elif((root.left != None and root.left.data == key) or(root.right != None and root.right.data == key)) :
#  When given key exists in binary tree
#  Get node height
self.height = level + 1
# return parent node
return root
else :
# Used to detect key node in left subtree
result = None
result = self.find_node_and_height(root.left, level + 1, key)
if (result != None) :
#  If get a parent node of given key
return result

return self.find_node_and_height(root.right, level + 1, key)

# Print cousin nodes in given level
def print_cousin_node(self, root, print_level, parent, level) :
if (root == None or parent == root or level > print_level) :
#  When node is null
#  or when root node is parent of key
#  or when level exceed print level
return

if (print_level == level) :
# Display cousin nodes
print(" ", root.data ,"", end = "")
return

# Recursively execute left and right subtree
self.print_cousin_node(root.left, print_level, parent, level + 1)
self.print_cousin_node(root.right, print_level, parent, level + 1)

# This are handling the request of finding cousin node of given key
def find_cousin_node(self, key) :
self.parent = None
if (self.root.data == key) :
# There are no cousins of root node
print("Cousin nodes of ", key ," is : None \n", end = "")
return

self.height = 0
# First get height and parent node of given key
parent = self.find_node_and_height(self.root, 0, key)
if (parent != None) :
print("Cousin nodes of ", key ," is : ", end = "")
self.print_cousin_node(self.root, self.height, parent, 0)
print("\n", end = "")
else :
print("Given key node [", key ,"] is not exist \n", end = "")

def main() :
# Make object of Binary Tree
obj = BinaryTree()
#
#         Construct Binary Tree
#         -----------------------
#                1
#              /   \
#             6     8
#            / \   / \
#           2   3 7   5
#          /           \
#         9             11
#

obj.root = Node(1)
obj.root.left = Node(6)
obj.root.left.left = Node(2)
obj.root.right = Node(8)
obj.root.right.right = Node(5)
obj.root.right.left = Node(7)
obj.root.left.right = Node(3)
obj.root.left.left.left = Node(9)
obj.root.right.right.right = Node(11)
# Display tree elements
print("\nBinary Tree Nodes : ", end = "")
obj.inorder(obj.root)
print("\n", end = "")
# Test Case
obj.find_cousin_node(2)
obj.find_cousin_node(1)
obj.find_cousin_node(5)
obj.find_cousin_node(4)
obj.find_cousin_node(9)

if __name__ == "__main__": main()``````

#### Output

``````Binary Tree Nodes :    9   2   6   3   1   7   8   5   11
Cousin nodes of  2  is :   7   5
Cousin nodes of  1  is : None
Cousin nodes of  5  is :   2   3
Given key node [ 4 ] is not exist
Cousin nodes of  9  is :   11``````
``````#   Ruby Program
#   Print cousins of given node in a binary tree

# Node of Binary Tree
class Node

# Define the accessor and reader of class Node
attr_accessor :data, :left, :right

def initialize(data)
# Set node value
self.data = data
self.left = nil
self.right = nil
end
end
class BinaryTree

# Define the accessor and reader of class BinaryTree
attr_accessor :root, :parent, :height

def initialize()

# Set initial tree root to null
self.root = nil
self.parent = nil
self.height = 0
end
# Display Inorder view of binary tree
def inorder(node)

if (node != nil)

#  Executing left subtree
self.inorder(node.left)
#  Print node value
print("  ", node.data)
#  Executing right subtree
self.inorder(node.right)
end
end
# Check whether given key node are exist in binary tree or not.
#  If key exists then return its parent node and calculate the height of key node
def find_node_and_height(root, level, key)

if (root == nil)

# When get empty node
return nil
elsif((root.left != nil && root.left.data == key) || (root.right != nil && root.right.data == key))

#  When given key exists in binary tree
#  Get node height
self.height = level + 1
# return parent node
return root
else

# Used to detect key node in left subtree
result = nil
result = self.find_node_and_height(root.left, level + 1, key)
if (result != nil)

#  If get a parent node of given key
return result
end
return self.find_node_and_height(root.right, level + 1, key)
end
end
# Print cousin nodes in given level
def print_cousin_node(root, print_level, parent, level)

if (root == nil || parent == root || level > print_level)

#  When node is null
#  or when root node is parent of key
#  or when level exceed print level
return
end
if (print_level == level)

# Display cousin nodes
print(" ", root.data ,"")
return
end
# Recursively execute left and right subtree
self.print_cousin_node(root.left, print_level, parent, level + 1)
self.print_cousin_node(root.right, print_level, parent, level + 1)
end
# This are handling the request of finding cousin node of given key
def find_cousin_node(key)

self.parent = nil
if (self.root.data == key)

# There are no cousins of root node
print("Cousin nodes of ", key ," is : None \n")
return
end
self.height = 0
# First get height and parent node of given key
parent = self.find_node_and_height(self.root, 0, key)
if (parent != nil)

print("Cousin nodes of ", key ," is : ")
self.print_cousin_node(@root, @height, parent, 0)
print("\n")
else

print("Given key node [", key ,"] is not exist \n")
end
end
end
def main()

# Make object of Binary Tree
obj = BinaryTree.new()
#
#         Construct Binary Tree
#         -----------------------
#                1
#              /   \
#             6     8
#            / \   / \
#           2   3 7   5
#          /           \
#         9             11
#

obj.root = Node.new(1)
obj.root.left = Node.new(6)
obj.root.left.left = Node.new(2)
obj.root.right = Node.new(8)
obj.root.right.right = Node.new(5)
obj.root.right.left = Node.new(7)
obj.root.left.right = Node.new(3)
obj.root.left.left.left = Node.new(9)
obj.root.right.right.right = Node.new(11)
# Display tree elements
print("\nBinary Tree Nodes : ")
obj.inorder(obj.root)
print("\n")
# Test Case
obj.find_cousin_node(2)
obj.find_cousin_node(1)
obj.find_cousin_node(5)
obj.find_cousin_node(4)
obj.find_cousin_node(9)
end
main()``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11
``````
``````/*
Scala Program
Print cousins of given node in a binary tree
*/
//Node of Binary Tree
class Node(var data: Int,
var left: Node,
var right: Node)
{
def this(data: Int)
{
this(data, null, null);
}
}
class BinaryTree(var root: Node,
var parent: Node,
var height: Int)
{
def this()
{
this(null, null, 0);
}
//Display Inorder view of binary tree
def inorder(node: Node): Unit = {
if (node != null)
{
// Executing left subtree
inorder(node.left);
// Print node value
print("  " + node.data);
// Executing right subtree
inorder(node.right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
def find_node_and_height(root: Node, level: Int, key: Int): Node = {
if (root == null)
{
//When get empty node
return null;
}
else if ((root.left != null && root.left.data == key) || (root.right != null && root.right.data == key))
{
// When given key exists in binary tree
// Get node height
this.height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
var result: Node = null;
result = find_node_and_height(root.left, level + 1, key);
if (result != null)
{
// If get a parent node of given key
return result;
}
return find_node_and_height(root.right, level + 1, key);
}
}
//Print cousin nodes in given level
def print_cousin_node(root: Node, print_level: Int, parent: Node, level: Int): Unit = {
if (root == null || parent == root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
print(" " + root.data + "");
return;
}
//Recursively execute left and right subtree
print_cousin_node(root.left, print_level, parent, level + 1);
print_cousin_node(root.right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
def find_cousin_node(key: Int): Unit = {
this.parent = null;
if (this.root.data == key)
{
//There are no cousins of root node
print("Cousin nodes of " + key + " is : None \n");
return;
}
this.height = 0;
//First get height and parent node of given key
var parent: Node = find_node_and_height(this.root, 0, key);
if (parent != null)
{
print("Cousin nodes of " + key + " is : ");
print_cousin_node(root, height, parent, 0);
print("\n");
}
else
{
print("Given key node [" + key + "] is not exist \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
//Make object of Binary Tree
var obj: BinaryTree = new BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
obj.root = new Node(1);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(5);
obj.root.right.left = new Node(7);
obj.root.left.right = new Node(3);
obj.root.left.left.left = new Node(9);
obj.root.right.right.right = new Node(11);
//Display tree elements
print("\nBinary Tree Nodes : ");
obj.inorder(obj.root);
print("\n");
//Test Case
obj.find_cousin_node(2);
obj.find_cousin_node(1);
obj.find_cousin_node(5);
obj.find_cousin_node(4);
obj.find_cousin_node(9);
}
}``````

#### Output

``````Binary Tree Nodes :   9  2  6  3  1  7  8  5  11
Cousin nodes of 2 is :  7 5
Cousin nodes of 1 is : None
Cousin nodes of 5 is :  2 3
Given key node [4] is not exist
Cousin nodes of 9 is :  11``````
``````/*
Swift 4 Program
Print cousins of given node in a binary tree
*/
//Node of Binary Tree
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinaryTree
{
var root: Node? ;
var parent: Node? ;
var height: Int;
init()
{
//Set initial tree root to null
self.root = nil;
self.parent = nil;
self.height = 0;
}
//Display Inorder view of binary tree
func inorder(_ node: Node? )
{
if (node != nil)
{
// Executing left subtree
self.inorder(node!.left);
// Print node value
print("  ", node!.data, terminator: "");
// Executing right subtree
self.inorder(node!.right);
}
}
//Check whether given key node are exist in binary tree or not.
// If key exists then return its parent node and calculate the height of key node
func find_node_and_height(_ root: Node? , _ level : Int, _ key: Int) -> Node?
{
if (root == nil)
{
//When get empty node
return nil;
}
else if ((root!.left != nil && root!.left!.data == key)
|| (root!.right != nil && root!.right!.data == key))
{
// When given key exists in binary tree
// Get node height
self.height = level + 1;
//return parent node
return root;
}
else
{
//Used to detect key node in left subtree
var result: Node? = nil;
result = self.find_node_and_height(root!.left, level + 1, key);
if (result != nil)
{
// If get a parent node of given key
return result;
}
return self.find_node_and_height(root!.right, level + 1, key);
}
}
//Print cousin nodes in given level
func print_cousin_node(_ root: Node? , _ print_level : Int, _ parent: Node? , _ level : Int)
{
if (root == nil || parent === root || level > print_level)
{
// When node is null
// or when root node is parent of key
// or when level exceed print level
return;
}
if (print_level == level)
{
//Display cousin nodes
print(" ", root!.data ,"", terminator: "");
return;
}
//Recursively execute left and right subtree
self.print_cousin_node(root!.left, print_level, parent, level + 1);
self.print_cousin_node(root!.right, print_level, parent, level + 1);
}
//This are handling the request of finding cousin node of given key
func find_cousin_node(_ key: Int)
{
self.parent = nil;
if (self.root!.data == key)
{
//There are no cousins of root node
print("Cousin nodes of ", key ," is : None \n", terminator: "");
return;
}
self.height = 0;
//First get height and parent node of given key
let parent: Node? = self.find_node_and_height(self.root, 0, key);
if (parent != nil)
{
print("Cousin nodes of ", key ," is : ", terminator: "");
self.print_cousin_node(self.root, self.height, parent, 0);
print("\n", terminator: "");
}
else
{
print("Given key node [", key ,"]is not exist \n", terminator: "");
}
}
}
func main()
{
//Make object of Binary Tree
let obj: BinaryTree = BinaryTree();
/*
Construct Binary Tree
-----------------------

1
/   \
6     8
/ \   / \
2   3 7   5
/           \
9             11
*/
obj.root = Node(1);
obj.root!.left = Node(6);
obj.root!.left!.left = Node(2);
obj.root!.right = Node(8);
obj.root!.right!.right = Node(5);
obj.root!.right!.left = Node(7);
obj.root!.left!.right = Node(3);
obj.root!.left!.left!.left = Node(9);
obj.root!.right!.right!.right = Node(11);
//Display tree elements
print("\nBinary Tree Nodes : ", terminator: "");
obj.inorder(obj.root);
print("\n", terminator: "");
//Test Case
obj.find_cousin_node(2);
obj.find_cousin_node(1);
obj.find_cousin_node(5);
obj.find_cousin_node(4);
obj.find_cousin_node(9);
}
main();``````

#### Output

``````Binary Tree Nodes :    9   2   6   3   1   7   8   5   11
Cousin nodes of  2  is :   7   5
Cousin nodes of  1  is : None
Cousin nodes of  5  is :   2   3
Given key node [ 4 ]is not exist
Cousin nodes of  9  is :   11``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.