Skip to main content
Binary Trees & Graphs
CHAPTER 04 Beginner

Binary Tree Representation

Updated: May 17, 2026
7 min read

# CHAPTER 4

Binary Tree Representation

1. Introduction

When you draw a Binary Tree on a whiteboard, it is a 2D geometric structure with nodes branching in multiple directions. However, a computer's physical RAM is just a massive 1D line of sequential memory addresses. How do we force a 2D branching tree into a 1D memory stick without destroying its hierarchical geometry? There are two primary architectural strategies used in computer science: Linked Representation (using Pointers) and Array Representation (using Index Math). Knowing which to use determines your application's speed and memory efficiency.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Construct a Binary Tree using Linked Node representation.
  • Construct a Complete Binary Tree using Array representation.
  • Apply mathematical index formulas to find Left/Right children in an Array.
  • Evaluate the critical memory tradeoffs between Pointers and Arrays.

3. Linked Representation (Using Pointers)

This is the most common and flexible way to represent a tree. Instead of trying to store nodes sequentially in memory, we scatter the nodes randomly across RAM. We preserve the geometry by embedding Memory Pointers directly inside each node. Every node knows exactly where its children live.

The Node Structure:

  1. 1. A data variable (Integer, String, etc.)
  1. 2. A Pointer variable pointing to the Left Child's RAM address.
  1. 3. A Pointer variable pointing to the Right Child's RAM address.

*(If a child does not exist, the pointer is explicitly set to NULL / None).*

Visualizing Memory:

text
1
[Data: 10 | Left: 0x2A | Right: 0x5F]  --> Root

Pros: Extremely dynamic. You can insert or delete nodes infinitely without resizing memory blocks. Excellent for chaotic, unbalanced trees. Cons: Pointers consume extra physical RAM.

4. Array Representation (Using Index Math)

What if we don't want to waste RAM on pointers? If our tree is a Complete Binary Tree (tightly packed left-to-right), we can store the nodes linearly in a standard Array and use rigid mathematical formulas to simulate the branching edges!

The Array Layout (1-Indexed Array): Insert the Root at Index 1. Read the tree level-by-level, left-to-right, sliding the data into subsequent array slots.

The Index Math Formulas: If a Node is located at Array Index $i$:

  • Its Left Child is located at Index $2 \times i$
  • Its Right Child is located at Index $(2 \times i) + 1$
  • Its Parent is located at Index $\lfloor i / 2 \rfloor$

Example Tree:

text
12345
        A(1)
       /    \
     B(2)   C(3)
    /  \
  D(4) E(5)

Array: [null, A, B, C, D, E] *Math Check:* Where is B's left child? $B$ is at Index 2. Left child = $2 \times 2 = 4$. Index 4 holds $D$. The math flawlessly simulates physical edges in $O(1)$ time!

Pros: Blisteringly fast $O(1)$ access. Zero RAM wasted on pointers. Highly cache-friendly. Cons: Catastrophic for Degenerate (unbalanced) trees. If you have a straight-line right-leaning tree of 10 nodes, the mathematical array index requires $2^{10}$ ($1024$) slots of memory, wasting 1014 slots of empty null space!

5. Code Example: Array Simulation

Here is how an algorithm traverses an Array representation to find a parent.

Python:

python
12345678910111213141516
tree_array = [None, 'A', 'B', 'C', 'D', 'E'] # 1-Indexed

def get_left_child(index):
    return tree_array[2 * index]

def get_parent(index):
    import math
    return tree_array[math.floor(index / 2)]

# Find B's Parent (B is at index 2)
parent = get_parent(2)
print(f"Parent of B is: {parent}") # Output: A

# Find B's Left Child
left_child = get_left_child(2)
print(f"Left child of B is: {left_child}") # Output: D

6. Complexity Analysis

Linked Representation:
  • Space Complexity: $O(N)$ (Data + 2 Pointers per node).
  • Expansion Time: $O(1)$ instant dynamic memory allocation.

Array Representation:

  • Space Complexity (Complete Tree): $O(N)$ (Perfect packing).
  • Space Complexity (Degenerate Tree): $O(2^N)$ (Catastrophic memory waste).

7. Common Mistakes

  • Using Arrays for heavily modified trees: If your tree constantly deletes nodes and re-balances, do not use an Array. Shifting hundreds of elements across a linear array to fix the gaps takes $O(N)$ time and destroys CPU efficiency. Use linked pointers!

8. Best Practices

  • Use Linked Representation for standard Binary Search Trees (BST) and dynamic applications.
  • Use Array Representation exclusively for strict Binary Heaps (Priority Queues), where the Complete Tree structure is mathematically guaranteed.

9. Exercises

  1. 1. Given an Array Representation [null, 10, 20, 30, 40, 50, 60, 70], calculate the mathematical index of node 30's Right Child, and identify its value.
  1. 2. Write a function in Java or C++ that returns the Right Child of a linked node structure.

10. MCQs with Answers

Question 1

What localized variable must be embedded inside a node to execute a "Linked Representation" tree?

Question 2

When utilizing a 1-Indexed Array Representation, what mathematical formula locates the Left Child of a node at index $i$?

Question 3

When utilizing a 1-Indexed Array Representation, what mathematical formula locates the Parent of a node at index $i$?

Question 4

What is the fatal architectural flaw of using Array Representation to store a "Degenerate" (straight-line) Binary Tree?

Question 5

Why is Array Representation universally used to program "Binary Heap" data structures?

Question 6

In Linked Representation, if a node is a "Leaf Node", what is the strict value of its Left and Right pointers?

Question 7

What is the Space Complexity of storing a Linked Representation Binary Tree with $N$ nodes?

Question 8

If node $X$ is at Array index 5, what is the index of its Right Child?

Question 9

If a 1-indexed Array tree has a total of 7 elements (Indices 1 to 7), what is the index of the absolute Root node?

Q10. True or False: Linked Representation allows nodes to be randomly scattered across fragmented physical RAM clusters. a) True. Pointers bypass the necessity for sequential hardware memory blocks, allowing the OS to place nodes dynamically wherever RAM space exists. b) False. Nodes must be contiguous. Answer: a) True. Pointers bypass the necessity for sequential hardware memory blocks...

11. Interview Questions

  • Q: How do the Left/Right child mathematical formulas change if you use a 0-Indexed Array instead of a 1-Indexed array? *(Answer: Left child becomes (2*i) + 1. Right child becomes (2*i) + 2. Parent becomes (i-1) / 2).*
  • Q: Discuss the memory tradeoffs between Pointers and Arrays for a massive 1 Billion node system.

12. Summary

Trees cannot physically exist in hardware; they must be simulated. Linked Representation relies on structural Pointers to teleport across fragmented RAM, providing dynamic flexibility. Array Representation abandons pointers entirely, utilizing elegant $2 \times i$ integer mathematics to simulate edges with blistering $O(1)$ speed, demanding strict Complete Tree packing to survive.

13. Next Chapter Recommendation

Now that our nodes exist in memory, how do we systematically read them? In Chapter 5: Binary Tree Traversal Algorithms, we will architect recursive loops to read, print, and process the tree using Inorder, Preorder, and Postorder matrices.

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: ·