Data Structures · Visualization

Binary Tree: Introduction

Binary Tree

A binary tree is a structure made of nodes, where each node holds a value and points to at most two children, called the 'left' child and the 'right' child. Time: Varies by operation (search/insert/delete are O(h) where h is tree height). Space: O(n).

Big-O: Traversals visit every node exactly once: O(n).

Binary Tree: Introduction

Binary Tree

A binary tree is a structure made of nodes, where each node holds a value and points to at most two children, called the 'left' child and the 'right' child. The topmost node is the 'root', and nodes with no children are called 'leaves'. Unlike a linked list's single chain, a tree branches out, which makes it great for representing hierarchical relationships.

How Binary Tree: Introduction Works

  1. Create a node with a value, a left pointer, and a right pointer (both start as null).
  2. Designate one node as the root — the entry point to the whole tree.
  3. Attach child nodes to the left and/or right of any existing node.
  4. A node with both children null is a leaf; the tree can grow arbitrarily deep.

Binary Tree: Introduction Worked Example

Input: Build a small tree with root 10, left child 5, right child 15

  1. Create root node(10)
  2. root.left = node(5)
  3. root.right = node(15)

Output: 10 / \ 5 15

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Binary Tree: Introduction
A hierarchy of nodes where each node has at most two children — usually called "left" and "right".
45
20
10
30
40
65
50
60
70
80
Active Result
Binary Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Binary Trees in Code
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

# A binary tree: each node has 0, 1, or 2 children
# example: intro

Binary Tree: Introduction Time & Space Complexity

TimeVaries by operation (search/insert/delete are O(h) where h is tree height)
SpaceO(n)

When to Use Binary Tree: Introduction

  • Representing hierarchical data like file systems or org charts
  • The foundation for binary search trees, heaps, and many other structures
  • Expression trees used to evaluate mathematical formulas
  • Decision trees in machine learning

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.