Introduction to Data Structures
Programming is all about taking data and manipulating it in all sorts of interesting ways. Now, depending on what we are doing, our data needs to be represented in a form that makes it easy for us to actually use. This form is better known as a data structure. As we will see shortly, data structures give the data we are dealing with a heavy dose of organization and scaffolding. This makes manipulating our data easier and (often) more efficient. In the following sections, we find out how that is possible!
Onward!
Right Tool for the Right Job
To better understand the importance of data structures, let’s look at an example. Here is the setup. We have a bunch of tools and related gadgets (Figure 1-1).
FIGURE 1-1 Tools, tools, tools
What we want to do is store these tools for easy access later. One solution is to simply throw all of the tools in a giant cardboard box and call it a day (Figure 1-2).
FIGURE 1-2 Tools, meet box!
If we want to find a particular tool, we can rummage through our box to find what we are looking for. If what we are looking for happens to be buried deep in the bottom of our box, that’s cool. With enough rummaging (Figure 1-3)—and possibly shaking the box a few times—we will eventually succeed.
FIGURE 1-3 A rummager!
Now, there is a different approach we can take. Instead of throwing things into a box, we could store them in something that allows for better organization. We could store all of these tools in a toolbox (Figure 1-4).
FIGURE 1-4 Our metaphorical toolbox
A toolbox is like the Marie Kondo of the DIY world, with its neat compartments and organized bliss. Sure, it might take a smidge more effort to stow things away initially, but that’s the price we pay for future tool-hunting convenience. No more digging through the toolbox like a raccoon on a midnight snack raid.
We have just seen two ways to solve our problem of storing our tools. If we had to summarize both approaches, it would look as follows:
Storing Tools in a Cardboard Box
Adding items is very fast. We just drop them in there. Life is good.
Finding items is slow. If what we are looking for happens to be at the top, we can easily access it. If what we are looking for happens to be at the bottom, we’ll have to rummage through almost all of the items.
Removing items is slow as well. It has the same challenges as finding items. Things at the top can be removed easily. Things at the bottom may require some extra wiggling and untangling to safely get out.
Storing Tools in a Toolbox
Adding items to our box is slow. There are different compartments for different tools, so we need to ensure the right tool goes into the right location.
Finding items is fast. We go to the appropriate compartment and pick the tool from there.
Removing items is fast as well. Because the tools are organized in a good location, we can retrieve them without any fuss.
What we can see is that both our cardboard box and toolbox are good for some situations and bad for other situations. There is no universally right answer. If all we care about is storing our tools and never really looking at them again, stashing them in a cardboard box is the right choice. If we will be frequently accessing our tools, storing them in the toolbox is more appropriate.