# Mastering Data Structures: An Overview of Types and Best Practices

## From arrays to trees: understanding the building blocks of efficient programming

To put it simply, Data Structure is just Storage units that Organize and store Data.

Be it life or Data we need to organize it, as it allows us to manage better and make sense of the information around us. In life, It helps to manage time more effectively, and prioritize tasks… can be achieved through techniques such as a to-do list or setting up goals or avoiding procrastination, and organizing the house and workplace. In Data, it helps to effectively manage and analyze data… There are several ways to organize data, including using databases, tables, and spreadsheets. To arrange data in a certain way, you may also utilize data structures like arrays, linked lists, stacks, queues, hash tables, and trees. This can help data-driven applications run more smoothly and make it possible to retrieve and analyze data more effectively.

Overall, being organized is important for managing and making sense of the information that surrounds us. We can raise our productivity, lower stress levels, and improve the performance of data-driven systems by putting good organizational practices into practice.

A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently.

# The smallest unit of Data and Data Element:

A Bit is the smallest Data unit, which a computer can store or process. The Bit represents the logical state with one of two possible states i.e. “0” or “1” and The Data Element is a basic unit that has a purpose and meaning. The size of the Data Element depends on the type of data it stores and the type of system in use. A data element can be of any size and it’s not necessarily 1 bit, it can be represented by multiple bits, bytes or even more depending on the data type and the amount of information it needs to store. **words** is another name for** Data elements**.

Data Element or Word:it is a fixed number of bits, it is different from computer to computer, but the same for each device. Compute store information in the form of words.

# Classification of Data Structure:

**Linear Data Structure:**A Data Storage unit in which Data Elements are stored sequentially or in a straight line with each element connected to its preceding and following neighboring elements.

**Static Data Structure:**The memory size of Static Data Structure is fixed and is simpler to access.*An example of this data structure is an array.***Dynamic Data Structure:**The memory size of Dynamic Data Structure changes Dynamically ( It can vary while the program is running, which is thought to be efficient given the code’s memory (and space) complexity)*Examples of this data structure are queue, stack, etc.*

**Non-linear data structure:**A Data Storage unit in which elements are stored in a not linear or non-sequential manner. We cannot explore every element in a non-linear data structure in a single search operation.*Examples of non-linear data structures are trees and graphs.*

Warning: Don't worry about code much. I am going to explain each D.S. in the coming articles.

# ……Some of the Commonly used Data Structure…

# Array

The collection of **Similar Data elements** arranged **linearly **in **fixed-size memory(Static Data Structure) is known** as **Array. **Because of their Static and Linear properties array are foolproof from memory shortage or overflow and Random Access is possible. CPU Scheduling and look-up tables are examples where Array is used.

`# Create an array of integers`

my_array = [1, 2, 3, 4, 5]

print(my_array)

# Linked List

Data elements are bundled with the address of the next Data elements in a structure called Node. The collection of Node is called Linked List. The last Node points to None so it can be identified as the end of the linked List. Linked List size can be altered but changing the address of the last Node. Linked List is used in Music Players for song playing, image view in devices and polynomial manipulation.

`# Node class for the linked list`

class Node:

def __init__(self, value):

self.value = value

self.next = None

# LinkedList class

class LinkedList:

def __init__(self):

self.head = None

# Method to add a new node to the linked list

def add(self, value):

new_node = Node(value)

new_node.next = self.head

self.head = new_node

# Method to print the values of the nodes in the linked list

def print_list(self):

current = self.head

while current:

print(current.value)

current = current.next

# Create a new linked list

my_list = LinkedList()

# Add some values to the linked list

my_list.add(1)

my_list.add(2)

my_list.add(3)

# Print the values of the nodes in the linked list

my_list.print_list()

# Output: 3, 2, 1

# Stack

Stack is a linear and Dynamic data structure where operation can be performed on **LIFO **—* **Last in first out **order. *Stack is used to store function calls in Recursive functions and their results as well. you call history even your browser history (Now you Know ) in stack memory*.*

`class Stack:`

def __init__(self):

self.items = []

def push(self, item):

self.items.append(item)

def pop(self):

if not self.is_empty():

return self.items.pop()

def peek(self):

if not self.is_empty():

return self.items[-1]

# Initialize an empty stack

stack = Stack()

# Add items to the stack

stack.push(1)

stack.push(2)

stack.push(3)

# Remove items from the stack

stack.pop()

stack.pop()

# Print the remaining item in the stack

print(stack.peek()) # Output: 1

# Queue

The queue is a linear and Dynamic data structure where operation can be performed on **FIFO **— *First In First Out **order. Queue is used in Job scheduling and requests in the server are in queue to respond.*

`class Queue:`

def __init__(self):

self.items = []

def enqueue(self, item):

self.items.append(item)

def dequeue(self):

if not self.is_empty():

return self.items.pop(0)

def peek(self):

if not self.is_empty():

return self.items[0]

def is_empty(self):

return len(self.items) == 0

# Tree

The tree is a non-linear hierarchical data structure made up of Nodes that contain data and child Nodes. The Topmost node is called Root Node and the bottom ones are the leaf Nodes. Used in Sorting, Auto-completion, and In file explorer to represent files and folders.

# A Python class that represents

# an individual node in a Binary Tree

class Node:

def __init__(self, key):

self.left = None

self.right = None

self.val = key

# Graphs

The Graph is a non-linear data structure consisting of Vertex ( also known as Node) and Edge. Edges are the line or path that connects two vertexes. They are generally used to represent relationships between objects.

`class AdjNode:`

def __init__(self, data):

self.vertex = data

self.next = None

class Graph:

def __init__(self, vertices):

self.V = vertices

self.graph = [None] * self.V

# Function to add an edge in an undirected graph

def add_edge(self, src, dest):

# Adding the node to the source node

node = AdjNode(dest)

node.next = self.graph[src]

self.graph[src] = node

# Adding the source node to the destination as

# it is the undirected graph

node = AdjNode(src)

node.next = self.graph[dest]

self.graph[dest] = node

I know this is just a brief view of Data Structures and I am hoping to cover a detailed article on each Data structure so make sure to follow. That’s all for this article. I hope it will help you to gain some insights into DSA.

If you found this article helpful, show some love by tapping the clap button for few times. 🙌