Basic data structures every software engineer must know
A data structure
is a specialized way of organizing data that allows efficiently process it with associated algorithms. Since data is the most crucial entity in computer science, so the true worth of data structures is clear. It is one of the important topics when it comes to Software Engineering interview questions.
In this article, we briefly take a look at common data structures:
- Array
- Linked list
- Stack
- Queue
- Tree
- Heap
- Graph
Arrays
An array
is a data structure
that stores a sequence of values that are all of the same type. We are using the numbering
to refer to the individual value in an array. For instance, if an array has N
values, we think of them as being numbered from 0
to N-1
. Individual objects are selected by an index
. In Java/C#/Typescript/etc we are using the notation array[i]
to refer to the ith value for any value of i
from 0
to N-1
.
Array operations
Insert
: may require to increase the array sizeDelete
: may require to reduce the array sizeUpdate
: update the value of an existing element at a given indexSearch
: search for an element in the array. The search can be done by its value or indexTraverse
: go through array elements
Linked list
A linked list
is a sequential data structure that consists of a sequence of nodes in linear order which are linked to each other. Node
is a data entity that contains data of any type and a reference to another node.
Terms related to linked lists:
Head
points to the first element of the linked listTail
points to the last element of the linked list- Elements in a linked list are known as
nodes
Node
is a data entity that contains data of any type and a pointer to another node
Linked list operations
Insert
: insert anode
to the linked listDelete
: delete anode
from a given linked listUpdate
: update the value of annode
Stack
Stack
is a data structure based on last in first out
(LIFO) policy. A good real-life example is a pile of plates. Just like a pile of plates, a stack allows for operating data that resides at the "end" of the stack. In other words, you can add an item to the top and remove an item from the top.
Stack operations
Push
: adds an item to the top of the collectionPop
- removes an item from the top of the collection
Queue
Queue
is a data structure based on first in first out
(FIFO) policy. In other words, the order of processing tasks depends on the order in which those tasks are added. A good real-life example is a queue in a store or a queue of people waiting in an airport.
Queue operations
Enqueue
- adds an item to the end of the collectionDequeue
- removes an item from the beginning of the collection
Trees
A tree
is a nonlinear data structure. Trees store hierarchies of data
Here are some example of trees:
- Binary tree
- Binary search tree (BST)
- B tree
- Red-black tree
- AVL tree
- Splay tree
- Treap
The terminology used in trees:
Root
: the node at the top of the binary treeChild
: a node below a given nodeParent
: the converse definition of a child (the opposite of child)Leaf
: a node without child nodesEdge
: a connection between two nodesSubtree
: a tree representing the descendants of a node
Heap
A heap
is a special tree-based data structure in which is a complete tree
Heaps can be of two types:
Max Heap
: thevalue
of the parent is greater than or equal to those of its children. This is called themax-heap
property. The root contains themaximum value
of theheap
Min Heap
: thevalue
of the parent is less than or equal to those of its children. This is called themin-heap
property. The root contains theminimum value
of theheap
Graphs
A graph
is a non-linear data structure consisting of nodes
and edges
The terminology used in graphs:
Vertex
is a synonym for anode
of a graphEdge
is a link which connects a vertex to another vertex in the graphAdjacent vertices
: two nodes are said to be adjacent if they are connected to each other by the same edge
Types of graphs
Undirected Graph
: In an undirected graph, nodes are connected by edges that are all bidirectional
Directed Graph
: In a directed graph, nodes are connected by directed edges. Edges go only in one direction