Shares

Introduction to Data Structures – A Beginner Friendly Guide

A solid introduction to data structures can make an enormous difference for those that are just starting out. The world of data structures and algorithms, for the unwary beginner, is intimidating to say the least.

I have written this post to alleviate some of the anxiety and provide a concrete introduction to provide beginners with a clarity and guide them in the right direction.

In this post, we will first examine the most frequent questions beginners have when inquiring about data structures.

1. What is a data structure?
2. Why you need to know data structures?
3. How do you learn data structures?

If you are a data structures veteran, you probably don’t need to read this post. But if you do decide to read it, I would be very interested in hearing about your thoughts and opinion. If you are willing, please share them with me!

Please note that this post will not cover algorithms.

Even though data structures and algorithms often go hand in hand, I want to keep this post exclusively focused on data structures. I will be writing an entirely separate post on introduction to algorithms, as well as a follow up post on both data structures and algorithms.

When I first started learning data structures and algorithms, I didn’t know where to start.

Not knowing where to start not only results in anxiety.

It also results in bright individuals clutching aimlessly at straws without a clear goal or purpose.

This sets aspiring developers back from reaching their true potential.

Every serious programmer and/or software engineer must have a strong understanding of data structures.

Why?

Because every piece of software processes, handles and displays data. Therefore, data structures are the building blocks of every substantial piece of software.

I will be updating this post continuously in the hope that it will eventually become a great resource for people that are starting out with data structures.

Prerequisites

Despite this being an introduction to data structures, I still require readers to have a solid foundation in programming. Even though this is an introduction to data structures, the very topic itself demands a relatively solid fundamental knowledge of programming.

You don’t have to be a master, but I expect you to know the basics such as the difference between an `int` and a `float`. Basic control flow and so on.

Here is a more specific list of requirements. You should be comfortable with the concepts listed below.

• Basic understanding of primitive data types such as `int`, `double`, `float`, `char`, etc.
• Variables.
• Strings.
• Basic Operators (+, -, *, /, <<, >>, etc.).
• Basic Control Flow (If, if-else, switch).
• Loops (for, while, do while loops).
• Arrays.
• Basic understanding of functions and classes.

Having a working knowledge of the topics below will help you greatly but is not an absolute requirement.

• Object oriented programming
• Polymorphism

If you remain on this page, I am going to assume that you have a decent working understanding of the basics of programming.

Okay then, let’s move onto examining the definition of a data structure.

What is a Data Structure?

A data structure is simply a place to store data in an organized manner. We have many different forms of housing, right? Homes, apartments, condominiums, penthouses, etc. Just as there are diverse types of residential buildings, each catering to a specific target, we have distinct types of data structures.

When we think of data structures, people usually think of the more complex user-defined structures such as linked-lists, b-trees and whatnot.

For your information, we also have primitive data structures, ones that are declared using reserved keywords.

For example, if we want to store multiple series of “related data”, an array might be a suitable data structure. If we are storing a large number, it might be better to use a `long` (8-byte Integer – in Java) rather than an `int` (4 bytes).

We primarily have two main data structure types

1. Primitive data structures. E.g. int, double, float, etc.
2. User-defined data structures. E.g. Strings, Array Lists, Stacks, Queues, etc.

If you don’t understand what they are, I will explain each of the sub-categories in one of the upcoming section.

Now that we know what a data structure is, we need to address the issue of why you, as a programmer or software engineer, need to learn data structures.

Why Do I Need to Learn Data Structures?

As I mentioned earlier, data structures are a means of storing data in a organized manner.

In your job as a programmer, will you be working with data?

In most, if not all cases, you will be working with data in one form or another.

Having a solid understanding of data structures equips you to make informed decisions that leads to effective data processing, optimized performance and ultimately, a positive impact on the business’s performance (whether in generated revenue or in savings), all of which are valuable to both yourself and your employer.

Caravans are a form of mobile housing. Apartments take up a lot less ground and house multiple families because they are built upwards.

Just as several types of houses are designed to specifically address different problems and have their own set of pros and cons, so it is with data structures.

There is no one size fits all.

