The Startup
Published in

The Startup

Actual Practical Examples of Data Structures

“Write pseudocode to perform in-order traversal in a binary tree” (Hackr.io)

First of all, what is a binary tree?

As it turns out, there’s sort of a reason why almost all (99.98%?) of companies searching for software engineers and developers want someone who has a Computer Science background. Although there are debates as to whether these qualifications and indeed, even interviews, are the best way to assess a candidate, Computer Science teaches important concepts that as developers and engineers, you must understand in order to ace that interview.

Note: of course, I can’t speak -or write- on behalf of all companies. There are many companies out there who don’t need or require someone who knows the benefits of breadth first search vs. depth first search. I would encourage you to do this research on your own.

One of these fundamentals are data structures. As I wrote this, I hear the collective groan of software engineers and computer science students everywhere. For those of us who don’t have a background in Computer Science, our first exposure to data structures can be baffling, intimidating, and seemingly impractical. After all, why learn something that you can’t see or really use on a daily basis? It’s theoretical too, at the end of the day.

The reality is also that, try as you may, data structures and algorithms are going to be part of your learning process and technical career. I won’t be writing about algorithms today, though, because I think data structures deserve their own time in the spotlight.

As it turns out, when data structures are put into use, they can be challenging but also extremely interesting. That said, let’s examine what each data structure is and bring in a practical example.

For starters, what is a Node?

A node can be many things. In this instance, we’ll take the definition of nodes to be a structure in computer science that holds some sort of data value.

Singly Linked Lists

My simple diagram of a singly linked list.

A singly linked list is a common data structure, and likely the first to be introduced. There is a head and tail node, and pointers that lead one way to another node. It’s important to note that a singly linked list is not an array, because you can only access a node one way: from the head. You also can’t traverse back because a singly linked list is one directional.

One analogy of a linked list is a “train” (Stack Overflow). A train is comprised of a number of cars, which are attached to one another. A train also goes in one direction from Point A to Point B. If you wanted to remove a car, you have to identify the car and then detach the car (or node) from the cars both ahead and behind it.

Doubly Linked Lists

Another diagram of mine of a doubly linked list

Doubly linked lists are similar to singly linked lists, except that each node in the list has an arrow pointing the opposite way. Whereas in a singly linked list, you can only traverse forward, in a doubly linked list, you can traverse back and forth. The sole exceptions are the head and tail nodes, which point to null. This makes them much easier to understand and use. The caveat is, though, that they take up slightly more space which can be important for those of us required to keep track of space complexity.

One popular practical example: a back and forward option on your browser history. At some point you can’t go back (since it’s null or doesn’t apply) or forward because that’s where you’re at. Another anology I’ve seen is from Christopher Webb, who wrote about doubly linked lists in terms of the forward or backward options on a playlist.

Stacks

Stacks are essentially a pile (for lack of a better word) where data is stored in a last in, first out (“LIFO”) manner. You may have already worked with stacks if you’re a developer; when you have code that runs indefinitely, it’ll cause a stack overflow!

Credits to Suzy Hazelwood and Pexels

You can think of a stack of plates. The last one on the stack of plates is the first one that will get washed, whereas the first place you stacked will get washed last.

Queues

A queue works the opposite way of a stack. Most notably, in a queue, data is stored in a First In, First Out (“FIFO”) manner.

A practical example of a queue would be a “printer” (Stack Overflow). The first thing you click to print from your computer or mobile device is the first thing the printer will print.

By its very definition, a queue is what the rest of the world calls waiting on/in line, so if you’re ever confused about what a queue is, think of data waiting in a line.

Credits to Juniata College for this example.

Binary Search Trees

Note: a binary search tree is a type of binary tree.

My diagram of a binary search tree

At the top of the data structure, we have a node, or root, in this instance. From there, each node has at most two child nodes. It is important to note in a binary search tree, the node to the left is always a lesser value compared to the parent than the node to the right, which is always greater.

Admittedly, I couldn’t think of practical examples of a binary search tree, but Minh-Phuc Tran’s blog provided an example, which are “3-D Game Engines”.

Graphs

Graphs, like all structures before it, have nodes as well, but the nodes are connected by edges and vertices. It’s a graph, after all. Unlike the other structures, though, graphs also come with directions and weights. That is, there may be either values or directions assigned to the distances of vertices.

Credits to freeCodeCamp

One practical example is the Friends/Followers algorithm on social media. If Person A and Person B are friends, and Person B is friends with Person C, Person C might come up as a suggested “Friend”/ “Follower” to Person A.

Data structures and algorithms are by no means easy concepts to understand, so feel free to leave a comment if you can think of additional practical examples or have resources for fellow developers.

Thank you for reading, and stay tuned for future posts.

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Flutter, getting started

CS 373 Fall 2021 Week 8: Lauren Warhola

So you want to get Cypress running in Docker?

The Spooky Reduce Function

What’s Inside Milvus 1.0?

Understanding Liquity’s Stability Pool

Effective HTTP Caching — Part II

Introducing the $HELIOS token.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aimee Liang

Aimee Liang

More from Medium

Leetcode problem: Intervals Between Identical Elements

Box Stack Problem — Recursive way

LeetCode Patterns Adventure 20— Average of Levels in Binary Tree

Delete Operation for Two Strings