m2skills

cousin cpp

May 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. // program to find out if 2 nodes are cousins or not
  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. // function to check if 2 nodes are siblings or not
  47. bool isSibling(node* parent, int n1, int n2){
  48.     if(parent == NULL){
  49.         return false;
  50.     }
  51.     if(parent->left != NULL && parent->right != NULL) {
  52.         return (parent->left->data == n1 && parent->right->data == n2) ||
  53.                (parent->left->data == n2 && parent->right->data == n1);
  54.     }
  55.     return (isSibling(parent->left, n1, n2) ||
  56.             isSibling(parent->right, n1, n2)
  57.     );
  58. }
  59.  
  60. bool isCousin(node* root, int a, int b){
  61.     if( (level_of_node(root, a) == level_of_node(root, b) && isSibling(root, a, b))){
  62.         return true;
  63.     }
  64.     return false;
  65. }
  66.  
  67. int main() {
  68.  
  69.     node* head = createNode(1);
  70.     head->left = createNode(2);
  71.     head->right = createNode(3);
  72.     head->left->left = createNode(4);
  73.     head->left->right = createNode(5);
  74.     head->right->right = createNode(6);
  75.     head->left->left->right = createNode(7);
  76.     head->right->right->left = createNode(8);
  77.     head->left->left->right->left = createNode(9);
  78.     head->left->left->right->left->left = createNode(10);
  79.     head->right->right->left->right = createNode(11);
  80.     cout<<"Nodes 2 and 3 are siblings : "<<isCousin(head, 2, 3)<<endl;
  81.     cout<<"Nodes 6 and 10 are siblings : "<<isCousin(head, 6, 10)<<endl;
  82.     cout<<"Nodes 7 and 8 are siblings : "<<isCousin(head, 7, 8)<<endl;
  83.  
  84.  
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.  
  91. Nodes 2 and 3 are siblings : 1
  92. Nodes 6 and 10 are siblings : 0
  93. Nodes 7 and 8 are siblings : 0
  94. Process finished with exit code 0
  95.  
  96. */
Add Comment
Please, Sign In to add comment