This document provides a beginner-friendly conceptual guide to the data structures and algorithms included in this repository.
The goal is not only to read the code, but to understand what each structure or algorithm is doing, why it works, and where it is useful.
- [Linked List]
- [Stack]
- [Queue]
- [Binary Tree]
- [Binary Search]
- [Tree Traversal]
- [Insertion Sort]
- [Merge Sort]
A Linked List is a linear data structure made from individual nodes.
Each node generally contains:
┌─────────┬─────────┐
│ Data │ Next │
└─────────┴─────────┘
The Next reference points to another node.
For example:
[10] → [20] → [30] → null
The final node points to null, indicating the end of the list.
An array stores elements by index:
[10] [20] [30] [40]
0 1 2 3
A linked list connects elements through references:
[10] → [20] → [30] → [40]
This makes the two structures useful for different situations.
- Insert
- Delete
- Search
- Traverse
Think of a linked list as a chain.
Each link knows where the next link is.
A Stack is a linear data structure that follows:
LIFO — Last In, First Out
The most recently added element is removed first.
┌──────┐
│ 30 │ ← Pop
├──────┤
│ 20 │
├──────┤
│ 10 │
└──────┘
If we perform:
Push(10)
Push(20)
Push(30)
The first element removed will be 30.
Adds an element to the top.
Removes the top element.
Looks at the top element without removing it.
Think about a stack of plates.
You normally place a plate on top and remove the top plate first.
A Queue is a linear data structure that follows:
FIFO — First In, First Out
The first element added is the first element removed.
Front Rear
↓ ↓
[10] → [20] → [30] → [40]
↑ ↑
Dequeue Enqueue
If we perform:
Enqueue(10)
Enqueue(20)
Enqueue(30)
The first element removed will be 10.
Adds an element to the rear of the queue.
Removes an element from the front.
Looks at the front element without removing it.
Think about people standing in a line.
The person who arrives first normally gets served first.
A Binary Tree is a hierarchical data structure in which each node can have at most two children.
Each node can have:
- No children
- One child
- Two children
The top node of the tree.
10 ← Root
A node directly connected below another node.
A node directly above another node.
A node with no children.
In the example above:
2, 7, 20
are leaf nodes.
Traversal means visiting the nodes of a tree in a particular order.
Three fundamental depth-first traversal methods are:
- Preorder
- Inorder
- Postorder
Root → Left → Right
10 → 5 → 2 → 7 → 15 → 20
The root is visited first.
Left → Root → Right
2 → 5 → 7 → 10 → 15 → 20
For a binary search tree, inorder traversal produces values in sorted order when the tree satisfies the binary-search-tree property.
Left → Right → Root
2 → 7 → 5 → 20 → 15 → 10
The root is visited after both subtrees.
Binary Search is a searching algorithm designed for a sorted collection.
Instead of checking every element one by one, it repeatedly divides the search space.
For example:
[1, 3, 5, 7, 9, 11, 13]
↑
middle
If the target is larger than the middle value, the search can ignore the left half.
If the target is smaller, it can ignore the right half.
The process continues until the value is found or the search space becomes empty.
Large search space
↓
Check middle
↓
Discard half
↓
Check middle again
↓
Discard half
↓
...
O(log n)
This assumes the data is sorted and the implementation can access the middle element efficiently.
Insertion Sort builds a sorted portion of a collection one element at a time.
Conceptually:
Unsorted elements
↓
Take one element
↓
Find its position
↓
Insert it into the sorted portion
↓
Repeat
Example:
[5, 3, 4, 1]
[5] [3, 4, 1]
↓
[3, 5] [4, 1]
↓
[3, 4, 5] [1]
↓
[1, 3, 4, 5]
Insertion Sort is simple and can be useful for small or nearly sorted collections.
- Best case: O(n) when the data is already sorted
- Average case: O(n²)
- Worst case: O(n²)
Merge Sort uses a divide-and-conquer strategy.
It repeatedly divides a collection into smaller parts, sorts those parts, and then merges them back together.
The smaller collections are then merged in sorted order:
[8] + [3] → [3, 8]
[5] + [1] → [1, 5]
[3, 8] + [1, 5] → [1, 3, 5, 8]
Divide
↓
Sort smaller parts
↓
Merge
↓
Sorted collection
O(n log n)
Merge Sort generally requires additional memory for the merging process, depending on the implementation.
| Topic | Category | Main Idea |
|---|---|---|
| Linked List | Data Structure | Nodes connected by references |
| Stack | Data Structure | LIFO |
| Queue | Data Structure | FIFO |
| Binary Tree | Data Structure | Hierarchical nodes |
| Binary Search | Algorithm | Repeatedly divide a sorted search space |
| Insertion Sort | Algorithm | Insert elements into a sorted portion |
| Merge Sort | Algorithm | Divide, sort and merge |
These two ideas are related but different.
A data structure describes how data is organized and accessed.
Examples:
Linked List
Stack
Queue
Binary Tree
An algorithm describes a sequence of steps used to solve a problem.
Examples:
Binary Search
Insertion Sort
Merge Sort
A useful way to think about it:
Data structure = how we organize information
Algorithm = how we process information
When studying these implementations, don't just memorize the method names.
Try asking:
- What problem is this structure solving?
- How is the data represented?
- Where is the next element stored?
- What happens when an element is added?
- What happens when an element is removed?
- What is the cost of the operation?
- What assumptions does the algorithm make?
Understanding these questions makes it easier to read unfamiliar implementations later.