m2skills

distance BT cpp

May 15th, 2018
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. // program to print the distance between 2 nodes in a binary tree
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. // node class
  7. class node{
  8. public:
  9.     int data;
  10.     node* left;
  11.     node* right;
  12. };
  13.  
  14. // function that returns a pointer to new node
  15. node* createNode(int element){
  16.     node* temp = (node*) malloc(sizeof(node));
  17.     temp->data = element;
  18.     temp->left = NULL;
  19.     temp->right = NULL;
  20.     return temp;
  21. }
  22.  
  23. // function to find and return the level of a node in binary tree
  24. int level_of_node(node* root, int data, int level = -1){
  25.     // if the tree is empty or if we reach a leaf node then return 0
  26.     if (root == NULL){
  27.         return -1;
  28.     }
  29.     if(root->data == data){
  30.         return level+1;
  31.     }
  32.  
  33.     // check in the left subtree for the element
  34.     // if found then return the level
  35.     int level_node = level_of_node(root->left, data, level + 1);
  36.     if (level_node != -1){
  37.         return level_node;
  38.     }
  39.  
  40.     // searching for the node in right subtree
  41.     level_node = level_of_node(root->right, data, level + 1);
  42.     return level_node;
  43.  
  44. }
  45.  
  46. node* least_common_ancestor(node* root, int n1, int n2){
  47.     if(root == NULL){
  48.         return root;
  49.     }
  50.  
  51.     if(root->data == n1 || root->data == n2){
  52.         return root;
  53.     }
  54.  
  55.     node* left = least_common_ancestor(root->left, n1, n2);
  56.     node* right = least_common_ancestor(root->right, n1, n2);
  57.  
  58.     if(left != NULL && right != NULL){
  59.         return root;
  60.     }
  61.  
  62.     if(left != NULL){
  63.         return least_common_ancestor(root->left, n1, n2);
  64.     }
  65.     return least_common_ancestor(root->right, n1, n2);
  66. }
  67.  
  68. // function that returns the distance between 2 given nodes
  69. int distance_between(node* root, int a, int b){
  70.     node* LCA = least_common_ancestor(root, a, b);
  71.     return level_of_node(LCA, a) + level_of_node(LCA, b);
  72. }
  73.  
  74. int main() {
  75.  
  76.     node* head = createNode(1);
  77.     head->left = createNode(2);
  78.     head->right = createNode(3);
  79.     head->left->left = createNode(4);
  80.     head->left->right = createNode(5);
  81.     head->right->right = createNode(6);
  82.     head->left->left->right = createNode(7);
  83.     head->right->right->left = createNode(8);
  84.     head->left->left->right->left = createNode(9);
  85.     head->left->left->right->left->left = createNode(10);
  86.     head->right->right->left->right = createNode(11);
  87.     cout<<"Distance between nodes 6 and 10 is : "<<distance_between(head, 6, 10)<<endl;
  88.     cout<<"Distance between nodes 1 and 10 is : "<<distance_between(head, 1, 10)<<endl;
  89.     cout<<"Distance between nodes 11 and 10 is : "<<distance_between(head, 11, 10)<<endl;
  90.  
  91. }
  92.  
  93.  
  94. /*
  95.  
  96. Distance between nodes 6 and 10 is : 7
  97. Distance between nodes 1 and 10 is : 5
  98. Distance between nodes 11 and 10 is : 9
  99.  
  100. Process finished with exit code 0
  101.  
  102. */
Add Comment
Please, Sign In to add comment