Data Structures Explained:
Arrays to Trees โ Full Guide 2026
Everything you need to master Data Structures โ with code examples, Big-O complexity, real-world use cases, GATE PYQs, and interview tips. Zero fluff.
Data Structures are the building blocks of every software system you have ever used โ from Google Search to WhatsApp, from your ATM machine to Swiggy’s delivery algorithm. If you are a Computer Science student, understanding data structures is not optional โ it is the single most tested skill in GATE CSE, TCS, Infosys, Wipro, and FAANG interviews.
This guide takes you from the very beginning โ what a data structure is โ all the way through Arrays, Linked Lists, Stacks, Queues, and Trees. Each section includes clean code examples, time complexity tables, real-world applications, and actual GATE-style questions. Bookmark this page and come back to it often.
1. What Are Data Structures?
A Data Structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently. Think of it like this โ data is the raw material (bricks), and a data structure is the architectural blueprint that decides how those bricks are arranged to build something useful.
Every program you write uses at least one data structure. When you store a list of contacts on your phone, that’s an array. When your browser lets you press Back to go to the previous page, that’s a stack. When you stand in a ticket queue at a railway station, that’s a queue in real life โ and it works exactly the same way in software.
Linear vs Non-Linear Data Structures
๐ Linear
๐ฟ Non-Linear
2. Arrays โ The Foundation of Data Structures
An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is identified by an index (position), starting from 0. If the base address is 1000 and each integer takes 4 bytes, then arr[3] is at address 1000 + (3 ร 4) = 1012.
Arrays are the simplest and most widely used data structure. Almost every other data structure is either built on top of arrays or compared against them.
Index: [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]
Value: [ 10 ] [ 25 ] [ 37 ] [ 48 ] [ 56 ]
โ
Base Address (e.g. 1000)
arr[2] โ Address = 1000 + (2 ร 4) = 1008 โ value = 37
Array Operations with Code (C)
// Declare and initialize an array int arr[5] = {10, 25, 37, 48, 56}; // Access โ O(1): direct index calculation printf("%d\n", arr[2]); // Output: 37 // Linear Search โ O(n) for (int i = 0; i < 5; i++) { if (arr[i] == 48) { printf("Found at index %d\n", i); break; } } // Insertion at end โ O(1) if space available // Insertion in middle โ O(n) due to shifting for (int i = 4; i >= 2; i--) { arr[i + 1] = arr[i]; // shift right } arr[2] = 99; // insert 99 at index 2
Time Complexity: Arrays
| Operation | Average Case | Worst Case | Explanation |
|---|---|---|---|
| Access by index | O(1) | O(1) | Direct formula: base + index ร size |
| Search (unsorted) | O(n) | O(n) | Must check every element |
| Search (sorted, Binary) | O(log n) | O(log n) | Divide and conquer |
| Insert at end | O(1) | O(1) | No shifting needed |
| Insert in middle | O(n) | O(n) | Elements must shift right |
| Delete from middle | O(n) | O(n) | Elements must shift left |
| Space | O(n) | n contiguous blocks required | |
Real-World Applications of Arrays
vector in C++ or list in Python) or switch to a Linked List for truly flexible sizing.
๐ฏ GATE Exam Tip โ Arrays
Arrays are tested heavily in GATE โ expect 2โ3 questions per year. Focus on: address calculation formulas for 1D and 2D arrays, time complexity of search operations, and questions on row-major vs column-major order.
Formula: Base + [(iโ1)รCols + (jโ1)] ร Size
3. Linked Lists โ Dynamic Memory Allocation
A Linked List is a linear data structure where elements (called nodes) are stored at non-contiguous memory locations. Each node contains two parts: the data and a pointer (link) to the next node. Unlike arrays, linked lists do not require a predefined size โ memory is allocated dynamically as new nodes are added.
Singly Linked List:
[HEAD]
|
[ 10 | โ] โโโ [ 25 | โ] โโโ [ 37 | โ] โโโ [ 48 | NULL]
Node 1 Node 2 Node 3 Node 4 (Tail)
Doubly Linked List:
NULL โโ [โ | 10 | โ] โโโโ [โ | 25 | โ] โโโโ [โ | 37 | โ ] โโโ NULL
Node Structure & Basic Operations (C)
// Define a Node struct Node { int data; struct Node* next; }; // Insert at beginning โ O(1) struct Node* insertAtHead(struct Node* head, int val) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = val; newNode->next = head; // new node points to old head return newNode; // new node becomes new head } // Search โ O(n) โ must traverse from head int search(struct Node* head, int val) { int pos = 0; while (head != NULL) { if (head->data == val) return pos; head = head->next; pos++; } return -1; // not found }
Types of Linked Lists
๐ Singly
๐ Doubly
๐ Circular
Array vs Linked List โ Quick Comparison
| Feature | Array | Linked List |
|---|---|---|
| Memory layout | Contiguous | Non-contiguous (scattered) |
| Size | Fixed at declaration | Dynamic (grows/shrinks) |
| Access by index | O(1) โ Direct | O(n) โ Traverse |
| Insert at beginning | O(n) โ Shift needed | O(1) โ Update pointer |
| Insert at end | O(1) โ If space exists | O(n) โ Reach tail first |
| Search | O(n) โ Linear | O(n) โ Linear |
| Cache efficiency | High (locality) | Low (scattered) |
| Memory overhead | Low | Extra pointer per node |
๐ฏ GATE Exam Tip โ Linked Lists
GATE frequently asks questions on: finding the middle node, detecting cycles (Floyd’s algorithm), reversing a linked list, and merging two sorted linked lists. Practice these operations until you can write them from memory.
Answer: O(1) โ Copy next node’s data into current, then delete next node.
4. Stacks โ Last In, First Out (LIFO)
A Stack is a linear data structure that follows the LIFO principle โ the last element inserted is the first one to be removed. Imagine a stack of plates: you always place a new plate on top, and you always remove the top plate first. You cannot access any plate below without removing the ones above it.
Stacks are the backbone of how computers execute function calls, evaluate expressions, and manage undo operations.
PUSH 10, PUSH 20, PUSH 30: POP:
| | | | | |
| 30 | โ TOP | 30 | removed | 20 | โ new TOP
| 20 | | 20 | | 10 |
| 10 | | 10 | | |
|____| |____| |____|
push(x) โ add to TOP pop() โ remove from TOP
peek() โ see TOP without removing
Stack Implementation (Array-based, C)
int stack[100]; int top = -1; // -1 means empty stack // Push โ O(1) void push(int x) { if (top == 99) { printf("Stack Overflow!\n"); return; } stack[++top] = x; } // Pop โ O(1) int pop() { if (top == -1) { printf("Stack Underflow!\n"); return -1; } return stack[top--]; } // Peek โ O(1) int peek() { return (top == -1) ? -1 : stack[top]; }
Real-World Applications of Stacks
๐ฏ GATE Exam Tip โ Stacks
GATE loves postfix/prefix expression evaluation, stack simulation questions, and recursion-based stack frame analysis. The key insight: every recursive function call pushes a frame onto the call stack โ so recursion depth = stack size.
Process: push 5 โ push 3 โ push 2 โ pop 2,3, push 3ร2=6 โ pop 6,5, push 5+6=11 โ push 4 โ pop 4,11, push 11โ4=7
5. Queues โ First In, First Out (FIFO)
A Queue is a linear data structure that follows the FIFO principle โ the first element inserted is the first one to be removed. Think of a line at a movie theatre: the person who arrives first gets served first. New people join at the rear (back), and people leave from the front.
Enqueue (add to REAR): Dequeue (remove from FRONT):
FRONT REAR FRONT REAR
โ โ โ โ
[ 10 ][ 20 ][ 30 ][ 40 ][ 50 ] [ 20 ][ 30 ][ 40 ][ 50 ]
โ removes here adds here โ 10 is removed โ
Types of Queues
๐ Simple Queue
๐ Circular Queue
โก Priority Queue
โ๏ธ Deque
from collections import deque # Python's deque is the optimal Queue implementation q = deque() # Enqueue โ O(1) q.append(10) q.append(20) q.append(30) # Dequeue โ O(1) removed = q.popleft() # removes 10 print(removed) # Output: 10 # Peek โ O(1) print(q[0]) # Output: 20 (front element)
Real-World Applications of Queues
6. Trees โ Hierarchical Data Structures
A Tree is a non-linear hierarchical data structure consisting of nodes connected by edges. Unlike linear data structures (arrays, linked lists), trees represent relationships with a parent-child hierarchy. A tree with n nodes has exactly nโ1 edges.
The root is the topmost node. Nodes with no children are called leaf nodes. The height of a tree is the length of the longest path from root to a leaf.
Tree Terminology:
[A] โ Root (Level 0)
/ \
[B] [C] โ Level 1 (children of A)
/ \ \
[D] [E] [F] โ Level 2 (D,E children of B; F child of C)
/ \
[G] [H] โ Leaf nodes (no children)
โข A is parent of B, C โข D, E, G, H are leaf nodes
โข Height of tree = 3 โข Depth of F = 2
โข B is an internal node โข Degree of B = 2 (has 2 children)
Binary Tree โ The Most Important Type
A Binary Tree is a tree where every node has at most 2 children โ commonly called the left child and right child. Binary trees are foundational because most tree-based data structures (BST, Heap, AVL) are binary trees with additional constraints.
Tree Traversals โ The Big Three
Traversal means visiting every node exactly once. The three main traversal methods differ in when you visit the root relative to the left and right subtrees:
| Traversal | Order | Pattern | Use Case |
|---|---|---|---|
| Inorder | Left โ Root โ Right | LโNโR | BST sorted output |
| Preorder | Root โ Left โ Right | NโLโR | Copy a tree, prefix expressions |
| Postorder | Left โ Right โ Root | LโRโN | Delete a tree, postfix expressions |
| Level Order | Level by Level (BFS) | Use Queue | Finding shortest path |
struct Node { int data; struct Node *left, *right; }; // Inorder: Left โ Root โ Right โ gives sorted order in BST void inorder(struct Node* root) { if (root == NULL) return; inorder(root->left); printf("%d ", root->data); inorder(root->right); } // Preorder: Root โ Left โ Right void preorder(struct Node* root) { if (root == NULL) return; printf("%d ", root->data); preorder(root->left); preorder(root->right); }
๐ฏ GATE Exam Tip โ Trees
Trees are extremely high-yield for GATE. Practice: given a traversal sequence, reconstruct the tree. Know the properties of complete binary trees, full binary trees, and perfect binary trees. Height of a complete binary tree with n nodes = โlogโnโ.
Key: First element of Preorder is always the root. Split Inorder around it to find left/right subtrees.
7. Binary Search Trees (BST)
A Binary Search Tree is a binary tree with one crucial additional property: for every node, all values in its left subtree are smaller, and all values in its right subtree are greater. This property enables incredibly efficient search, insert, and delete operations โ all in O(log n) on average.
BST Property: Left < Root < Right
[50]
/ \
[30] [70]
/ \ / \
[20][40][60] [80]
Search 40: 50 โ (40<50, go left) โ 30 โ (40>30, go right) โ 40 โ
Path length = 3 steps = O(log n) for balanced BST
โ Worst Case: Inserting sorted data [10,20,30,40] โ becomes a linked list
[10]
\
[20]
\
[30] โ Skewed BST โ search degrades to O(n)
\
[40]
BST Operations โ Time Complexity
| Operation | Average Case (Balanced) | Worst Case (Skewed) |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| Inorder traversal | O(n) | O(n) |
| Find Min / Max | O(log n) | O(n) |
| Space | O(n) | |
8. Big-O Complexity Cheatsheet โ All Data Structures
This is the table you need to memorize before any placement interview or GATE exam. Know the average and worst-case complexities for every major operation on each data structure.
| Data Structure | Access (Avg) | Search (Avg) | Insert (Avg) | Delete (Avg) | Space |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | O(n) |
| Singly Linked List | O(n) | O(n) | O(1) | O(1) | O(n) |
| Doubly Linked List | O(n) | O(n) | O(1) | O(1) | O(n) |
| Stack | O(n) | O(n) | O(1) | O(1) | O(n) |
| Queue | O(n) | O(n) | O(1) | O(1) | O(n) |
| Hash Table | N/A | O(1) | O(1) | O(1) | O(n) |
| Binary Search Tree | O(log n) | O(log n) | O(log n) | O(log n) | O(n) |
| AVL Tree | O(log n) | O(log n) | O(log n) | O(log n) | O(n) |
| Min/Max Heap | N/A | O(n) | O(log n) | O(log n) | O(n) |
9. Interview & GATE Tips โ What Toppers Know
For Placement Interviews (TCS, Infosys, Amazon, Google)
For GATE CSE โ Data Structures Strategy
๐ฏ Recommended Practice Resources
GATE Overflow (gateoverflow.in) โ solve every DS question from the past 15 years.
GeeksforGeeks DSA โ topic-wise practice with detailed explanations.
LeetCode Easy/Medium โ for placement coding rounds at TCS, Wipro, Infosys.
EnggPrep.in โ use our free tools like the CGPA calculator and grade tracker to stay on top of your academic standing alongside your DSA prep.
10. Frequently Asked Questions (FAQ)
๐ Stay Ahead with EnggPrep.in
Use our free engineering tools โ CGPA Calculator, Attendance Tracker, Grade Calculator, and more โ designed specifically for CSE students like you.
Try CGPA Calculator โ