Data Structures
Data Structures
Queues
Data
Structures
CHEAT SHEET
Time Complexity
Arrays
Arrays
Time Complexity
Description
"n" represents the number of elements in the array.
An array is a collection of elements identified by index or
key values. The elements are stored contiguously in memory.
Operation Time Complexity
Access O(1)
Visualization Search O(n)
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] Insertion O(n)
8 10 12 7 19 4 8 Deletion O(n)
Array Indices 0 1 2 3 4 5 6
Visualization
Visualization
Notice these two values are same!! Notice these two values are same!!
Single Node
Description Visualization
A stack is a linear data structure that follows the LIFO
Push Pop
(Last In First Out) principle. It has two main operations: D D
push and pop.
Top D D
Time Complexity
C Top C
"n" represents the number of elements in the Stack.
B B
Queues
Description Visualization
A queue is a linear data structure that follows the FIFO
(First In First Out) principle. It mainly has two operations:
enqueue and dequeue.
Dequeue
Enqueue E D C B A
Time Complexity
Back Front
"n" represents the number of elements in the Queue. E A
Job scheduling.
Breadth-first search.
Space Complexity
O(n)
Description Visualization
A binary tree is a tree-type non-linear data structure with a
maximum of two children for each parent. 20
8 22
Time Complexity
4 12
"n" represents the number of nodes in the binary tree.
Description Visualization
A binary search tree, also known as an ordered or sorted binary
tree, is a rooted binary tree whose internal nodes each store a key
8 8 is greater than all the nodes in its left
greater than all the keys in the node's left subtree and less than
subtree. 8 is less than all the nodes in its
those in its right subtree.
right subtree
4 10
Space Complexity
O(n)
4 10 pointer 4 10 4 10
As 4 < 5, go right.
2 6 9 2 6 9 2 6 9
pointer
5 5 5 As 6 > 5, go left.
4 10
2 6 9
5 Got it!
Notable Usages
Efficient searching.
In-order traversal.
Visualization
Concept of Left Rotation
2 2
4 4
Go up
(Left rotation) 1
1 1 6
6 6
0 0
0 0 4 8
8 8
2 2
4 Go up 4
(Right rotation)
1 1 1
2 2 2
0 0
0 0 1 4
1 1
2 2 2
4 4 4
Right Rotation
1 1 1 1
2 2 3 3
Left rotation
0 0
0 0
3 0 2 4
3 2
2 2 2
4 4 4
Left Rotation
1 1 1 1
6 6 5 5
Right Rotation
0 0
0 0
5 0 4 6
5 6
Insert 8
0 Insert 12 1 Insert 20 2
8 8 8
(Balanced) 0 1
12 12
(Balanced)
0
20
Left Rotation
1 1
12 12
Insert 10
0 0 1 0
8 20 8 20
0
(Balanced)
10
Space Complexity
O(n)
Description
A hash table, also known as a hash map, is a data structure that provides efficient key-value pair storage and retrieval.
It uses a hash function to compute an index, called a hash code, which maps the key to a specific location in the underlying array.
HashTable-Size = 5 NULL
HashTable function: h(key) = key % 5
NULL
NULL
NULL
NULL
17
43
2
34
43
98
10
Insert 17
NULL
NULL
0x7FFD9518 17 NULL
NULL
NULL
Insert 43
NULL
NULL
0x7FFD9518 17 NULL
0x8EFA9528 43 NULL
NULL
NULL
0x7FFD9518 17 0x7AAD8528 2 NULL
0x8EFA9528 43 NULL
NULL
Insert 34
NULL
NULL
0x7FFD9518 17 0x7AAD8528 2 NULL
0x8EFA9528 43 NULL
0x8ACA6512 34 NULL
Insert 98
NULL
NULL
0x7FFD9518 17 0x7AAD8528 2 NULL
0x8EFA9528 43 0x7AAD8528 98 NULL
0x8ACA6512 34 NULL
Insert 10
0x1B2C2924 10 NULL
NULL
0x7FFD9518 17 0x7AAD8528 2 NULL
0x8EFA9528 43 0x7AAD8528 98 NULL
0x8ACA6512 34 NULL
Operation Best Case Time Complexity Average Case Time Complexity Worst Case Time Complexity
Search O(1) O(1+k) O(n)
Insertion O(1) O(1+k) O(n)
Deletion O(1) O(1+k) O(n)
Trie
Description
A trie is a tree-like data structure used for efficient retrieval and storage of strings. It organizes strings by their characters in a
tree-like structure, making it ideal for tasks such as autocomplete, spell-checking, and searching for words with a common prefix.
Visualization of Trie
Let's create a trie out the following strings:
1. a
2. any
3. the
4. there
5. their
6. annoy
(root)
Insert "a"
(root)
Insert "any"
Insert "the"
(root)
a t
n h
y e
a t a t a t
n h n h n h
n
y e y e y e
r r i r i
e e r e r
Note: All green nodes are leaf nodes representing the tail of a word from the root.
Space Complexity
O(n * m) where n is the maximum length
of a key and m is the number of keys
inserted.