Docsity

Data Structures Containers, Summaries of Computer Science

Toggle sidebar
bg1
1 Data Structures: Containers
2 1. What is a Container?
3 In computer science, a container is a class, a data structure, or an object that stores
and organizes other objects (data elements).
4
5 Purpose: To provide a way to store, manage, and manipulate collections of data in
memory.
6 Analogy: Think of a physical warehouse. A warehouse (container) holds boxes (data).
Depending on how you organize the warehouse (shelves, floor piles, barcode system),
it takes different amounts of time to find or store a box.
7
8 2. Broad Classifications of Containers
9 Containers are generally divided into two main categories based on how they organize
data:
10 A. Sequence Containers (Linear)
11 Elements are arranged in a strict linear order. Access is usually based on position
(index).
12
13 Examples: Arrays, Linked Lists, Vectors (Dynamic Arrays).
14 Use Case: When the order of insertion matters, or when you need to process data
sequentially.
15
16 B. Associative Containers (Non-Linear / Key-Based)
17 Elements are organized based on a key or a specific sorting rule, not by their physical
position.
18
19 Examples: Hash Maps (Dictionaries), Sets, Trees.
20 Use Case: When you need to find data quickly based on a unique identifier (key) or
need to maintain a sorted collection.
21
22 3. The Core Containers (Deep Dive)
23 1. Arrays (Static)
24
25 Structure: Contiguous block of memory. Fixed size upon creation.
26 Pros: Extremely fast access by index (O(1)O(1)
27 O(1)). Memory efficient (no extra overhead for pointers).
28 Cons: Fixed size (cannot grow/shrink). Insertion/deletion in the middle is slow
(O(n)O(n)
29 O(n)) because elements must be shifted.
30
31 2. Dynamic Arrays (Vectors / ArrayLists)
32
33 Structure: Contiguous memory, but automatically resizes itself when full (usually by
doubling its capacity).
34 Pros: Fast index access (O(1)O(1)
35 O(1)). Flexible size.
36 Cons: Resizing takes time (O(n)O(n)
37 O(n)), though it happens rarely (Amortized O(1)O(1)
38 O(1) for appending).
39
40 3. Linked Lists
41
42 Structure: Non-contiguous memory. Each element (Node) contains data and a
pointer/reference to the next node.
43 Pros: Dynamic size. Insertion/deletion is very fast (O(1)O(1)
44 O(1)) if you already have the pointer to the node.
45 Cons: No direct index access (O(n)O(n)
46 O(n) to find the ii
47 i-th element). Extra memory overhead for pointers.
48
49 4. Hash Maps / Dictionaries
50
51 Structure: Uses a Hash Function to convert a "Key" into an array index, where the
"Value" is stored.
52 Pros: Blazing fast lookups, insertions, and deletions by key (Average O(1)O(1)
53 O(1)).
54 Cons: Unordered. Poor worst-case performance (O(n)O(n)
55 O(n)) if there are many hash collisions. Keys must be hashable/immutable.
56
pf3
pf4

Partial preview of the text

Download Data Structures Containers and more Summaries Computer Science in PDF only on Docsity!

