diff --git a/BalancedBinaryTree.cpp b/BalancedBinaryTree.cpp new file mode 100644 index 00000000..bd21bd7d --- /dev/null +++ b/BalancedBinaryTree.cpp @@ -0,0 +1,62 @@ +// Checking if a binary tree is height balanced in C++ + +#include +using namespace std; + +#define bool int + +class node { + public: + int item; + node *left; + node *right; +}; + +// Create anew node +node *newNode(int item) { + node *Node = new node(); + Node->item = item; + Node->left = NULL; + Node->right = NULL; + + return (Node); +} + +// Check height balance +bool checkHeightBalance(node *root, int *height) { + // Check for emptiness + int leftHeight = 0, rightHeight = 0; + + int l = 0, r = 0; + + if (root == NULL) { + *height = 0; + return 1; + } + + l = checkHeightBalance(root->left, &leftHeight); + r = checkHeightBalance(root->right, &rightHeight); + + *height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; + + if (std::abs(leftHeight - rightHeight >= 2)) + return 0; + + else + return l && r; +} + +int main() { + int height = 0; + + node *root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + + if (checkHeightBalance(root, &height)) + cout << "The tree is balanced"; + else + cout << "The tree is not balanced"; +}