What Is a Binary Tree?
A binary tree is a type of tree data structure where each node has at most two children:
- A left child
- A right child
Each node contains:
- A value
- A reference to a left child (can be
None)
- A reference to a right child (can be
None)
Example Binary Tree:
1
/ \\
2 3
/ \\
4 5
- Node
1 is the root
- Node
2 is the left child of 1
- Node
3 is the right child of 1
- Node
4 is the left child of 2
- Node
5 is the right child of 2
How It Works
- Trees are built top-down, starting from the root
- You can traverse the tree to visit nodes (explained below)
- Trees are used for organizing data that naturally branches (like file systems or expression trees)
Binary Tree in Python (with TreeNode)