|
17 | 17 | - [Insert at tail](#insert-at-tail)
|
18 | 18 | - [Reverse a Singly Linked List](#reverse-a-singly-linked-list)
|
19 | 19 | - [Stack](#stack)
|
| 20 | + - [Popping element from stack](#popping-element-from-stack) |
20 | 21 | - [Queue](#queue)
|
| 22 | + - [Dequeue an element from Queue](#dequeue-an-element-from-queue) |
| 23 | + - [Enqueue an element in Queue](#enqueue-an-element-in-queue) |
21 | 24 | - [Tree](#tree)
|
22 | 25 | - [Binary Tree](#binary-tree)
|
23 | 26 | - [Binary Search Tree (BST)](#binary-search-tree-bst)
|
@@ -119,6 +122,7 @@ Data-structure represents how data will be stored in memory.
|
119 | 122 | Arrays can store a fixed number of elements, whereas a collection stores object dynamically so there is no size restrictions it grows and shrinks automatically.
|
120 | 123 |
|
121 | 124 | - **Insert** at the end (push) is efficient.
|
| 125 | +- **Insert** at in between is not efficient. |
122 | 126 |
|
123 | 127 | #### Implement Binary Search on a Sorted Array
|
124 | 128 |
|
@@ -206,10 +210,40 @@ Running program output
|
206 | 210 |
|
207 | 211 | Depth First Search (DFS) uses a `stack` for storing the nodes that it is visiting.
|
208 | 212 |
|
| 213 | +#### Popping element from stack |
| 214 | + |
| 215 | +```ts |
| 216 | +var stack = [1, 2, 3, 4]; |
| 217 | +stack.pop(); // 4 , stack [1,2,3] |
| 218 | +stack.pop(); // 3 , stack [1,2] |
| 219 | +stack.pop(); // 2 , stack [1] |
| 220 | +stack.pop(); // 1 , stack [] |
| 221 | +``` |
| 222 | + |
209 | 223 | ### Queue
|
210 | 224 |
|
211 | 225 | Breadth First Search (BFS) uses a `queue` for storing the nodes that it is visiting.
|
212 | 226 |
|
| 227 | +#### Dequeue an element from Queue |
| 228 | + |
| 229 | +```ts |
| 230 | +var queue = [1, 2, 3, 4]; |
| 231 | +queue.shift(); // 1 , queue [2,3,4] |
| 232 | +queue.shift(); // 2 , queue [3,4] |
| 233 | +queue.shift(); // 3 , queue [4] |
| 234 | +queue.shift(); // 4 , queue [] |
| 235 | +``` |
| 236 | + |
| 237 | +#### Enqueue an element in Queue |
| 238 | + |
| 239 | +```ts |
| 240 | +var queue = []; |
| 241 | + |
| 242 | +queue.unshift(1); // queue [1] |
| 243 | +queue.unshift(2); // queue [2,1] |
| 244 | +queue.unshift(3); // queue [3,2,1] |
| 245 | +``` |
| 246 | + |
213 | 247 | ### Tree
|
214 | 248 |
|
215 | 249 | A tree has hierarchical data and it has nodes.
|
@@ -308,20 +342,22 @@ Example: Suppose you have given a tree structure and asked to calculate the aver
|
308 | 342 | | Time complexity: Fast | Time complexity: Slow |
|
309 | 343 | | Where to use: if you can find at root or leaf, find connected components. | Where to use: Find shortest path,find connected components. When you think you have less data go for it. |
|
310 | 344 |
|
311 |
| - |
312 |
| -## Sorting |
| 345 | +## Sorting |
313 | 346 |
|
314 | 347 | ### Merge Sort
|
315 | 348 |
|
316 | 349 | Browser's JavaScript Engine (`Array.prototype.sort`) uses merge sort maximum time. Runtime complexity O(n logn), Memory complexity O(n) because we have to create new list. It uses divide-and-conquer algorithm! and also it is recursive.
|
317 | 350 |
|
| 351 | +[Exercise File](src/sorting/merge-sort.mjs) |
| 352 | + |
| 353 | +## Math.floor |
318 | 354 |
|
319 |
| -## Math.floor |
320 | 355 | 2.5 = 2
|
321 | 356 | 2.8 = 2
|
322 | 357 | 2.4 = 2
|
323 | 358 |
|
324 | 359 | ## Math.round
|
| 360 | + |
325 | 361 | 2.5 = 3
|
326 | 362 | 2.8 = 3
|
327 |
| -2.4 = 2 |
| 363 | +2.4 = 2 |
0 commit comments