From 2c862ed738e211dcc626f0b690bbd29aa1959fe6 Mon Sep 17 00:00:00 2001 From: pranav290804 <121602294+pranav290804@users.noreply.github.com> Date: Wed, 11 Oct 2023 23:07:24 +0530 Subject: [PATCH] Create BalancedBinaryTree.cpp --- BalancedBinaryTree.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 BalancedBinaryTree.cpp 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"; +}