Overview
Data structures are fundamental building blocks in computer science that help organize and store data efficiently. They work hand-in-hand with algorithms - data structures store the data, while algorithms process it.
Core Data Structures in ACSL:
- Stacks: Last-In-First-Out (LIFO) structure
- Queues: First-In-First-Out (FIFO) structure
- Binary Search Trees: Hierarchical structure for efficient searching
- Priority Queues: Special queue where items have priorities
Key Concepts
Stacks
Key Operations:
- PUSH(item): Adds an item to the top
- POP(): Removes and returns the top item
Real-world Example: Think of a stack of books - you can only add or remove books from the top.
When POP() is called on an empty stack, it returns NIL (or null).
Queues
Key Operations:
- PUSH(item): Adds an item to the rear
- POP(): Removes and returns the front item
Real-world Example: Like a line at a store - first person to arrive is the first person served.
Items enter at the rear (back) and leave from the front, just like a real queue of people.
Binary Search Trees (BST)
Key Properties:
- Each node has at most two children
- Left child's key ≤ Parent's key
- Right child's key > Parent's key
Important Terms:
- Root: Top node of the tree
- Leaf: Node with no children
- Internal Path Length: Sum of depths of all nodes
- External Path Length: Sum of depths of potential insertion points
Tree Traversals:
- Inorder: Left → Root → Right (gives sorted order)
- Preorder: Root → Left → Right
- Postorder: Left → Right → Root
Remember:
- If new value < current node: Go left
- If new value > current node: Go right
- If spot is empty: Insert there
- If duplicate value: Go left (ACSL convention)
Priority Queues
Key Features:
- Elements have priorities
- Can only access/remove the highest priority element
- Usually implemented as a Heap
Heap Properties:
- Complete binary tree (filled from left to right)
- Parent is always smaller (min-heap) or larger (max-heap) than its children
- Root is always the smallest/largest element
Common Operations:
- Insert(value):
- Add at next available spot (left to right)
- Swap up while parent is larger (bubble-up)
- RemoveMin():
- Take root value (always smallest in min-heap)
- Move last element to root
- Swap down with smaller child until heap property restored
Remember:
- Parent of node at index i is at (i-1)/2
- Children of node at index i are at 2i+1 and 2i+2
- Always maintain complete tree shape
- For min-heap: Parent always smaller than children
- For max-heap: Parent always larger than children
Examples
- Ex. PUSH(3) on the stack [1,7,2] results in [1,7,2,3]
- Ex. X=POP() on the stack [3,8,1,8] results in [3,8,1] and X=8
- Ex. PUSH(5) on the queue [9,2,5] results in [9,2,5,5]
- Ex. X=POP() on the queue [5,4,7,1] results in [4,7,1] and X=5
Let's build a BST by inserting one letter at a time:
- Insert S (root):
S
- Insert T (greater than S):
S \ T - Insert A (less than S):
S / \ A T - Insert C (less than S, greater than A):
S / \ A T \ C - Insert K (less than S, greater than A, greater than C):
S / \ A T \ C \ K
Tree Analysis:
- Root node: S (depth 0)
- First level: A, T (depth 1)
- Second level: C (depth 2)
- Third level: K (depth 3)
- Internal path length = 0 + 2(1) + 1(2) + 1(3) = 7
- Inorder traversal: A, C, K, S, T (alphabetical order)
Let's build a min-heap by inserting one number at a time:
- Insert 8: Becomes root
8
- Insert 3: Less than 8, swap up
3 / 8 - Insert 5: Goes right, no swap needed
3 / \ 8 5 - Insert 1: Goes left, then swaps up twice
Before swap: After swaps: 3 1 / \ / \ 8 5 → 3 5 / / 1 8 - Insert 4:
1 / \ 3 5 / \ 8 4
Practice Problems
Problem 1 Junior
List the nodes at depth 5 of the binary search tree of the string "WATERBOTTLE" from left to right, capitalized.
Solution
The correct answer is: E, O, T
Let's build the BST step by step:
1. Insert W:
W
2. Insert A:
W
/
A
3. Insert T:
W
/
A
\
T
4. Insert E:
W
/
A
\
T
/
E
5. Insert R:
W
/
A
\
T
/
E
\
R
6. Insert B:
W
/
A
\
T
/
E
/ \
B R
7. Insert O:
W
/
A
\
T
/
E
/ \
B R
/
O
8. Insert T:
W
/
A
\
T
/
E
/ \
B R
/ \
O T
9. Insert T:
W
/
A
\
T
/
E
/ \
B R
/ \
O T
/
T
10. Insert L:
W
/
A
\
T
/
E
/ \
B R
/ \
O T
/ /
L T
11. Insert E:
W
/
A
\
T
/
E
/ \
B R
\ / \
E O T
/ /
L T
To find nodes at depth 5:
- Start at root (W) at depth 0
- Its child (A) is at depth 1
- T is at depth 2
- E is at depth 3
- B,R are at depth 4
- E,O,T are at depth 5
Therefore, the nodes at depth 5 in alphabetical order are: E, O, T
Problem 2 Junior
Given an initially empty queue and the following sequence of operations, what would be the next POPPED element?
PUSH(G), PUSH(I), PUSH(N), PUSH(N), POP(X), POP(X), POP(X),
PUSH(G), PUSH(E), PUSH(E), PUSH(R), POP(X), POP(X), PUSH(D),
PUSH(O), POP(X), POP(X), POP(X), PUSH(G), PUSH(O), PUSH(O),
POP(X), POP(X), POP(X), PUSH(G), PUSH(E), POP(X)
Solution
The correct answer is: O
Let's track the queue state after each operation:
- Initial queue: []
- PUSH(G): [G]
- PUSH(I): [G,I]
- PUSH(N): [G,I,N]
- PUSH(N): [G,I,N,N]
- POP(X)=G: [I,N,N]
- POP(X)=I: [N,N]
- POP(X)=N: [N]
- PUSH(G): [N,G]
- PUSH(E): [N,G,E]
- PUSH(E): [N,G,E,E]
- PUSH(R): [N,G,E,E,R]
- POP(X)=N: [G,E,E,R]
- POP(X)=G: [E,E,R]
- PUSH(D): [E,E,R,D]
- PUSH(O): [E,E,R,D,O]
- POP(X)=E: [E,R,D,O]
- POP(X)=E: [R,D,O]
- POP(X)=R: [D,O]
- PUSH(G): [D,O,G]
- PUSH(O): [D,O,G,O]
- PUSH(O): [D,O,G,O,O]
- POP(X)=D: [O,G,O,O]
- POP(X)=O: [G,O,O]
- POP(X)=G: [O,O]
- PUSH(G): [O,O,G]
- PUSH(E): [O,O,G,E]
- POP(X)=O: [O,G,E]
Therefore, the next element to be popped would be O.
Problem 3 Intermediate
How many nodes only have one child in the binary search tree for RAINSTORMS?
Solution
The correct answer is: 3
Let's build the BST step by step:
1. Insert R:
R
2. Insert A:
R
/
A
3. Insert I:
R
/
A
\
I
4. Insert N:
R
/
A
\
I
\
N
5. Insert S:
R
/ \
A S
\
I
\
N
6. Insert T:
R
/ \
A S
\ \
I T
\
N
7. Insert O:
R
/ \
A S
\ \
I T
\
N
\
O
8. Insert R:
R
/ \
A S
\ \
I T
\
N
\
O
\
R
9. Insert M:
R
/ \
A S
\ \
I T
\
N
/ \
M O
\
R
10. Insert S:
R
/ \
A S
\ /\
I S T
\
N
/ \
M O
\
R
Counting nodes with exactly one child:
- A has only a right child (I)
- I has only a right child (N)
- O has only a right child (R)
Therefore, there are 3 nodes that have exactly one child.
Problem 4 Intermediate
List the nodes in the bottom row of the min-heap (priority queue) of the word BLANKET from left to right, capitalized.
Solution
The correct answer is: N,L,E,T
Let's build the min-heap step by step. Remember in a min-heap, smaller values bubble up:
1. Insert B:
B
2. Insert L:
B
/
L
3. Insert A (bubbles up to root as it's smallest):
B A
/ \ → / \
L A L B
4. Insert N:
A
/ \
L B
/
N
5. Insert K (goes under L, then swaps with L):
A A
/ \ / \
L B → K B
/ \ / \
N K N L
6. Insert E:
A
/ \
K B
/ \ /
N L E
7. Insert T:
A
/ \
K B
/ \ / \
N L E T
In a min-heap:
- Each parent must be smaller than its children
- The tree is filled from left to right
- The smallest element is always at the root
Reading the bottom row from left to right: N, L, E, T
Problem 5 Senior
Find the internal path length of the binary search tree for KALEIDOSCOPE.
Solution
The correct answer is: 30
Let's build the BST step by step and track the depth of each node (root is at depth 0):
1. Insert K:
0 K
2. Insert A:
0 K
/
1 A
3. Insert L:
0 K
/ \
1 A L
4. Insert E:
0 K
/ \
1 A L
\
2 E
5. Insert I:
0 K
/ \
1 A L
\
2 E
\
3 I
6. Insert D:
0 K
/ \
1 A L
\
2 E
/ \
3 D I
7. Insert O:
0 K
/ \
1 A L
\ \
2 E O
/ \
3 D I
8. Insert S:
0 K
/ \
1 A L
\ \
2 E O
/ \ \
3 D I S
9. Insert C:
0 K
/ \
1 A L
\ \
2 E O
/ \ \
3 D I S
/
4 C
10. Insert O:
0 K
/ \
1 A L
\ \
2 E O
/\ /\
3 D I O S
/
4 C
11. Insert P:
0 K
/ \
1 A L
\ \
2 E O
/\ /\
3 D I O S
/ /
4 C P
12. Insert E:
0 K
/ \
1 A L
\ \
2 E O
/\ /\
3 D I O S
/ \ \
4 C E P
To calculate internal path length, sum the depths of all nodes:
- Depth 0: K (1 node × 0 = 0)
- Depth 1: A, L (2 nodes × 1 = 2)
- Depth 2: E, O (2 nodes × 2 = 4)
- Depth 3: D, I, O, S (4 nodes × 3 = 12)
- Depth 4: C, E, P (3 nodes × 4 = 12)
Internal path length = 0 + 2 + 4 + 12 + 12 = 30