CS With NehalCS With Nehal

Binary Tree Simulator

Build a binary tree, run pre-order, in-order, post-order and level-order traversals step by step, and see its height and leaf nodes.

Build the tree

Insert fills the tree level by level, left to right. Or click a node to select it and add a child exactly where you want.

Tree

DBEAFCG

Drag to move · scroll to zoom

7
Nodes
2
Height
edges on the longest root-to-leaf path
4
Leaves
D, E, F, G
3
Internal nodes

Traversal

Step 1 / 8
Press Play to traverse.

Pseudocode

The current line is highlighted.

Full order: D → B → E → A → F → C → G

About This Simulation

This binary tree simulator lets you build a tree level by level or by adding left and right children wherever you like. It animates pre-order, in-order, post-order and level-order traversals and shows the height, leaves and number of nodes.

Features

  • Insert in level order or add a child to a selected node
  • Delete a node together with its subtree
  • Pre-order, in-order, post-order and level-order traversals step by step
  • Visit order shown as a growing output sequence
  • Height, node count, leaf nodes and internal nodes

Topics covered

  • Binary Tree
  • Root, Parent, Child and Leaf
  • Tree Traversal
  • Tree Height and Depth
  • Recursion

Explanation

A binary tree is a hierarchical data structure where each node has at most two children, called the left child and the right child. The top node is the root, nodes without children are leaves, and the height of the tree is the number of edges on the longest path from the root to a leaf.

How it works

  1. Pre-order: visit the node, then traverse the left subtree, then the right subtree (root, left, right).
  2. In-order: traverse the left subtree, visit the node, then traverse the right subtree (left, root, right).
  3. Post-order: traverse the left subtree, then the right subtree, then visit the node (left, right, root).
  4. Level-order: visit nodes level by level from left to right, using a queue.
  5. The depth of a node is the number of edges from the root to it; the height of the tree is the greatest depth.

Why it matters

Binary trees are on the Cambridge 9618 syllabus and in university data structures courses. They underpin binary search trees, heaps, expression trees and syntax trees in compilers, and traversals are a classic use of recursion.

Frequently Asked Questions

What is the difference between pre-order, in-order and post-order?

They differ in when the node itself is visited: before its subtrees (pre-order), between them (in-order) or after them (post-order).

What is a leaf node?

A node with no children. Every other node is an internal node.

What is the height of a binary tree?

The number of edges on the longest path from the root down to a leaf. A tree with only a root has height 0.

Which traversal is used to evaluate an expression tree?

Post-order: both operands are evaluated before the operator. In-order gives the normal (infix) expression and pre-order gives prefix notation.

Related Simulations