Home
DSA Visualizer
Data Structures
Binary Tree
Binary Tree
A binary tree is a hierarchical structure in which each element has at most two children, referred to as the left child and the right child.
ANY
Any binary tree: every node has at most two children, and nothing else is promised.
Shape
Intuition
A hierarchical structure where each element has at most two children, referred to as the left child and the right child.
Concept
The foundation of many advanced data structures like BST, Heaps, and AVL Trees. There are several variations: Full (0 or 2 children), Perfect (all levels filled), Complete (filled level by level), Balanced (height diff ≤ 1), and Skewed (degenerate tree).
How it Works
- Root: Topmost node.
- Child: Node connected downwards.
- Leaf: Node with no children.
- Height: Longest path from node to leaf.
- Depth: Path length from root to node.
Step-by-Step Breakdown
- Traversal: Visiting nodes (Pre-order, In-order, Post-order, Level-order).
- Insertion: Adding a node (logic varies by tree type).
- Deletion: Removing a node (often replacing with leaf or child).
- Height/Depth: Calculating tree metrics.
When to Use
- Representing hierarchical data (file systems, HTML DOM).
- Decision trees.
- Expression parsers.
- Binary Heaps (Complete Trees).
When NOT to Use
- When data is linear (use Array/Linked List).
- When relationships are complex/cyclic (use Graph).
How to Identify
Any tree structure where max degree of any node is 2.
Applying for software engineering roles? Free, no sign-up:
