If youāve ever scratched your head at the mention of binary trees or felt overwhelmed by tree data structures in generalādonāt worry, youāre not alone. Binary trees are one of the most fundamental and powerful data structures in computer science, and once you understand how they work, a whole new world of algorithmic problem-solving opens up.
In this guide, weāll break binary trees down into digestible chunks and build one from scratch using JavaScript. By the end, youāll understand what binary trees are, why they matter, and how to implement them in code.
š§ What is a Binary Tree?
A binary tree is a hierarchical data structure in which each node has at most two childrenācommonly referred to as the left and right child.
Hereās a visual example:
10
/ \
5 15
/ \
3 7
In the tree above:
-
10
is the root node. -
5
and15
are children of10
. -
3
and7
are children of5
.
š¦ Each node in a binary tree contains:
- A value
- A pointer to a left child node (if any)
- A pointer to a right child node (if any)
š§± Real-World Analogies
Binary trees might sound abstract, but they show up everywhere:
- File systems: Folders containing files and subfolders
- Game decision trees: AI choosing optimal moves
- Expression trees: Used in compilers and calculators
- Hierarchical UI components: Think collapsible menus
š Implementing a Binary Tree in JavaScript
Letās roll up our sleeves and build a basic binary tree from scratch.
š¹ Step 1: Create a Node
class
Each node will hold a value and pointers to its left and right children.
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
šø Step 2: Create the BinaryTree
class
This class manages the root and provides methods to work with the tree.
class BinaryTree {
constructor() {
this.root = null;
}
insert(value) {
const newNode = new Node(value);
if (!this.root) {
this.root = newNode;
return;
}
// Level-order insertion using a queue
const queue = [this.root];
while (queue.length) {
const current = queue.shift();
if (!current.left) {
current.left = newNode;
break;
} else {
queue.push(current.left);
}
if (!current.right) {
current.right = newNode;
break;
} else {
queue.push(current.right);
}
}
}
}
š” How This Insert Works
Weāre using level-order insertion, which means weāre filling the tree from top to bottom and left to right. This is not a Binary Search Tree yetāthat comes later in this series.
š„ Example Usage:
const tree = new BinaryTree();
tree.insert(10);
tree.insert(5);
tree.insert(15);
After inserting:
10
/ \
5 15
š¤ Why Are Binary Trees Useful?
Hereās why you should care about binary trees:
- They form the foundation for more advanced trees: BST, AVL, Heaps, and Tries
- They're used in search and sort algorithms
- Great for storing hierarchical or recursive data
- A must-know for coding interviews and system design
š§ Common Terms
Term | Meaning |
---|---|
Root | The top node of the tree |
Leaf | A node with no children |
Subtree | A smaller tree within a tree |
Depth | Levels from the root to the current node |
Height | Number of levels from node to deepest leaf |
š Wrapping Up
In this post, you learned:
- What a binary tree is and why itās useful
- How to model a node and a binary tree in JavaScript
- How to insert elements using level-order logic
Top comments (0)