Skip to main content
Binary Trees & Graphs
CHAPTER 03 Beginner

Binary Trees Basics

Updated: May 17, 2026
6 min read

# CHAPTER 3

Binary Trees Basics

1. Introduction

In Chapter 2, we discussed General Trees, where a parent node could have 1, 5, or 100 children. While flexible, general trees are mathematically chaotic, making it difficult to write highly optimized searching algorithms. To tame this chaos, computer scientists created the Binary Tree. By strictly limiting the number of children a node can have, we create predictable geometric boundaries. Binary Trees are the absolute foundation of Database Indexing, 3D Graphic Rendering, and Data Compression algorithms.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define the mathematical constraints of a Binary Tree.
  • Identify Left and Right Children.
  • Distinguish between Full, Complete, Perfect, and Degenerate Binary Trees.
  • Understand the mathematical properties of a Binary Tree's geometry.

3. What is a Binary Tree?

A Binary Tree is a specialized tree data structure where every single node has a maximum of two children. These two children are explicitly named:
  1. 1. The Left Child
  1. 2. The Right Child

A node in a Binary Tree can have:

  • Exactly 0 children (A Leaf node)
  • Exactly 1 child (Left OR Right)
  • Exactly 2 children (Left AND Right)

Visual Example:

text
12345
        A
       / \
      B   C
     /     \
    D       E

4. Types of Binary Trees

Interviewers frequently test your knowledge of specific Binary Tree shapes, as the shape directly impacts the algorithm's Big O Time Complexity.

1. Full Binary Tree (Strict Binary Tree): Every node has either exactly 0 children or exactly 2 children. There are absolutely no nodes with just 1 child.

text
12345
        A
       / \
      B   C
         / \
        D   E

2. Complete Binary Tree: Every single level of the tree is completely filled with nodes, except possibly the very last level. Furthermore, the last level must be filled from Left to Right without any gaps. (This shape is mandatory for creating Heaps).

text
12345
        A
       / \
      B   C
     / \  /
    D   E F

3. Perfect Binary Tree: The most geometrically beautiful tree. It is both Full and Complete. Every internal node has exactly 2 children, and every single leaf node is on the exact same bottom level.

text
12345
        A
       / \
      B   C
     / \ / \
    D  E F  G

4. Degenerate (Pathological) Binary Tree: Every internal node has exactly 1 child. The tree acts exactly like a sluggish Linked List, plunging in a single straight line.

text
12345
    A
     \
      B
       \
        C

5. Mathematical Properties of Binary Trees

Understanding the math behind perfect trees allows you to calculate space and time complexities instantly:
  • Maximum nodes at Level L: $2^L$ (Level 0 has 1, Level 1 has 2, Level 2 has 4).
  • Maximum nodes in a Perfect Tree of Height H: $2^{H+1} - 1$.
  • Example: A perfect tree of height 2 has $2^{3} - 1 = 8 - 1 = 7$ nodes.
  • Minimum Height of a tree with N nodes: $\log_2(N+1) - 1$. (This logarithmic height is what makes searching so incredibly fast!).

6. Code Representation Structure

Unlike general trees which use dynamic arrays for children, a Binary Tree node physically hardcodes two localized pointers.

C / C++:

cpp
123456789101112
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
    
    // Constructor
    Node(int val) {
        data = val;
        left = NULL;
        right = NULL;
    }
};

Java:

java
123456789
class Node {
    int data;
    Node left, right;

    public Node(int item) {
        data = item;
        left = right = null;
    }
}

Python:

python
12345
class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

7. Common Mistakes

  • Confusing Full vs. Complete Trees: A Full tree can be lopsided (as long as nodes have 0 or 2 children). A Complete tree cannot be lopsided; it must be tightly packed level-by-level, left-to-right.
  • Forgetting Degenerate Trees: When evaluating an algorithm, do not assume your Binary Tree looks like a perfect pyramid. Always consider the worst-case scenario: a Degenerate Tree that acts like a $O(N)$ Linked List!

8. Best Practices

  • Always check if the left and right pointers are NULL (or None) before attempting to access their data to avoid Null Pointer Exception crashes.

9. Exercises

  1. 1. Draw a Complete Binary Tree with 6 nodes.
  1. 2. Calculate the maximum number of nodes possible in a Perfect Binary Tree with a height of 3.

10. MCQs with Answers

Question 1

What is the maximum number of children a node can possess in a Binary Tree?

Question 2

A Binary tree where every node has either 0 or 2 children is classified as:

Question 3

In a Complete Binary Tree, the lowest level must be filled in which direction?

Question 4

What type of tree occurs when every internal node has exactly 1 child?

Question 5

A Perfect Binary Tree of height 2 (root is height 0) contains exactly how many nodes?

Question 6

What happens if you try to access the left child of a Leaf Node?

Q7. True or False: Every Perfect Binary Tree is also a Complete Binary Tree. a) True b) False Answer: a) True. A perfect tree is tightly packed and filled left-to-right, naturally satisfying all conditions of a Complete tree.

Q8. True or False: Every Complete Binary Tree is also a Perfect Binary Tree. a) True b) False Answer: b) False. A complete tree might have an incomplete bottom row, disqualifying it from being perfect.

Question 9

The maximum number of nodes present strictly at Level $L$ (where Root is Level 0) is:

Question 10

Why is a Degenerate Binary Tree dangerous for algorithms?

11. Interview Questions

  • Q: How do you mathematically differentiate the worst-case Time Complexity between a Perfect Binary Tree and a Degenerate Binary Tree?
  • Q: Is a single node with no children considered a Full Binary Tree? *(Answer: Yes. It has exactly 0 children, satisfying the 0 or 2 rule).*

12. Summary

By restricting geometric branching to a maximum of two pathways (Left and Right), the Binary Tree provides a rigid, highly predictable architectural framework. Understanding the distinct shapes (Full, Complete, Perfect) is the absolute prerequisite to optimizing algorithmic time limits and manipulating data at logarithmic speeds.

13. Next Chapter Recommendation

We know what a Binary Tree looks like on a whiteboard. But how do we actually store this complex 2D geometric structure inside a flat 1D computer memory stick (RAM)? In Chapter 4: Binary Tree Representation, we cover Linked Node memory allocation and Array-based indexing.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·