Choosing the wrong data structure for a job is like attempting to store boiling water inside of a glass beaker. Expect it to crack under the pressure.

The next question you may ask is

Does Every Programming Job Require Knowledge of Data Structures?

Almost every programming job (if not all) requires some knowledge of data structures. Therefore, every developer should know and learn data structures.

Without a strong knowledge of data structures, developers are left to blindly rely on the built in API implementations of each data structures. You may be doing something that is highly inefficient without you even knowing, until that fateful day that your server is unable to handle the sheer volume of data.

There are (were) certain jobs in the development industry that requires little to no knowledge of data structures.

For example, front-end web development.

Oops, did I neglect to tell you that times are changing? In the past, front-end developers got away with having minimal knowledge of data structures.

But with more and more people now having access to the internet, the amount of data that is transferred has increased dramatically.

It is no wonder that a solid understanding of data structures is required on the front-end as well, in order to process, handle, manipulate and display large result sets.

This is why data structures and algorithms workshops are being taught on Front End Masters.

Please note that I am not bashing front-end developers. They are a great asset to every development team that develops products with a GUI.

Fortunately for front-end developers, JavaScript provides built in Hash Tables in the form of JavaScript Objects. E.g. `var hashTable = {};`.

Arrays in JavaScript act as both stacks and queues/dequeues (double ended queues), thanks to the `shift()` and `pop()` which allows consumers to remove and return the first and last element in an array respectively. Of course, adding data to the start and end is possible as well.

Being well-insulated, as I mentioned before, does come with a price. When it comes to data structures,

Ignorance is not bliss

Having knowledge of how each data structure works will allow you to make informed decisions, which will result in some critical optimization, especially if you work in mission critical fields.

What is a Primitive Data Structure?

A primitive data structure is a way of storing data that is pre-defined by the system.

One great litmus test for primitive data structures or primitive types, when working with an OOP language involves checking whether a keyword is used to declare the variable that is used to store that data.

If so, then chances are, you are looking at a primitive data structure.

In Java, primitive data structures are declared using the following keywords (source: docs.oracle.com)

• byte
• short
• int
• long
• float
• double
• boolean
• char

Heads up: for each language, the keyword for defining the primitive data structures may vary.

What is a User-defined Data Structure?

If you come from an object-oriented programming background, it is pretty much any data structure that is created using classes. For those that don’t know what a class is, it is a blueprint for creating a specific object.

These objects are created from blueprints, which in turn, is designed and developed by other developers. User-defined data structures utilize the built-in language features and primitive data structures as its base.

General rule of thumb: If it does not have its own keyword, but must be instantiated, the medium of storage is likely a user-defined data structure.

One classic example of a user defined data structure is the String. However, a String is often not associated with data structures.

A data structure is a specialized format for organizing and storing data. A string uses an array as an underlying data structure to store bytes of characters. Therefore, it is a data structure, because it is a wrapper for an array of characters. Strings are all around us. The very words that you are reading on this screen is stored using strings. Without the string data structure, we will be back to using character arrays, just as in the good old C programming days.

Examples

Other examples of user-defined data structures include

• Trees
• Graphs
• Hash tables
• Lists
• Sets
• Maps
• Queues
• Stacks

If you have no idea what these data structures are, don’t worry about it. For now, the most important thing is to understand that the word data structure is a fancy term for a container that is used to store data. And that data structures can either be pre-defined by the system (primitive) or defined by a User (either yourself or a third party).

How to learn Data Structures

Okay now that we know a little about what data structures are, and some of the basic data structure types, let us dive into how we can learn this monstrosity.

I wrote this introduction to data structures, because the topic is broad and intimidating.

Because I have (am) going through the process of learning data structures and algorithms, I think my opinion can be valuable to you.

The first real piece of advice I want to give to you is this.

Make it your Number One Priority to Learn Data Structures

Because this is not an easy topic, it is all the important that you consistently apply yourself. By consistently, I am talking about devoting at least an hour on three out of the seven days.

This is my personal definition, but I presented it to you to highlight the importance of setting specific time-slots during the week to devote towards learning data structures.

Unless you make learning data structures your number one priority on your to-learn list, you are most likely not going to consistently work on it.