1 Data Structures: Containers 2 1. What is a Container? 3 In computer science, a container is a class, a data structure, or an object that stores and organizes other objects (data elements). 4 5 Purpose: To provide a way to store, manage, and manipulate collections of data in memory. 6 Analogy: Think of a physical warehouse. A warehouse (container) holds boxes (data). Depending on how you organize the warehouse (shelves, floor piles, barcode system), it takes different amounts of time to find or store a box. 7 8 2. Broad Classifications of Containers 9 Containers are generally divided into two main categories based on how they organize data: 10 A. Sequence Containers (Linear) 11 Elements are arranged in a strict linear order. Access is usually based on position (index). 12 13 Examples: Arrays, Linked Lists, Vectors (Dynamic Arrays). 14 Use Case: When the order of insertion matters, or when you need to process data sequentially. 15 16 B. Associative Containers (Non-Linear / Key-Based) 17 Elements are organized based on a key or a specific sorting rule, not by their physical position. 18 19 Examples: Hash Maps (Dictionaries), Sets, Trees. 20 Use Case: When you need to find data quickly based on a unique identifier (key) or need to maintain a sorted collection. 21 22 3. The Core Containers (Deep Dive) 23 1. Arrays (Static) 24 25 Structure: Contiguous block of memory. Fixed size upon creation. 26 Pros: Extremely fast access by index (O(1)O(1) 27 O(1)). Memory efficient (no extra overhead for pointers). 28 Cons: Fixed size (cannot grow/shrink). Insertion/deletion in the middle is slow (O(n)O(n) 29 O(n)) because elements must be shifted. 30 31 2. Dynamic Arrays (Vectors / ArrayLists) 32 33 Structure: Contiguous memory, but automatically resizes itself when full (usually by doubling its capacity). 34 Pros: Fast index access (O(1)O(1) 35 O(1)). Flexible size. 36 Cons: Resizing takes time (O(n)O(n) 37 O(n)), though it happens rarely (Amortized O(1)O(1) 38 O(1) for appending). 39 40 3. Linked Lists 41 42 Structure: Non-contiguous memory. Each element (Node) contains data and a pointer/reference to the next node. 43 Pros: Dynamic size. Insertion/deletion is very fast (O(1)O(1) 44 O(1)) if you already have the pointer to the node. 45 Cons: No direct index access (O(n)O(n) 46 O(n) to find the ii 47 i-th element). Extra memory overhead for pointers. 48 49 4. Hash Maps / Dictionaries 50 51 Structure: Uses a Hash Function to convert a "Key" into an array index, where the "Value" is stored. 52 Pros: Blazing fast lookups, insertions, and deletions by key (Average O(1)O(1) 53 O(1)). 54 Cons: Unordered. Poor worst-case performance (O(n)O(n) 55 O(n)) if there are many hash collisions. Keys must be hashable/immutable. 56

57 5. Sets 58 59 Structure: A container that stores unique elements. Often implemented via Hash Tables or Balanced Trees. 60 Pros: Guarantees no duplicates. Fast membership testing ("Is X in the set?"). 61 Cons: Cannot store duplicate values. 62 63 6. Container Adaptors (Stacks & Queues) 64 Note: These are not full containers themselves; they are interfaces that restrict how you interact with underlying containers (like Arrays or Linked Lists). 65 66 Stack (LIFO - Last In, First Out): Like a stack of plates. Operations: push (add to top), pop (remove from top). 67 Queue (FIFO - First In, First Out): Like a line at a store. Operations: enqueue (add to back), dequeue (remove from front). 68 69 4. Standard Container Operations (The API) 70 Almost all containers support a standard set of operations. Knowing the Time Complexity (Big-O) of these is crucial for writing efficient code. 71 Operation 72 73 Description 74 Insert / Add 75 76 Adds a new element to the container. 77 Delete / Remove 78 79 Removes an element from the container. 80 Find / Search 81 82 Checks if an element exists or retrieves it. 83 Traverse / Iterate 84 85 Visits every element in the container exactly once. 86 Size / Length 87 88 Returns the number of elements currently stored. 89 IsEmpty 90 91 Returns a boolean indicating if the container has zero elements. 92 5. Complexity Cheat Sheet (Average Case) 93 Container 94 95 Access by Index 96 97 Search 98 99 Insert / Delete 100 Array / Dynamic Array 101 102 O(1)O(1) 103 O(1) 104 105 O(n)O(n) 106 O(n) 107 108 O(n)O(n) 109 O(n) 110 Linked List 111 112 O(n)O(n) 113 O(n) 114 115 O(n)O(n) 116 O(n) 117 118 O(1)O(1) 119 O(1)* 120 Hash Map / Dict

189 Hash Set 190 191 std::unordered_set 192 193 set 194 195 HashSet 196 Sorted Map 197 198 std::map 199 200 N/A (use dict or sorted()) 201 202 TreeMap 203 Stack 204 205 std::stack 206 207 list (using append/pop) 208 209 Stack / Deque 210 Queue 211 212 std::queue 213 214 collections.deque 215 216 Queue / Deque