Tree
A hierarchical non-linear data structure where each node contains a value and references to child nodes.
Traversal Visualizer
Left → Root → Right
8
3
10
1
6
14
4
7
Order:
Types / Variants
Binary Tree
A tree where every node has at most two children.
1
2
3
Binary Search Tree (BST)
A sorted binary tree where the left child's value is less than the parent's, and the right child's is greater.
10
5
15
When to Use Trees
- For hierarchical data, like file systems or organizational structures.
- For fast search, insertion, and deletion in sorted data (using BSTs).
- In compilers for creating abstract syntax trees.
- For database indexing to speed up searches.
Complexity Analysis
| Operation | Time Complexity | Notes |
|---|---|---|
| Search | O(log n) | For balanced BST |
| Insert | O(log n) | For balanced BST |
| Deletion | O(log n) | For balanced BST |
| Traversal | O(n) | Visits every node |