Printing nodes that don’t have sibling in Java

Hey Everyone! In this article, we will learn how to find out the nodes in a given binary tree that does not have any sibling and print it in Java.


Before directly getting into the code, first, let us understand a few terms.

Binary tree


Binary Tree is defined as a Tree in which it consists of maximum two children known as the left child and the right child.

Example:-

Binary Tree

Nodes in a Binary Tree


The Binary Tree is made entirely of nodes.
Nodes contain data in them and maybe further succeeded by nodes i.e either the left node or the right node or both.
The head of the binary tree is known as the root node.
Refer the diagram below for better understanding




Example:-

Nodes in a Binary Tree

Siblings in a Binary tree


Nodes belonging to the same parent in a binary tree are known as siblings.
Refer the diagram below for better understanding.

Example:-
Siblings in a Binary tree

Program to find nodes that don’t have sibling in a binary tree in Java


Now, in this program, we have to find all the nodes that do not have any sibling.
Let us write our code according to the binary tree shown below.
No sibling nodes in a Binary Tree in java

public class NoSiblings {

    
    public static void main(String[] args) {
        
        //Constructing a binary tree according to our example.
        //You can create it for any binary tree.
        Node root = new Node('A'); // root node
        //creating the binary tree using the reference of the root node
        root.left = new Node('B');
        root.right = new Node('C');
        root.left.left = new Node('E');
        root.left.right = new Node('D');
        root.left.left.left = new Node('F');
        root.left.right.right = new Node('G');
        
        System.out.println("The nodes that do not have any siblings are :- ");
        NoSiblings ns = new NoSiblings();
        ns.NoSiblingNodes(root);
        System.out.println("");
        
        
    }
    
    
   void NoSiblingNodes(Node root){
    if(root!=null)
                {
                        //Recursively traversing throught Right subtree
                        NoSiblingNodes(root.right);
      
                        //Here applying conditions on the parent node
      if((root.left==null) && (root.right!=null))
                        {
                            
        System.out.print(" " + root.right.data);
      }
      if((root.left!=null) && (root.right==null))
                        {
                            
        System.out.print(" " + root.left.data);
      }
                        
      //Recursively traversing throught Left subtree
                        NoSiblingNodes(root.left);
                        
    }

  }
    
}


class Node{
  

    // any data type such as int float double can be 
    //considered for constructing the nodes
    char data; 
    Node right;
    Node left;
  
    Node(char info)  
    { 
        this.data = info; 
        this.left=null;
        this.right=null;
    } 

}

 

The above code is written w.r.t to the binary tree considered in the diagram above.

Output :-

The nodes that do not have any siblings are :- 
 G F

Thus, referring to the binary tree considered above the nodes that do not have any siblings are nodes G and F.


I hope this article was helpful to you. If you have any doubts or suggestions please leave a comment down below.


Leave a Reply

Your email address will not be published. Required fields are marked *