Data Structures · Visualization

Binary Tree: Insert

Binary Tree

In a general (non-search) binary tree, there is no ordering rule for where a new value must go, so insertion typically fills the tree level by level, left to right, to keep it as balanced and compact as possible (like a heap's shape). Time: O(n) (may need to visit every node to find the first open spot). Space: O(n) for the queue in the worst case.

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

Binary Tree: Insert

Binary Tree

In a general (non-search) binary tree, there is no ordering rule for where a new value must go, so insertion typically fills the tree level by level, left to right, to keep it as balanced and compact as possible (like a heap's shape). This is usually done with a breadth-first search to find the first open spot.

How Insert Works

  1. Start at the root using a queue-based (level-by-level) traversal.
  2. For each node visited, check if its left child is empty; if so, insert the new node there and stop.
  3. Otherwise check if its right child is empty; if so, insert the new node there and stop.
  4. If both children are filled, add both children to the queue and continue to the next node.

Insert Worked Example

Input: Insert 7 into a tree with root 10, left child 5 (right child empty)

  1. Visit root(10): left child (5) is filled, right child is empty
  2. Insert 7 as root's right child

Output: 10 / \ 5 7

Try your own input
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
def insert(root, val):
    if not root:
        return Node(val)
    if val < root.val:
        root.left = insert(root.left, val)
    else:
        root.right = insert(root.right, val)
    return root
# example: insert 7
Input: Insert 7 into a tree with root 10, left child 5 (right child empty)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Insert Time & Space Complexity

TimeO(n) (may need to visit every node to find the first open spot)
SpaceO(n) for the queue in the worst case

When to Use Insert

  • Building a complete binary tree, such as the shape used by heaps
  • Constructing a tree from a level-order list of values
  • Any scenario without ordering rules where a balanced shape is desired

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.