What is a data structure
When you put things away, what do you put them in? Sometimes you line them up, sometimes you drop them into a drawer split into compartments, and sometimes you hang them in a branching shape. Even the same things are easier or harder to take out depending on where you put them. Information is no different. Holding information has a shape too, and in this lesson we call that shape a container.
Same information, different shapes
Say you have the names of five friends. How could you hold them? You could stand them in one long line, put them in a table split into cells, or split them from top to bottom like branches and hang them there.
The contents are the same, but the shape that holds them is completely different. In this lesson we call that shape a container; in computer science it is called a data structure.
Hold the same names in a line, a table, and a branch in turn. The contents stay the same, but the shape changes.
Even as the shape changed, the names inside stayed the same. Still, each shape was probably good for something slightly different.
A line shows the order at a glance, a table makes it easy to point at one cell, and a branch shows who is above whom. So which shape should we choose?
A container that fits the job
You do not just use any container. First you think of the job you want to do. If you want to go through everything in order, holding it in a line is good. If you want to find something the moment you say its name, a table is better.
If you want to work out who is under whom, a branch fits well, like a company's org chart or the folders on a computer. Even for the same information, the container that fits changes with the purpose.
Tap the job you want. The container that fits it best lights up and tells you why.
Each time you tapped a different job, a different container lit up. There is no container that is always the right answer; what you want to do decides it.
If order matters, a line; if finding fast matters, a table. But there is one more thing: no single container does every job equally well.
Some jobs fast, some slow
Say you have picked one container. Among the actions you do with it, some are very fast and some are quite slow.
Think of a line. Sweeping through it from the front is fast. But to squeeze a new name into the middle, every name after it has to move over one place, so it is slow. Each container has jobs it is good at and jobs it is clumsy at.
Pick a container, then tap an action. Even in the same container, fast and slow actions split apart.
Even within the same container, the fast jobs shown in green and the slow jobs shown in red split apart. So when you choose a container, you first look at what you will do most often.
A container that is fast at the jobs you do often is a good choice. With only ten names the difference is small, but with a million names the same job can finish in the blink of an eye or take a long time. Even for the same data, how you hold it decides whether it is fast or slow.
A taste of the main containers
Let's get a small taste of the containers we will meet often, just their names. The container that holds things in order like a line and lets you take one out by number is an array. The one that holds pairs with a name tag is called a dictionary.
The one you add to and take from at one end only is a stack, and the one where things line up and the first in is the first out is a queue. The branching shape that splits from top to bottom is called a tree. For now, remembering the names and the feel is enough.
Tap through the main containers. Each one shows a one-line introduction and the jobs it fits.
It is fine if the names feel a little strange. That there are many kinds of containers, and that each fits certain jobs, is enough to hold onto for now.
Each one is covered in detail in the lessons that follow. Before that, let's gather what we have learned into one line.
Summary
In short: holding information has a shape too, a container. Even the same data can be held in different shapes, such as a line, a table, or a branch.
You choose a container to fit the job, and a single container is fast at some jobs and slow at others. So choosing well makes a program faster. Even for the same data, how you hold it decides the speed; that is the heart of it.
Tap the key points in order to review. (information has a shape → a container that fits → fast and slow tasks → a taste of containers)
Now you know that information has a shape too. You have gained a bit of an eye for choosing containers.
In the next lesson we look closely at the most basic container, the array, and at the secret of its speed: number every slot, and you can take any item out the moment you name its number.