m2skills

isBalanced cpp

May 15th, 2018
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // function to print if the given binary tree is balanced or not
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. // node class
  7. class node{
  8. public:
  9.     int data;
  10.     int hd;
  11.     node* left;
  12.     node* right;
  13. };
  14.  
  15. // function that returns a pointer to new node
  16. node* createNode(int element){
  17.     node* temp = (node*) malloc(sizeof(node));
  18.     temp->data = element;
  19.     temp->hd = -1;
  20.     temp->left = NULL;
  21.     temp->right = NULL;
  22.     return temp;
  23. }
  24.  
  25. // function to find the height of a binary tree
  26. int height(node* root){
  27.     if (root == NULL){
  28.         return 0;
  29.     }
  30.     return (1 + max(height(root->left), height(root->right)));
  31. }
  32.  
  33. // function to check if the tree is a height balanced tree or not
  34. bool isBalanced(node* root){
  35.     if(root == NULL){
  36.         return true;
  37.     }
  38.     // get the heights of the left and the right subtrees
  39.     int left_height = height(root->left);
  40.     int right_height = height(root->right);
  41.     // if the difference between heights of the left and right subtrees is less than 2
  42.     // and left and right subtrees are also balanced then return true
  43.     return abs(left_height - right_height) <= 1 && isBalanced(root->left) && isBalanced(root->right);
  44.  
  45. }
  46.  
  47. int main() {
  48.  
  49.     node* head = createNode(1);
  50.     head->left = createNode(2);
  51.     head->right = createNode(3);
  52.     head->left->left = createNode(4);
  53.     head->left->right = createNode(5);
  54.     head->right->right = createNode(6);
  55.     head->left->left->right = createNode(7);
  56.     head->right->right->left = createNode(8);
  57.     head->left->left->right->left = createNode(9);
  58.     head->left->left->right->left->left = createNode(10);
  59.     head->right->right->left->right = createNode(11);
  60.     cout<<"The binary tree #1 is Balanced : "<<isBalanced(head)<<endl;
  61.  
  62.     node* head2 = createNode(5);
  63.     head2->left = createNode(2);
  64.     head2->right = createNode(12);
  65.     head2->left->left = createNode(-4);
  66.     head2->left->right = createNode(3);
  67.     head2->right->left = createNode(9);
  68.     head2->right->right = createNode(21);
  69.     head2->right->right->left = createNode(19);
  70.     head2->right->right->right = createNode(25);
  71.     cout<<"The binary tree #2 is Balanced : "<<isBalanced(head2)<<endl;
  72.  
  73.     node* m1 = createNode(8);
  74.     m1->left = createNode(3);
  75.     m1->right = createNode(10);
  76.     m1->left->left = createNode(1);
  77.     m1->left->right = createNode(6);
  78.     m1->left->right->left = createNode(4);
  79.     m1->left->right->right = createNode(7);
  80.     m1->right->right = createNode(14);
  81.     m1->right->right->left = createNode(13);
  82.     cout<<"The binary tree #3 is Balanced : "<<isBalanced(m1);
  83.  
  84. }
  85.  
  86.  
  87.  
  88. /*
  89.  
  90.  
  91. The binary tree #1 is Balanced : 0
  92. The binary tree #2 is Balanced : 1
  93. The binary tree #3 is Balanced : 0
  94. Process finished with exit code 0
  95.  
  96. */
Add Comment
Please, Sign In to add comment