Calculating the Height of a Binary Tree
Note:
This code was written during a crunch period and isn't perfect. There will
be some errant spacing, some files will be
using namespace std,
etc. But it's all still usable and can be a
handy guideline if you're learning Data Structures.
struct Node
{
Node(int value) : value(value){};
int value;
Node *left;
Node *right;
};
int height(Node *cu);
int main(int argc, char *argv[])
{
Node *head = new Node(0);
Node *cu = head;
for (int i = 1; i < 25; i++)
{
Node *tmp = new Node(i);
cu->left = tmp;
cu = cu->left;
}
cu = head;
for (int i = 1; i < 90; i++)
{
Node *tmp = new Node(i);
cu->right = tmp;
cu = cu->right;
}
int ht = height(head);
std::cout << "Calculating the Height of a Binary Tree Test" << std::endl;
std::cout << ht << std::endl;
}
int height(Node *cu)
{
if (cu == nullptr)
{
return 0;
}
else
{
return 1 + std::max(height(cu->left), height(cu->right));
}
}