What You Should Know About Data Structures

Data structures. Whether you’re new to computer programming or have many years already under your belt, you’ll hear about data structures all the time. Understanding data structures is critical if you want to become a good programmer or even have a program capable of carrying out basic operations. After all, it’s been said that programs= algorithms+data structures.

There’s a lot to understand about data structures. You’ll want to know what they are, why they’re important, and what the main ones are. This understanding will help you whether you’re worried about preparing for interview questions, or you are looking to build a solid foundation for the rest of your coding education. The bottom line is, if you want to be a successful programmer, you need to understand data structures.

What is a data structure?

A data structure in computer science is a system used to store data, keep it organized, and enable easy modification and access. Put simply, a data structure refers to a group of data values, how they relate to each other, and the operations or functions that can be carried out on them. Remember it this way:

  • Programs= algorithms+data structures
  • Data structures=related data+allowed operations on that data

In other words, effective and efficient programs all start with solid data structures. In fact, data structures are likely the most fundamental and building-block subject in computer science.

Why are data structures important?

Data structures are the foundation of any solid computer program. Without the right data structure in place, it’s very difficult to build an efficient program. Computer programs rely on data, and lots of it. Making sure you get it right at the data structure level is key if you hope to build a program that runs well. The right data structures organize many data types logically and in a way where the data can easily be accessed, modified, and configured.

What’s an easy way to understand data structures?

Three examples may help you better understand data structures. First, think of a dictionary. In a dictionary, words are organized alphabetically. This enables you to search for and find a word quickly and efficiently.

Next, a city map. Organized into geometries, a city map has scales and directions and it makes it easy to search effectively for a landmark. With a city map, you can also find a route from one place to another.

A business cash-in-cash-out statement. These statements often use tabular schema, or a table. Much like certain data structures, aggregating and extracting data is easy when it’s in organized columns.

What are the different data structures?

Data must be organized in different ways depending on the data type and what you want to do with it. In order to effectively organize data and build a program, you must understand the different types of data structures and how they work. In a broad sense, data structures are often sorted into linear data structures and non-linear data structures. Here are some of the main types of data structures:

Linear Data Structures

  • Arrays
  • Stacks
  • Queues
  • Linked Lists
  • Hash Tables

Non-linear data structures

  • Trees
  • Graphs
  • Tries

Linear Data Structures

Arrays

Arrays

Arrays are the most fundamental and basic data structure. If you want to build other structures like queues, stacks, or hash tables, it’s best to first know how to build arrays. An array is a group of similar data stored together neatly in a memory location. Each piece of data in an array is given a positive numerical value. This is called the index. Index numbers make it easy to determine the position of each data piece or element by adding a number to a base value. The base value is usually the first element’s memory location. Many programming languages give the starting index of an array the value of 0.

Stacks

Stacks

If you’ve ever used the Undo function on your computer, you can thank the stack data structure. Stacks work on the Last In First Out (LIFO) method. Picture a stack of books. The last one you place on top of the stack is the first one you can remove. In order to safely remove the bottom book (the first one you laid down), you would need to pull away all of the books on top of it.

Queues

Queues

Queues and stacks are alike in that they both keep elements stored sequentially. The difference is that Queue uses the First in First Out (FIFO) method instead of the Last in First Out (LIFO) method. To understand a queue, think of people lined up to get on a rollercoaster. The first person in line is the first person to exit the line and get on the ride. The last person in line will be the last one out of the line.

Linked Lists

Linked Lists

Just like stacks and queues, a linked list is a linear data structure. A linked list is different, however, in a number of ways. Internal structure, memory allocation, and the way you conduct basic operations all differ. Think of a linked list as a sequence of elements where each element is linked to the next.  Each node is comprised of information including data and a pointer.

Linked lists can contain anything including strings, characters, or numbers. They can be unsorted or sorted. And they can contain duplicate elements or all unique elements. In an array, the elements are indexed and you can instantly get to an element. In a linked list, though, you have to start with the head and work your way through until you get to the desired element. It takes linear time, so it’s quite a bit slower. The advantage of linked lists is that you can insert and delete elements to the beginning of the list in constant time, or very quickly.

Hash Tables

Hash Tables

Hash tables are a data structure that can be implemented as a linear or non-linear data structure. Often, they are implemented as a linear data structure. Hash tables are used to map keys to values. If you had a list of names, for example, a hash table might be used to identify a person’s phone number using their name. Usually, hash tables are built using arrays.

Non-Linear Data Structures

Trees

Trees

Whereas arrays, stacks, queues, and linked lists are all linear data structures, a tree is a non-linear hierarchical data structure. Think of it like a company where the CEO is the root and the department heads are the first nodes. Those department heads are over other employees who report to them, creating additional nodes. Trees are structured so that there is one edge (or connection) for each parent-child node relationship. There must be only one possible path from a root to any given node.

Trees can be one of the more complex data structures and they are often used in artificial intelligence or other intricate problem-solving systems.

There are a number of different types of trees, including binary tree, binary search tree, AVL tree, balanced tree, red black tree, 2-3 tree, and N-ary tree. Binary tree and binary search tree are typically the most often used.

Graphs

Graphs

A tree is essentially one form of a graph. What separates a graph from a tree is that in a graph, there are no rules that dictate the connection among the nodes. A graph can be seen more as a network. Just like a graph in mathematics, graphs in coding involve vertices. Nodes can be seen as points on a graph, but each node is often connected to one or more others, creating a web-like shape.

The types of graphs include an undirected graph and a directed graph. They can be represented using an adjacency matrix or adjacency list.

Tries

If you’ve ever been given auto suggestions while using a search engine, you’ve seen a trie data structure at work. Tries are effective at solving string-related problems. Tries are also called “prefix trees” because they are prefixes of longer paths.

Final Thoughts on Data Structures

As you can see, data structures can become incredibly complex. We’ve really only scratched the surface here. Each data structure often has a number of different applications and uses. That said, having a basic understanding of data structures is a critical first step to becoming a good programmer.

Once you learn data structures, you’ll have an easier time learning different programming languages as you’ll have a solid base understanding to work from. So whether you’re implementing data structures in Java or Python, or studying for interview questions, understanding the most common data structures and how they work is the first step.

Take one of our courses to dive deeper into data structures and get the foundation you need to excel as a computer programmer.

Recommended Reading

What Is Data Science?: A Beginner’s Guide

A List of Programming Languages Every Programmer Should Know (Or at Least Know About)

What are Data Types in JavaScript? – JavaScript 101

See also:

Related posts