Like anything that poses somewhat of a challenge the following rule applies to data structures.

Use it or lose it.

I don’t recommend proceeding unless you are prepared to make that commitment. Otherwise, chances are, you will just waste your time.

This is just my humble opinion. If you need second opinions, I recommend doing a google search or asking on sites such as Quora.

We want to bite off only as much as we can handle.

Don’t throw yourself into the deep end, especially if you don’t yet know how to swim. You will end up drowning.

These little steps will eventually turn into giant strides.

Conceptualize

For example, when learning about the singly linked list, it is important to conceptualize it in your mind. For example, you can liken the linked list to a train.

Why? Because a linked list is made up of nodes, with the first and last node being labelled as the head and the tail respectively. The head node is similar to the first carriage of a train. We know we are at the end of a linked list when the next node pointer points to `null`. In another word, when we reach the last carriage, that last carriage is the head and points to nothing.

Ever seen those old school CD players, where you can only move up/down a track? Each track has a pointer to the next and previous track. That also sounds a lot like a doubly linked list to me.

You see?

It becomes easier to pick how a data structure works if you conceptualize it and use real-world items or examples to compare the data structure with.

Want another example. For the stack data structure, think of a stack of cards. You draw from the top of the deck. In most cases, you only interact with the card at the top of the deck of cards.

What is the use of just reading about data structures if you don’t know how to use or implement your own version of it.

Believe it or not, programmers need to build up muscle memory! Implementing these data structures is a fantastic way of working out those coding muscles.

It will not only solidify your knowledge, it will also simultaneously boost your problem-solving skills and deepen your understanding of the programming language that you used to implement the data structure.

Right now, I am working feverishly to update my posts on the basic data structure. I will walk through the steps, thought process behind the implementation in the updated posts on the basic data structure.

Things to Avoid When Learning Data Structure

Coding before Processing

You will thank me once you have gone through the process of learning data structures.

Some people (myself in the past included) start coding right away. They “process” the information as they code and make adjustments when needed.

While sometimes, this may yield positive results, in most cases, I recommend processing the information, and coding after you intimately understand the subject matter, which in this case, is a specific data structure.

Avoid looking at code until you conceptually understand the data structure

To conceptually understand the data structure, you first need to know

• What makes that data structure special.
• Its key features.
• Underlying assumptions that it operates on.

If we take it all the way to the highest level, in the broad field of software engineering, before writing a single line of code, it is important to gather the information mentioned above.

These pieces of information are what makes up the meat and bones of the software, which in this case, is a linked list implementation.

Learning data structures is like learning a new language. You can’t learn how to write a thesis without knowing your ABC’s.

By skipping ahead, you are setting yourself up for failure and disillusionment. Don’t do that to yourself!

Yes, I understand how tempting it is to jump right into the complex data structures. Ones that are used extensively in forming the backbone of databases, operating systems, etc.

However, I STRONGLY ADVISE you to master the fundamentals first before moving onto the “cool data structures”.  Trust the process and grind.

Your blood, sweat and tears will eventually pay off. If you want to be discouraged, and give up on learning data structures, or stunt your growth by losing sight of what you need to learn, feel free to skip ahead. But don’t say I didn’t want you.

Which Data Structures Should I Learn First?

Below is my own personal curriculum/outline for those that are learning data structures for the first time.

• Built-in arrays.
• Stacks.
• Queues and deque (double ended queues).
• Sets.
• Maps.

Focus on each of the items listed, one at a time. After mastering each item on the list, it is imperative that you

One of the best ways to solidify your knowledge and become a better programmer is by writing code.

Build your own implementation from scratch using all the programming languages that you know. If you only know a single programming language, no need to feel embarrassed. In fact, stick with that language until you are extremely confident in that language before learning a new language.

I currently have posts on the linked list and stack data structure, but they are currently in serious need of a rework, which is what I am working on right now. As a matter of fact, I plan on writing detailed guides to each of the data structures mentioned above.

Once you have written your own implementation, trust me, you will never ever forget about that data structure and its properties.

After these basic data structures, you will be ready for the more complex data structures. I will write an additional post on how to work through the intermediate data structures in a future post.

