Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,71 @@
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"files.associations": {
"iostream": "cpp",
"string": "cpp"
"string": "cpp",
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"scoped_allocator": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp"
}
}
89 changes: 89 additions & 0 deletions CPP/data_structures/trees/Trees/Calculate_Tree_Height.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Algorithm: Height of Binary Tree
* Description: Recursively finds the maximum number of edges on the path from the root to any leaf node.
* Time Complexity: O(n)
* Space Complexity: O(h) // Where 'h' is the height of the tree, due to recursion stack
* Author: Adi-3108
*/

#include <iostream>
#include <algorithm> // Required for max

using namespace std;
// 1. Structure Definition
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;

// Constructor
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

/**
* @brief Recursively calculates the height of the binary tree.
* * Height is defined as the number of edges on the longest path from the root to a leaf.
* * @param root The root of the binary tree (or subtree).
* @return The height of the tree (number of edges).
*/
int calculateHeight(TreeNode* root) {
// Base case: An empty tree (NULL) has a height of -1 (so a single node returns 0).
if (root == NULL) {
return -1;
}

// Recursively find the height of the left and right subtrees
int left_height = calculateHeight(root->left);
int right_height = calculateHeight(root->right);

// The height of the current tree is 1 (for the current node/edge)
// plus the maximum height of the subtrees.
return 1 + max(left_height, right_height);
}

// Test function with modern C++ features
void testCalculateHeight() {
// Test Case 1: Skewed Tree (Height = 3)
// 1 -> 2 -> 3 -> 4
TreeNode *root1 = new TreeNode(1);
root1->left = new TreeNode(2);
root1->left->left = new TreeNode(3);
root1->left->left->left = new TreeNode(4);
// for verification We are Printing the expected height
cout << "Test Case 1 (Skewed Tree):\n";
cout << "Expected Height: 3, Actual Height: " << calculateHeight(root1) << endl;

// Test Case 2: Balanced Tree (Height = 2)
// 1
// / \
// 2 3
// / \
// 4 5
TreeNode *root2 = new TreeNode(1);
root2->left = new TreeNode(2);
root2->right = new TreeNode(3);
root2->left->left = new TreeNode(4);
root2->left->right = new TreeNode(5);

cout << "\nTest Case 2 (Balanced Tree):\n";

// for verification We are Printing the expected height
cout << "Expected Height: 2, Actual Height: " << calculateHeight(root2) << endl;

// Clean up memory
delete root1->left->left->left;
delete root1->left->left;
delete root1->left;
delete root1;

delete root2->left->left;
delete root2->left->right;
delete root2->left;
delete root2->right;
delete root2;
}

int main() {
testCalculateHeight();
return 0;
}