Data-Structures Debunked!: From complexity to simplicity


Introduction
Whether you are a beginner, a student revising old concepts or a developer brushing up on your fundamentals, understanding data structures is essential. These foundations are necessary to understand, as they give the developer the power to make more complex projects for the future. This series is a guide to break down the core of data structures in a more digestible way, making it easy to understand their functionality.
Abstract
Every piece of software, in essence, is an illusion, a controlled dance of data changing, storing, and organising constantly. This is why learning data structures is an important part of a developer’s arsenal. In fact, “data structure” is another way to say “organising data and storing it in a computer system.”
Arrays
What are Arrays? Arrays are memory locations in a computer system that are fixed in size. To make it clearer with an anecdote, “Imagine a room(your computer system) and there are a set of boxes”. You pick 4 boxes and store an X amount of candy in each box. You can say “show me the candies in box number 2“, and the compiler will go to index 2 and give you access to the candy inside that box. Here is the equivalent in code:
import numpy as np
Boxes = np.array([12, 23, 11, 66]) #Each box has a set amount of candy
#box num ---> 0, 1, 2, 3 # box number 3 has 66 candies
print(Boxes[2]) # "Show me the candies in box number 2" --> 11
NOTE!: You may have noticed that the box number starts from 0? No need to worry about it, it is normal. This is because computer systems start the count from 0. Of course, you may ask why? It is a Reasonable question that I will answer in a separate article.
What makes Arrays different from other storage types? Strictly speaking, you can only store a limited amount of data in Arrays as the software compiles. Languages such as C, C++ are strict; the Python NumPy library, on the other hand, makes resizing possible by creating new Arrays. To keep it aligned to the anecdote, you cannot add additional boxes to the set of 4. The only way to add new values is to make a larger Array.
What are the Pros of using an Array? Arrays have the benefit of being a fast-access storage type. They are easy to create and use, and they are great for fixed data size situations, where you know ahead of time what you need to store. To conclude, Arrays are a clean and efficient choice.
What are the cons of using an Array? Arrays can store any data type. However, one Array can only have one uniform data type. Returning to the anecdote, imagine that each box can only store candies, and nothing else.
As a side note, it is possible to add different types. However, when the NumPy library outputs the data in Python, it will automatically change all values in the Array to a common type. The disadvantages of using different data types are poor efficiency and memory usage, which I will discuss further in another blog post in the future.
Here is an example below that you can copy and run in your compiler:
import numpy as np
Boxes = np.array([2.5, 23, "Candy", 66]) # it contains a Float, 2 Integers and a String
print(Boxes)
# expected output ['2.5' '23' 'Candy' '66']
Why would you use an Array? Generally, Arrays are much more efficient when it comes to retrieving data. There is no need to go through each index in the Array; you can point to the index, and it will go straight to the data. Additionally, Arrays are your building blocks for future, more complex data structures.
This is a set of operations that you can do on Arrays:
Access means to retrieve the value stored at a specific index in the Array. Using my earlier anecdote, it’s like going to a particular box and looking at the candies inside.
Here is the code equivalent:
import numpy as np Boxes = np.array([12, 23, 11, 66]) #box num ---> 0, 1, 2, 3 # box number 3 has 66 candies num_candies_box_2 = Boxes[1] #it access index 1 and stores it in variable print(num_candies_box_2) # verify if the element has been accessed and stored in variable
Update means to change the value of an existing element in the Array. Continuing with the anecdote, imagine you pick box number 2 (index 1), and you replace or change the candies inside. Remember, an update does not change the size of the Array — it just replaces what’s already there.
Here is the code equivalent:
import numpy as np Boxes = np.array([12, 23, 11, 66]) #box num ---> 0, 1, 2, 3 # box number 3 has 66 candies Boxes[1] = 5 print(Boxes) #The expected output is 23 replaced with 5
Insertion means adding a new element at a specific position in the Array, shifting other elements if needed. In the anecdote, you pick the box at index 0 and insert a new box, pushing the other boxes to the right.
Keep in mind, in fixed-size Arrays, you need space available to insert; if the Array is full, you cannot insert without resizing. In Python lists (dynamic Arrays), this is handled automatically.
Here is the code equivalent:
import numpy as np Boxes = np.array([12, 23, 11, 66]) #box num ---> 0, 1, 2, 3 # box number 3 has 66 candies new_Boxes = np.insert(Boxes, 2, 5) print(new_Boxes) #The expected output is: [12, 23, 5, 11, 66]
Deletion means removing an element from the Array. Using the anecdote, it’s like taking one box out of the set entirely.
Here is the code equivalent:
import numpy as np Boxes = np.array([12, 23, 11, 66]) #box num ---> 0, 1, 2, 3 # box number 3 has 66 candies np.delete(Boxes, 2) print(new_Boxes) #The expected output is: [12, 23, 66]
Traversal means visiting each element in the Array, usually to read or process its contents. In the anecdote, imagine you add 3 candies to every box you visit.
Here is the code equivalent:
import numpy as np Boxes = np.array([12, 23, 11, 66]) #box num ---> 0, 1, 2, 3 # box number 3 has 66 candies for i in range(len(Boxes)): Boxes[i] += 3 print(Boxes) #the expected output is [15 26 14 69]
Subscribe to my newsletter
Read articles from Joynal Abdul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