For example, the binary search tree utilizes the concept of nodes, which you cover in linked lists. Without understanding what a node is in a linked list, learning the binary search tree will become that much more difficult.

It is like learning how to run without first learning how to walk.

Remember that this is an introduction to data structures.

Therefore, to prevent overwhelming you with an unnecessarily large list of to dos, start off by learning the data structures that I listed above.

Can I learn Algorithms Simultaneously alongside Data Structures?

Sure! Why not? As a matter of fact, I think learning algorithms and data structures go hand in hand. That is why when you search “data structures” on google, more often than not, the accompanying auto complete suggests “and algorithms”.

However, this is an introduction to data structures, so we will not talk about algorithms here.

My short answer is: YES. But like most things, only bite off as much as you can chew.

Even if you are cruising through the current data structure you are studying, don’t let yourself off the hook until you pass the general litmus test of mastery.

If you truly understand the data structure, you

SHOULD be able to build the implementation from scratch off the top of your head.

By top of your head, I mean without referring to any external resources. Just you and your IDE, without the help of the internet (except for maybe possibly looking up built-in API methods, if you are not familiar with the language).

Because this is an introduction to data structures, I do not recommend the following book:

Don’t get me wrong. I love this book and have a copy of it myself. However, I believe that if a beginners were to pick it up, expecting an introduction, they are in for a rude awakening!

Despite its thoroughness, the book is heavy (both literally and figuratively). The only group of beginners I would recommend this book to are those who have a strong background in Mathematics.

For beginners, I recommend the following resources.

Online Resources

• Introduction to basic data structures: This is a great introduction to basic data structures. I suggest you read this first before moving onto anything else.
• 10 Common data structures explained with videos: The content is short enough to go through quickly. So, if you are commuting or travelling somewhere, this is a great link to pull out and review. For now, the basics is more than enough since this is only an introduction to data structures.
• Data Structures Succinctly: The readings are not too overwhelming and they cover most of the basic data structures recommended in this post. Going through this, reviewing repeatedly will help you solidify your fundamentals and give you a good workout.
• Java Data Structures and Algorithms: If you are learning data structures in Java, this GitHub repository has tons of useful source code for reference. You will become a better programmer by reading, and applying what you see from here. Needless to say, that although the resource is free, you have to make the effort to sift through the resources, analyze and write some code.
• Coding Interview Data Structures: I posted it above, but I will post it again in case you missed it. I will let the contents do all the explanation. If you never came across this page, you will thank me. Great resources and also abundant in quantity. Going through it all will take a lot of time, so focus on the key data structures that I mentioned earlier before proceeding onward.
• Tutorialspoint: If you are into clear, concise summaries on data structures, this place is a good place to start. Aside from data structures, they have resources for almost anything related to programming. Tutorialspoint is a great place to ease yourself into a specific programming-related topic. Data structures is no exception to this rule.

Books

• Computer Science Distilled: This book is very short for a book on computer science (180 pages only!) and covers both data structures and algorithms in laymen terms. It also provides very practical steps towards solving problems, along with illustrations to help you understand concepts. Although only a single chapter is dedicated to data structures, the book teaches readers to think like a software engineer. Each example is designed to draw out your problem-solving skills so that your ability to learn, adapt to, and solve new problems is being built with each problem solved. Ultimately, this book teaches you how to adopt the right mindset when learning data structures, which is something that the short online tutorials cannot replicate as easily.
• Cracking the coding Interview 189 Programming Questions and Solutions: Even though this is a book on coding interviews, the book is rich in content regarding basic data structure questions that pop up during interviews. The main reason why I recommend this book is because it teaches you something more than the topic of data structures: the thought process behind the implementation details of a specific data structure. The book is rich in information on not just coding interviews, but ways to approach problems. It is ultimately a collection of case studies that teaches readers how to think like a software engineer.

This post is a perpetual work in progress and I want to make this post the most useful introduction to data structures that you can find. I know that this is a tall order, but with your help, hopefully this becomes a reality in the distant future.

Please provide me with suggestions and feedback on how to make this post more useful, information and less intimidating.

The whole goal of this post is to springboard beginners in their mission to learn data structures.