Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views5 pages

Data Structures

A data structure is a method for organizing and managing data efficiently for various operations. Key categories include linear structures (like arrays and linked lists), non-linear structures (like trees and graphs), and hash-based structures (like hash tables). Each data structure has specific properties and time complexities for operations such as insertion, deletion, and searching.

Uploaded by

anshikapal204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Data Structures

A data structure is a method for organizing and managing data efficiently for various operations. Key categories include linear structures (like arrays and linked lists), non-linear structures (like trees and graphs), and hash-based structures (like hash tables). Each data structure has specific properties and time complexities for operations such as insertion, deletion, and searching.

Uploaded by

anshikapal204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structures

What is a Data Structure?


A data structure is a way to organize, store, and manage data efficiently for
operations like insertion, deletion, searching, and updating.

Categories of Data Structures


| Category | Examples |
| -------------- | -------------------------------- |
| **Linear** | Array, Linked List, Stack, Queue |
| **Non-Linear** | Tree, Graph |
| **Hash-based** | Hash Table, HashMap |

1. Array
Fixed-size, indexed collection of elements (same type)

Elements stored in contiguous memory

Operations:
Access: O(1)

Search: O(n)

Insert/Delete: O(n)

Example:

int[] arr = {1, 2, 3, 4};


System.out.println(arr[2]); // 3

2. Linked List
Nodes connected using pointers

Each node = data + next

Types:
Singly Linked List

Doubly Linked List

Circular Linked List

Operations:
Insertion/Deletion: O(1) at head

Search: O(n)

class Node {
int data;
Node next;
}

3. Stack (LIFO)
Last-In-First-Out structure

Used for recursion, undo, etc.

Operations:
push(x) – Add element

pop() – Remove top

peek() – Top without removing

Time Complexity: All O(1)

Stack<Integer> stack = new Stack<>();


stack.push(10);
stack.pop();

4. Queue (FIFO)
First-In-First-Out structure

Used for scheduling, buffering

Operations:
enqueue(x) – Add

dequeue() – Remove

Variants:
Circular Queue

Deque (Double-ended Queue)

Priority Queue

Queue<Integer> q = new LinkedList<>();


q.add(1);
q.poll(); // remove

5. HashMap / Hash Table


Stores key-value pairs

Uses hashing for fast access

Time Complexity:
Insert, Search, Delete: O(1) average, O(n) worst-case

Map<String, Integer> map = new HashMap<>();


map.put("A", 1);
int x = map.get("A");

6. Tree
Non-linear, hierarchical data structure

Root → Child nodes


Types:
Binary Tree

Binary Search Tree (BST)

AVL Tree (Self-balancing)

Heap (Min/Max)

B-Tree (Database use)

Operations (BST):
Insert, Delete, Search: O(log n) average, O(n) worst

class Node {
int val;
Node left, right;
}

7. Trie (Prefix Tree)


Specialized tree for searching words/prefixes

Used in autocomplete, spell-check

Operations:
Insert, Search: O(length of word)

8. Heap
Complete binary tree with heap property

Min-Heap: Parent < Children

Max-Heap: Parent > Children

Operations:
Insert, Remove: O(log n)

Used in: Priority Queue, Heap Sort

9. Graph
Nodes (vertices) connected by edges

Can be Directed / Undirected, Weighted / Unweighted

Representations:
Adjacency Matrix

Adjacency List

Traversals:
DFS (Depth-First Search)

BFS (Breadth-First Search)

Uses:
Networks, Social Graphs, Maps

10. Set
Unordered, unique elements

No duplicate values

Implemented using Hashing or BST

Set<Integer> set = new HashSet<>();


set.add(10);

Time Complexity Cheat Sheet

| Operation / DS | Array | Linked List | Stack | Queue | HashMap | BST (avg) | Heap
| Graph |
| -------------- | ----- | ----------- | ----- | ----- | ------- | --------- |
-------- | ------ |
| Insert | O(n) | O(1) | O(1) | O(1) | O(1) | O(log n) |
O(log n) | O(1)\* |
| Delete | O(n) | O(1) | O(1) | O(1) | O(1) | O(log n) |
O(log n) | O(1)\* |
| Search | O(n) | O(n) | O(n) | O(n) | O(1) | O(log n) | O(n)
| O(V+E) |

or Graphs, search depends on the traversal algorithm.

Common Interview DS Questions

| Problem Name | DS Used |


| --------------------- | -------------------- |
| Reverse a Linked List | Linked List |
| Valid Parentheses | Stack |
| LRU Cache | HashMap + LinkedList |
| Top K Elements | Heap |
| Word Search | Trie, Backtracking |
| Graph Traversals | Graph + DFS/BFS |
| Merge K Sorted Lists | Heap, LinkedList |

Summary Table

| Data Structure | Key Property | Average Time Ops |


| -------------- | ----------------------- | ----------------------- |
| Array | Indexed, fixed size | Access: O(1) |
| LinkedList | Nodes with pointers | Insert/Delete: O(1) |
| Stack | LIFO | All: O(1) |
| Queue | FIFO | All: O(1) |
| HashMap | Key-value, hash-based | Get/Put: O(1) |
| BST | Ordered tree | Insert/Search: O(log n) |
| Heap | Min/Max complete tree | Insert/Del: O(log n) |
| Graph | Nodes + edges | Varies by algorithm |
| Trie | Prefix tree for strings | Word ops: O(L) |

You might also like