title: Data Structure Basics pt.1 description: > Introductory notes on the basics of data structures. categories: posts
Table of Contents
As applications get more complex, and data grows richer, we face three common problems:
Data structures help by keeping data organized efficiently.
At its core, a data structure is an efficient data arrangement. They are everywhere in our daily life. We simply think of them as collections of information. For instance, a shopping list, a directory, a dictionary, a flight schedule.
Since raw data usually fits a data structure we need to study at least the most recurrent ones. Before learning a bit about their characteristics lets see what implementation and interface are, though.
Depending on the context, implementation refers to either the code representation of a data structure. Or the definition of the algorithms used on a given data structure.
An interface represents the set of operations that a data structure supports. It may include the type of parameters these operations take, as well as their return type.
Since data structures are, by definition, data centered lets clarify some concepts which will make further explanations brief yet loaded with content.
We define data according to its characteristics. A good definition should be:
Data objects are mere representations of objects that have data. Data types refer to different ways to classify data objects according to its data and operations it can handle.
Even though there are many data types they are broadly divided into:
From now on we'll focus mostly on abstract data types.
When designing data structures we should keep in mind its:
Most data structures implement these 5 fundamental behaviors:
Although we shouldn't be taken by surprise if they don't.
Since speed is part of what makes data structures efficient they don't persist. That is, they are created and hold in memory. We consider three cases when comparing data structures for speed:
Now its time for some simple data structures.
Most languages don't implement these data structures, not because they are not useful but because we only need to collect data objects to implement them; no behavior.
A record results from intentionally grouping some values together. Hence, a record is a container of values, also known as, fields. Typically, a record is defined by having a fix number of fields. Those fields, always appear in the same order. Notice the lack of name.
We use structs whenever we just want to group some data objects together and give that group a name. Structs differentiate from classes:
Struct | Class |
---|---|
only data - no behavior | behavior and data |
simple creation | explicit instantiation (new, alloc) |
value types | reference types |
no OO features | Polymorphism, etc. |
"Plain Old Data Structure" (PODS) |
New developers should distinguish between data structures' naming conventions and their language's conventions. Since Ruby is my native computer language I will point some of these differences when appropriate. For further details, check Ruby's documentation.
Ruby has structs, though they are implemented as lightweight classes. This leaves the 'only data - no behavior' rule up to the developer.
We covered lots of key concepts. From why we need data structures, and are in fact, already using them in our daily life, all the way to their key characteristics.
We also cover the two must basic data structures; records and structs. Finally, we talked about how Ruby structs differentiate from their data structure counter part.
That's it for this post. Next one{:rel="noopener"} will cover the basics on arrays and sorting algorithms.