m2skills

distance BT java

Jun 1st, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to find distance between 2 nodes in a given binary tree
  3.  
  4. /**
  5.  * Created by MOHIT on 25-05-2018.
  6.  */
  7.  
  8. import java.io.*;
  9. import java.lang.reflect.Array;
  10. import java.util.*;
  11.  
  12. import static java.lang.Integer.max;
  13.  
  14.  
  15. // node class
  16. class node{
  17.     int data;
  18.     node left;
  19.     node right;
  20.  
  21.     // function that returns a pointer to new node
  22.     public node(int element){
  23.         this.data = element;
  24.         this.left = null;
  25.         this.right = null;
  26.        
  27.     }
  28. };
  29.  
  30.  
  31. public class BinaryTree {
  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 find the least common ancestor of 2 nodes in a binary tree
  63.     static node least_common_ancestor(node root, int n1, int n2){
  64.         if(root == null){
  65.             return root;
  66.         }
  67.        
  68.         if(root.data == n1 || root.data == n2){
  69.             return root;
  70.         }
  71.        
  72.         node left = least_common_ancestor(root.left, n1, n2);
  73.         node right = least_common_ancestor(root.right, n1, n2);
  74.        
  75.         if(left != null && right != null){
  76.             return root;
  77.         }
  78.        
  79.         if(left != null){
  80.             return least_common_ancestor(root.left, n1, n2);
  81.         }
  82.         return least_common_ancestor(root.right, n1, n2);
  83.     }
  84.    
  85.    
  86.     // function that returns the distance between 2 given nodes
  87.     static int distance_between(node root, int a, int b){
  88.         node LCA = least_common_ancestor(root, a, b);
  89.         return level_of_node(LCA, a) + level_of_node(LCA, b);
  90.     }
  91.    
  92.  
  93.  
  94.     public static void main(String arg[]) {
  95.         node head = new node(1);
  96.         head.left = new node(2);
  97.         head.right = new node(3);
  98.         head.left.left = new node(4);
  99.         head.left.right = new node(5);
  100.         head.right.right = new node(6);
  101.         head.left.left.right = new node(7);
  102.         head.right.right.left = new node(8);
  103.         head.left.left.right.left = new node(9);
  104.         head.left.left.right.left.left = new node(10);
  105.         head.right.right.left.right = new node(11);
  106.         System.out.println("Distance between nodes 6 and 10 is : " + distance_between(head, 6, 10));
  107.         System.out.println("Distance between nodes 1 and 10 is : " + distance_between(head, 1, 10));
  108.         System.out.println("Distance between nodes 11 and 10 is : " + distance_between(head, 11, 10));
  109.  
  110.     }
  111. }
Add Comment
Please, Sign In to add comment