CS50 Data Structures Overview


CS50 Data Structures Overview

Michael
Today we're diving into data structures—how we organize data in memory to solve problems efficiently. We'll distinguish between abstract data types, like queues and stacks, and their actual implementations in C.

Hope
Queues sound familiar from real life—like lines at a store. What makes them special in computing?

Michael
Queues follow FIFO: first in, first out. Think of people lining up for cookies. The first person in line gets served first. In code, we enqueue to add and dequeue to remove. That fairness is key for many applications.

Hope
So queues are great for fairness. But what about stacks? I've heard of push and pop.

Michael
Stacks are LIFO: last in, first out. Imagine stacking plates—you take the top one first. Your email inbox often works like a stack: newest emails on top, so you see them first. Push adds, pop removes.

Hope
I can see how that's useful for undo operations or function calls. But implementing these with arrays seems limiting—fixed size.

Michael
Exactly! Arrays are contiguous and have a fixed capacity. If you need more space, you have to copy everything to a larger array, which is O(n) and wasteful. That's where linked lists come in.

Hope
Linked lists use pointers to connect nodes, so they can grow dynamically. But they use extra memory for those pointers, right?

Michael
Right. Each node holds data and a pointer to the next node. Inserting at the beginning is constant time—no copying needed. But searching is O(n) because you have to traverse from the start.

Hope
So there's a trade-off: you save time on insertion but lose on search. Can we get the best of both?

Michael
That's where binary search trees come in. Each node has two pointers—left and right—keeping everything sorted. If balanced, search becomes O(log n), like binary search on an array. But if you insert in sorted order, it degenerates into a linked list.

Hope
So balance matters. What about hash tables? I hear they're used for dictionaries.

Michael
Hash tables combine arrays and linked lists. You use a hash function to map keys to array indices. Ideally, each index has a short linked list, giving near-constant time search. But in the worst case, all keys collide, and it's just a linked list.

Hope
So it's all about the hash function. A good one spreads keys evenly. But what about tries? I've heard they're even faster.

Michael
Tries are trees of arrays. Each node is an array of 26 pointers (for letters). Searching for a word takes time proportional to its length—constant time, regardless of how many words are stored. The catch is memory: lots of null pointers waste space.

Hope
So tries give incredible speed but at a huge memory cost. Real-world systems often choose hash tables as a compromise. Like at Sweetgreen, they hash salads by first letter—mostly O(1), but busy times cause collisions.

Michael
Exactly. Every data structure has trade-offs between time and space. Understanding these lets you pick the right tool for your problem. That's the power of data structures.

Ask anything
We've read this whole study set, so questions about it get the best answers.