Binary Tree Visualizer
Interactive visualization of binary tree operations with step-by-step code execution
Insert/Delete Node
Search Node
Animation Speed
FastSlow
4x Speed
Tree Actions
Tree Structure
Drag to move • Hover nodes to interact
This Tree is Empty!
Add some nodes to get started!
Binary Tree Insert Operation
1function insert(root, value) {
2 // Base case: create new node
3 if (root === null) {
4 return new TreeNode(value);
5 }
6
7 // Recursive case: traverse tree
8 if (value < root.value) {
9 root.left = insert(root.left, value);
10 } else if (value > root.value) {
11 root.right = insert(root.right, value);
12 }
13
14 return root;
15}Step 1 of 6
Check if we've reached an empty spot (base case)
About Binary Trees
Understanding the fundamentals, properties, and complexity analysis of binary tree data structures
What is a Binary Tree?
A binary tree is a hierarchical data structure where each node has at most two children, referred to as the left child and right child. In a Binary Search Tree (BST), the left subtree contains values less than the parent, and the right subtree contains values greater than the parent.
Advantages
- Efficient searching O(log n) average case
- Dynamic size with ordered data
- In-order traversal gives sorted sequence
Disadvantages
- Can become unbalanced O(n) worst case
- No constant-time access by index
- Extra memory overhead for pointers
Properties
Type:Hierarchical
Structure:Non-linear
Search (avg):O(log n)
Search (worst):O(n)
Insert (avg):O(log n)
Delete (avg):O(log n)
Common Use Cases
Database Indexing
Fast data retrieval in databases
File Systems
Directory structure organization
Expression Parsing
Parse mathematical expressions
Decision Trees
Machine learning algorithms
Huffman Coding
Data compression algorithms
Time Complexity
Balanced Tree
SearchO(log n)
InsertO(log n)
DeleteO(log n)
Unbalanced Tree
SearchO(n)
InsertO(n)
DeleteO(n)
Space Complexity
Total spaceO(n)
Auxiliary spaceO(log n)