Linear Search in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <stdlib.h>
int max(int a, int b) { return a > b ? a : b; }
typedef struct N { int d; struct N *l, *r; } N;
N* n(int d) {
    N* t = malloc(sizeof(N));
    t->d = d; t->l = t->r = NULL; return t;
}
int h(N* r) {
    return !r ? 0 : 1 + max(h(r->l), h(r->r));
}
int main() {
    N *r = n(1);
    r->l = n(2); r->r = n(3);
    r->l->l = n(4);
    printf("%d", h(r));
}

C Output

        1
       / \
      2   3
     /
    4


C++ Program

#include <iostream>
using namespace std;
struct N {
    int d; N *l, *r;
    N(int v) : d(v), l(0), r(0) {}
};
int h(N* r) {
    return !r ? 0 : 1 + max(h(r->l), h(r->r));
}
int main() {
    N *r = new N(10);
    r->l = new N(5); r->r = new N(20);
    r->r->r = new N(30);
    cout << h(r);
}

C++ Output

       10
      /  \
     5    20
             \
             30


JAVA Program

class N {
    int d; N l, r;
    N(int d) { this.d = d; }
}
public class Main {
    static int h(N r) {
        return r == null ? 0 : 1 + Math.max(h(r.l), h(r.r));
    }
    public static void main(String[] a) {
        N r = new N(7);
        r.l = new N(3); r.r = new N(9);
        r.l.l = new N(1); r.l.l.l = new N(0);
        System.out.println(h(r));
    }
}

JAVA Output

        7
       / \
      3   9
     /
    1
   /
  0


Python Program

class N:
    def __init__(s, d): s.d, s.l, s.r = d, None, None
def h(r):
    return 0 if not r else 1 + max(h(r.l), h(r.r))

r = N(4)
r.l = N(2); r.r = N(6)
r.r.l = N(5); r.r.r = N(7)
print(h(r))

Python Output

        4
       / \
      2   6
         / \
        5   7


In-Depth Explanation
Example
In the C++ example, the tree is:


    10
   /  \
  5    20
         \
         30

Root node is level 1

Node 20 is level 2

Node 30 is level 3
So the height is 3.

Real-Life Analogy
Consider a company's organizational chart. The CEO is level 1, then VPs are level 2, then managers are level 3, and so on. The taller that hierarchy, the more steps from top to bottom. Likewise, height in a binary tree is how deep you go to get to the farthest node.

Why It Matters
Height of a tree matters most for:

Analyzing performance of tree operations (search, insert, delete)

Recalling balance — AVL trees and Red-Black trees depend upon height

Testing efficiency of hierarchical data models

When a tree becomes too "tall" (such as a linked list), it is inefficient. Small height ensures O(log n) performance.

Learning Takeaways
This issue illustrates:

Recursion — how problems are divided into smaller subproblems

Tree traversal logic

Calculation of maximum depth using divide-and-conquer

The beautiful 1-line recursive solution works as follows:

If node is null → height = 0

Else → height = 1 + max(left height, right height)

Interview & Real-World Use
In interviews, the problem is utilized to test:

Tree understanding

Recursive logic

Handling base vs recursive cases

In real-world usage:

It's applied in UI rendering (hierarchical components)

In compiler design (AST tree height = depth of nesting)

In AI (game trees), databases (index height), and XML parsers

A binary tree's height is one of the most basic data structure concepts. It signifies the depth to which your tree extends and goes towards defining efficiency for searches and insertion operations. With these neat, concise solutions in C, C++, Java, and Python — and various input trees — you now know how to compute tree height easily, accurately, and with real-world application sensitivity.