m2skills

cousin java

Jun 1st, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to print if 2 nodes of a binary tree are cousins or not
  3.  
  4. /**
  5.  * Created by MOHIT on 25-05-2018.
  6.  */
  7.  
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. import static java.lang.Integer.max;
  12.  
  13.  
  14. // node class
  15. class node{
  16.     int data;
  17.     node left;
  18.     node right;
  19.  
  20.     // function that returns a pointer to new node
  21.     public node(int element){
  22.         this.data = element;
  23.         this.left = null;
  24.         this.right = null;
  25.        
  26.     }
  27. };
  28.  
  29.  
  30. public class BinaryTree {
  31.  
  32.  
  33.     static int level_of_node(node root, int data){
  34.         return level_of_node_helper(root, data, -1);
  35.     }
  36.  
  37.  
  38.     // function to find and return the level of a node in binary tree
  39.     static int level_of_node_helper(node root, int data, int level){
  40.         // if the tree is isEmpty or if we reach a leaf node then return 0
  41.         if (root == null){
  42.             return -1;
  43.         }
  44.         if(root.data == data){
  45.             return level+1;
  46.         }
  47.        
  48.         // check in the left subtree for the element
  49.         // if found then return the level
  50.         int level_node = level_of_node_helper(root.left, data, level + 1);
  51.         if (level_node != -1){
  52.             return level_node;
  53.         }
  54.        
  55.         // searching for the node in right subtree
  56.         level_node = level_of_node_helper(root.right, data, level + 1);
  57.         return level_node;
  58.    
  59.     }
  60.    
  61.    
  62.     // function to check if 2 nodes are siblings or not
  63.     static boolean isSibling(node parent, int n1, int n2){
  64.         if(parent == null){
  65.             return false;
  66.         }
  67.         if(parent.left != null && parent.right != null) {
  68.             return  (parent.left.data == n1 && parent.right.data == n2) ||
  69.                     (parent.left.data == n2 && parent.right.data == n1);
  70.         }
  71.         return (isSibling(parent.left, n1, n2) || isSibling(parent.right, n1, n2));
  72.     }
  73.    
  74.     static boolean isCousin(node root, int a, int b){
  75.         if( (level_of_node(root, a) == level_of_node(root, b) && isSibling(root, a, b))){
  76.             return true;
  77.         }
  78.         return false;
  79.     }
  80.    
  81.  
  82.     public static void main(String arg[]) {
  83.         node head = new node(1);
  84.         head.left = new node(2);
  85.         head.right = new node(3);
  86.         head.left.left = new node(4);
  87.         head.left.right = new node(5);
  88.         head.right.right = new node(6);
  89.         head.left.left.right = new node(7);
  90.         head.right.right.left = new node(8);
  91.         head.left.left.right.left = new node(9);
  92.         head.left.left.right.left.left = new node(10);
  93.         head.right.right.left.right = new node(11);
  94.         System.out.println("Nodes 2 and 3 are siblings : " + isCousin(head, 2, 3));
  95.         System.out.println("Nodes 6 and 10 are siblings : " + isCousin(head, 6, 10));
  96.         System.out.println("Nodes 7 and 8 are siblings : " + isCousin(head, 7, 8));
  97.  
  98.     }
  99. }
Add Comment
Please, Sign In to add comment