📖 Scenario: You are working on a program that manages a family tree. Each person can have children, and you want to find out how many generations are in the family tree. This means finding the height of the tree, which is the longest path from the oldest ancestor to the youngest descendant.
🎯 Goal: Build a program in C that creates a simple binary tree, then calculates and prints its height.
📋 What You'll Learn
Create a binary tree node structure with integer data and left and right child pointers
Manually create a binary tree with 5 nodes in a specific shape
Write a recursive function to calculate the height of the binary tree
Print the height of the binary tree
💡 Why This Matters
🌍 Real World
Binary trees are used in many applications like organizing family trees, file systems, and decision processes.
💼 Career
Understanding tree structures and recursion is important for software development, especially in areas like databases, compilers, and AI.
Progress0 / 4 steps
1
Create the binary tree node structure and build the tree
Write the struct Node with int data, struct Node* left, and struct Node* right. Then create 5 nodes named root, node1, node2, node3, and node4. Assign data values 1, 2, 3, 4, and 5 respectively. Connect them to form this tree:
1 / \ 2 3 / \ 4 5
DSA C
Hint
Start by defining the Node structure. Then create variables for each node and assign their data and child pointers to build the tree shape.
2
Create a function to calculate the height of the tree
Write a recursive function called height that takes a pointer to Node named root and returns an int. The function should return 0 if root is NULL. Otherwise, it should return 1 plus the maximum height of the left and right subtrees.
DSA C
Hint
Use recursion to find the height of left and right children, then return the bigger one plus one.
3
Call the height function in main
Inside main, create an int variable called tree_height. Assign it the result of calling height with the address of root.
DSA C
Hint
Call the height function with &root and store the result in tree_height.
4
Print the height of the binary tree
Add a printf statement in main to print the text "Height of the tree: " followed by the value of tree_height and a newline.
DSA C
Hint
Use printf("Height of the tree: %d\n", tree_height); to show the height.
Practice
(1/5)
1. What is the height of a binary tree with only one node (the root)?
easy
A. 0
B. 1
C. 2
D. Undefined
Solution
Step 1: Understand height definition
The height of a binary tree is the number of edges on the longest path from the root to a leaf.
Step 2: Apply to single node tree
With only one node (root), the longest path has length 0 edges.
Final Answer:
0 -> Option A
Quick Check:
Height of single node tree = 0 [OK]
Hint: Height counts edges on longest root-to-leaf path [OK]
Common Mistakes:
Thinking height is 1 for single node
Confusing height with number of nodes
Assuming height is undefined for single node
2. Which of the following is the correct C function signature to calculate the height of a binary tree node?
easy
A. void height(struct Node root);
B. int height(Node root);
C. int height(struct Node* root);
D. int height(struct Node root);
Solution
Step 1: Check parameter type for tree node
In C, a tree node is usually passed as a pointer to struct Node.
Step 2: Check return type and parameter correctness
The function returns an int (height) and takes a pointer to struct Node.
Final Answer:
int height(struct Node* root); -> Option C
Quick Check:
Correct signature uses pointer to struct Node [OK]
Hint: Use pointer to struct Node for tree parameters [OK]
Common Mistakes:
Passing struct Node by value instead of pointer
Using void return type instead of int
Missing struct keyword in parameter
3. What is the output of the following code snippet calculating the height of this binary tree?