Data Structures 101: A K.I.S.S Guide to Data Structures for Beginners
In this post, I’ll be explaining what data structures are and why they exist. The goal is to break down the idea of data structures into tiny, K.I.S.Sable parts.
I’ll be using several examples and scenarios to create simple illustrations of one of the most important concepts in computer science.
Ready? Let’s go!
To begin, I’ll present 2 problems to you and you will give me your best answer.
You just downloaded a new song onto your computer and you need to find it. All you have to do is open your downloads folder, look through it and find the song.
There’s only one problem, you haven’t organized your computer in a while and now you have 1,653 files to sift through!
New plan: Sort through the downloads folder using your computer's sort-by function, but which do you pick?
- Sort by name
- Sort by kind
- Sort by date added
- Sort by date modified
What is the best way to arrange the files in your download folder so you can find your song quickly and efficiently?
You're on a self-help high after taking a Tony Robbins course and now you want to do better. The first step is to read all those self-help books you got in college. You will use a reading schedule and it will be fun.
That’s easy enough!
You open your closet to begin only to find 20+ books scattered in a corner, (you don’t remember them being this much) You decide to organize them but where do you begin?
- Do you arrange them alphabetically, by year, by size, by genre?
How do you categorize your books and fit them into your schedule to begin your self-help journey?
Data Structures — What are They?
Every problem we face is a data problem.
The two problems I described above are just 2 out of the thousands of data decisions we have to make every day.
The data in the two problems above have to be arranged in a certain way, to allow you easily find and retrieve what you need.
So the question is;
‘How do I organize a problem to begin solving it?’
To begin to solve a problem, you must understand what it is and how it works, but this understanding only comes when you organize the problem into a more meaningful and solvable form.
This is the idea of data structures.
The data structure is a fundamental concept in computer science that describes how data is organized.
Just like a jug holds water and a pen holds ink, a data structure is simply a container that holds and organizes data in a specific way.
The goal of data structures is to make data more meaningful and useful when solving problems.
There are various kinds of data structures and like Disney princesses, each one has its unique story, its own strengths, and weaknesses which make it amazing at solving certain problems and terrible (inefficient) at solving others.
No data structure is perfect and it is your duty as a software engineer to determine the best (most efficient) data structure to use in solving a problem.
Why Use Data Structures?
So why use data structures anyways? Why not just starting coding the problem away?
Answer: Data structures exist to help us solve problems quickly and efficiently.
Data structures help us organize data and design solutions that work quickly (time-efficient) and use up ONLY the space needed (memory-efficient)
Think of it like this, if you were moving and you needed to organize your belongings into boxes, would you label each box by name or by color?
Although both options achieve the same goal of categorizing your belongings, only one method is most efficient in a case when you need to, for example, find your ‘kitchen stuff’ faster — organizing by name.
Thinking of how you would organize your stuff to allow for easy access in the future is what it means to ‘design a solution’.
⛔ Freebie Alert
Are you wondering what language to learn in 2021?
👉 Here’s a guide on the top 10 programming languages you should be learning right now
Picking the Right Data Structure
Recall that to solve a computing problem efficiently, we need to organize the data in a meaningful way.
This is achieved using the RIGHT data structure.
The RIGHT data structure is determined by how fast or slow a data structure allows an operation (algorithm — a series of steps) to be carried out.
The speed of an algorithm depends on the data structure used to store data.
In other words, if you want to perform a specific operation, you must pick a data structure that optimizes that operation instead of one that slows it down.
Array and Linked Lists — A Case Study
For example, while an array is perfect for accessing data randomly, it is pretty shitty (slow) when adding or removing elements from its structure.
On the other hand, linked lists work wonderfully well when adding or removing elements but struggle when it needs to search through itself.
NB: There are many operations performed by data structures but the most common are access, insertion, traversal and deletion.
☝️The speed (rate of growth) of an algorithm can be described using the term Big-O notation.
Although we will not be discussing the Big-O notation in this post, it is important to note that the Big-O notation is an important concept in computer science that tells us how fast or slow an algorithm runs.
Types of Data Structures
Here is an overview of some of the most common data structures in programming and computer science;
This is one of the simplest and most common data structures. An array is a data structure that stores data in contiguous locations.
This means that the elements of an array are stored sequentially (side by side) — Think a grid structure. Due to this pattern of arrangement, each element of an array can be easily identified by its ‘index’ and easily accessed in most cases.
A linked list is a data structure that stores elements in containers called nodes. Each node is connected to the next by pointers which could run in one direction (singly linked lists) or in both directions (doubly linked lists)
Unlike arrays, data in a linked list are arranged randomly rather than sequentially. Due to this pattern of arrangement, the exact location of a node is not easily known at any given time.
In a stack, elements are stacked one on top of the other. Here, data is arranged in a last-in, first-out (LIFO) manner.
This means that the last element added to the stack is the first and easiest to access. For example, in a plate dispenser, the last plate added is the first to get dispensed, while the first plate added will be accessed last.
A queue works differently but similarly to the stack (confused?).
Here, although data is somewhat sequential, the elements are arranged in a first-in, first-out (FIFO) manner.
This means that the first item to be added will be the first and easiest to access. Imagine an actual line at an Apple store, you risk everything to be the first on the line so you can be the first to get the new Apple release. Get it?
The hash table data structure stores data in the form of key-value pairs. In this linear data structure, a key is generated which associates itself(maps) to a unique value.
The system of organizing data in key-value pairs results in a very fast way of finding data efficiently. This is because since each key is mapped to a unique value — once we know a key then we can find the associated value instantly.
Some examples of processes that use hash tables are;
- Usernames mapping to profiles on social media
- Student ID mapping to student records
- A domain name system (DNS) mapping to an IP address
Due to its speed and efficiency, the hash table is often used in processes that require the look-up/search functionality
Trees are one of the most important and most complex data structures. They are non-linear data structures that store data in a hierarchical manner.
A tree is a collection of elements called nodes. These nodes are connected to each other by edges. Each node contains a value or data, and it may or may not have a child node.
Much like a biological tree, a tree in programming has leaves, a height, a depth, and some extra features such as a parent, child, and sibling node.
The above data structures all have one thing in common — they all store data! But, what separates one from the other is HOW they each store data.
Choosing the right data structure for your project depends on one question;
How do I plan to STORE and USE this data in the long run?
A thorough understanding of the pros and cons of each data structure will help you answer this question AND determine the best data structure for your project.
Thank you for reading☕!
With ♥️ From DevSwitch
Here are some freebies to help you get started in your tech journey