Introduction to Data Structures and Algorithms
Tags: data, algorithms, javascript, webdev
Published: 2022-12-26 09:31:03

Data structures can seem complex to grasp at first but understanding the concepts can allow us to write efficient and optimized computer programs. The key to understanding this amazing concept in programming is being consistent with practice as with every other thing.

Learning data structures are also very useful for technical interviews and if you understand the patterns you will be a step ahead of the competition.

In this article, you will understand the fundamentals of data structures.


## What is a data structure ?

A data structure is a group of data elements that provide the most efficient ways to store and perform different actions on and with the data stored on a computer. A data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.
 
The choice of a good data structure makes it possible to perform a wide range of critical operations effectively. An efficient data structure utilizes minimal memory space and processing time.
 
Data structures serve as the foundation for abstract data types, the abstract data type defines the logical form of the data type, and the data structure implements the physical form of the data type.

## The need for data structures

The structure of the data and synthesis of the algorithm are relative to each other. Data structures are necessary for designing efficient algorithms, for provision of reusability and abstraction. 

Data structures are critical for time-saving while performing operations such as storage, retrieval, processing, and manipulation of any amount of data. Some of the needs for data structures include: Efficient time utility, easy data representation, easy data organization and reduction in computer memory space requirement.


**Types of data structures**
There are two types of data structures; the linear data structures and the non-linear data structures. Let’s learn about their differences:

- **Linear data structures:** This is a type of data structure where data is arranged linearly or in a linear pattern. The elements are arranged so that an element is linked directly to the previous and next elements in the data set. Some examples are: arrays, lists, stacks and queues.

- **Non-linear data structures:** In this type of data structure, data is arranged in different dimensions such as one-to-one, one-to-many and many-to-many. Some examples are trees, graphs, and tables. 

![Types of data structures](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwk43eskpofgw7j5lpkx.png)
An image showing the linear and non-linear data structures

## Breaking down the data structures
In this section we’ll be going over some linear and non linear data structures.

**1. Arrays**

Arrays are extremely powerful data structures that store a collection of similar data elements at adjacent memory locations.

This implementation is necessary for the retrieval of elements stored at any particular index and storage of multiple items of the same type at once. This is done by adding an offset to the base value, which in most cases is the memory location of the first element of the array (usually represented by the name of the array).

**2. Linked Lists**

Just like arrays, linked lists are linear data structures. Linked list elements are not stored at adjacent memory locations rather the elements are linked using pointers from the elements within the list.

A linked list consists of nodes where each node represents a data field and a reference link to the next node in the list. There are different types of linked lists including `singly linked lists` & `doubly linked lists`

- **Singly linked lists:** This type of linked list is unidirectional. It contains nodes that have a value field as well as a next field that points to the next node in the list. Some operations that can be performed on a linked list are insertion, removal, and traversal.

- **Doubly linked lists:** In this type of linked list, each node contains a value field, a next field, and a previous field pointing to the previous node in the sequence.


**3. Stacks**

A stack is a linear data structure that follows a specific order in which operations are running. The order may be LIFO (Last-In-First-Out) or FILO (First-In-Last-Out).
 
In this type of data structure, all insertion and deletion operations are allowed only on one end, called TOP.
 
A stack is an abstract data type (ADT) and can be implemented in most programming languages. It is named a stack because it behaves like a real-world stack e.g. a deck of cards or a stack of plates.
 
The basic operations that can be performed in a stack are:


- **Initialize:** Create an empty stack
- **Push:** Add an item to the stack. If stack is full, return an overflow condition.
- **Pop:** Remove the top item from the stack. 
- **Peek or Top:** Return the top element of the stack
- **isEmpty:** Return `true` if the stack is empty, else return `false`.
- **isFull:** Check if stack is full.

**4. Queues**

A queue is a linear list in which elements can only be inserted at one end, called the rear, and deleted only at the other end, called the front. Like a stack, a queue follows a specific order in which the operations are performed. The order is First-in-First-out (FIFO).

A real-world example of a queue would be a consumer queue, where the first consumer is served first, and the next comes.

Stacks and queues are similar, although one major difference is that in stacks, we remove the item that was most recently added, and in queues, we remove the item that was least recently added.

The basic operations that can be performed in a queue are

- **Enqueue:** Insert an element to the queue
- **Dequeue:** Remove an element from the queue.  if the queue is empty, then it is said to be an underflow condition
- **Peek:** Retrieves the element at the front node of the queue without deleting it. 
- **isFull:** Validates if the queue is full.
- **isNull:** Checks if the queue is empty.
- **Front:** Get the first item from the queue.
- **Rear:** Get the last item from the queue. 

**5. Binary trees**

A binary tree is a tree-type non-linear data structure with a maximum of two children for each parent, referred to as parents. Trees are multi-level data structures with a hierarchical relationship among their elements known as nodes.

A binary tree is denoted by a pointer to the topmost node in the tree. If the tree is empty, that makes the value of the root `NULL`. A binary tree node contains three main parts: Data element, Left (Pointer to the left child), and Right (Pointer to the right child).
 
**5. Binary search tree**

A binary search tree is a binary tree with the following additional properties:

- The left part of the root node contains keys less than the root node key
- The right part of the root node contains keys greater than the root node key
- There is no duplicate key present in the the binary tree

**6. Heap**

A heap is a special tree-based data structure which is an almost complete tree that satisfies the heap property. Heaps can be of two types:

- **Max-Heap:** In a max heap, the value in each internal node is greater than or equal to the values in the children of that node. 

- **Min-Heap:** In a min heap, the value in each internal node is less than or equal to the values in the children of that node.

**7. Graph**

A graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes referred to as nodes, and edges are lines or arcs that connect any two nodes in the graph. A graph fundamentally has two parts:

- **Vertices:** These are the fundamental units of the graph. They can also be called nodes or vertexes. Every node/vertex can be labeled or unlabeled.

- **Edges:** These are drawn to connect two nodes in the graph. It can be an ordered pair of nodes in a directed graph. Edges can also be called arcs, and every edge can be labeled or unlabeled.

Graphs are very useful in real-life problems. They can be used to represent networks, and these may include paths in a city or telephone network, or circuit network. They are also used on social networks like LinkedIn and Facebook. On Facebook, each person is represented by a node that contains information like the id, name, gender, etc.


**Conclusions**
In conclusion, data structures can be very useful and efficient in real world scenarios.

It is very important to understand how data structures work, when and how to implement them to level up your skills and gain access to any esteemed tech companies. 

In the next article, I will cover the implementation of these data structures, so stay tuned. In the mean time, you can check out [this course](https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass) by Colt Steele on data structures and algorithms.!