Skip to content

Commit 43c76ff

Browse files
committed
merging
Merge branch 'main' into branch-quickSort
2 parents b3ced50 + 51d3ad6 commit 43c76ff

File tree

14 files changed

+345
-96
lines changed

14 files changed

+345
-96
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ This repository contains implementation of some of the fundamental data structur
88
## Full List of Implementation (in alphabetical order):
99
## Structures
1010
- Adelson-Velskii and Landis (AVL) Binary Search Tree
11-
- Disjoint Set
11+
- Disjoint Set / Union Find
1212
* Quick Find
13-
* Weighted Union with path compression
13+
* Weighted Union
14+
* Path compression
15+
- [Hashing](src/dataStructures/hashSet)
16+
* [Chaining](src/dataStructures/hashSet/chaining/)
17+
* [Open Addressing](src/dataStructures/hashSet/openAddressing/)
1418
- [Heap](src/dataStructures/heap/)
1519
* Max heap implementation
16-
- Linked List
20+
- [Linked List](src/dataStructures/linkedList)
1721
- LRU Cache
1822
- Minimum Spanning Tree
19-
- Queue
23+
- [Queue](src/dataStructures/queue)
2024
- Segment Tree
2125
* Array implementation
2226
* TreeNode implementation
23-
- Stack
27+
- [Stack](src/dataStructures/stack)
2428
- Trie
2529

2630

@@ -47,11 +51,12 @@ This repository contains implementation of some of the fundamental data structur
4751

4852
## Short-cut to CS2040S Material
4953
1. Basic structures
50-
* Linked List
51-
* Stack
52-
* Queue
54+
* [Linked List](src/dataStructures/linkedList)
55+
* [Stack](src/dataStructures/stack)
56+
* [Queue](src/dataStructures/queue)
5357
2. Binary Search
5458
* Peak Finding
59+
* Template
5560
3. Sorting
5661
* [Bubble](src/algorithms/sorting/bubbleSort)
5762
* [Insertion](src/algorithms/sorting/insertionSort)
@@ -64,9 +69,18 @@ This repository contains implementation of some of the fundamental data structur
6469
* Kd-tree
6570
* Interval tree
6671
* Augmented tree for orthogonal range searching
67-
5. Heap
72+
* Red-Black Tree
73+
* ab-Tree
74+
5. [Binary Heap](src/dataStructures/heap/)
75+
* Max heap implementation
6876
6. Disjoint Set / Union Find
69-
7. Hashing
77+
* Quick Find
78+
* Weighted Union
79+
* Path compression
80+
7. [Hashing](src/dataStructures/hashSet)
81+
* [Chaining](src/dataStructures/hashSet/chaining/)
82+
* [Open Addressing](src/dataStructures/hashSet/openAddressing/)
83+
* Double Hashing
7084
* Bloom filter
7185
8. Basic graphs
7286
* Depth-first search

src/algorithms/patternFinding/KMP.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* there is no prefix ending before its index, 0, that can be matched with.
1818
* Read: ^ ^ 'B' and 'C' cannot be matched with any prefix which are just 'A' and 'AB' respectively.
1919
* Read: ^ Can be matched with an earlier 'A'. So we store 1.
20-
* Prefix is the substring from idx 0 to 1 (exclusive).
21-
* Which can also be interpreted as the index of the next character to match against!
20+
* Prefix is the substring from idx 0 to 1 (exclusive). Note consider prefix from 0-indexed.
21+
* Realise 1 can also be interpreted as the index of the next character to match against!
2222
* Read: ^ ^ Similarly, continue matching
2323
* Read: ^ ^ No matches, so 0
2424
* Read: ^ ^ ^ ^ ^ ^ Match with prefix until position 6!
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package src.dataStructures.queue;
2+
3+
import java.util.Deque;
4+
import java.util.ArrayDeque;
5+
6+
/**
7+
* Implementation of a non-increasing (decreasing) monotonic queue for certain (but common) use case:
8+
* When larger objects pushed to the queue are able to replace and represent smaller objects that come before it.
9+
* Callable methods are:
10+
* isEmpty()
11+
* max()
12+
* pop()
13+
* push()
14+
* @param <T> generic type for objects to be stored or queried
15+
*
16+
* index v Increasing queue Decreasing queue
17+
* 1 5 [5] [5]
18+
* 2 3 [3] 3 kick out 5 [5, 3] #3->5
19+
* 3 1 [1] 1 kick out 3 [5, 3, 1] #1->3
20+
* 4 2 [1, 2] #2->1 [5, 3, 2] 2 kick out 1 #2->3
21+
* 5 4 [1, 2, 4] #4->2 [5,4] 4 kick out 2, 3 #4->2
22+
*/
23+
24+
public class MonotonicQueue<T extends Comparable<T>> {
25+
private Deque<T> dq = new ArrayDeque<>(); // or LinkedList
26+
27+
/**
28+
* Checks if queue is empty.
29+
* @return boolean
30+
*/
31+
public boolean isEmpty() {
32+
return dq.isEmpty();
33+
}
34+
35+
/**
36+
* Push an object into the queue and maintain the non-increasing property.
37+
* @param obj to be inserted
38+
*/
39+
public void push(T obj) {
40+
Integer count = 0;
41+
while (!dq.isEmpty() && obj.compareTo(dq.peekLast()) > 0) {
42+
dq.pollLast(); // Removes elements that do not conform the non-increasing sequence.
43+
}
44+
dq.offerLast(obj);
45+
}
46+
47+
/**
48+
* Returns the highest value stored in the queue.
49+
* @return the first value of the queue.
50+
*/
51+
public T max() {
52+
if (isEmpty()) {
53+
return null;
54+
}
55+
return dq.peek();
56+
}
57+
58+
/**
59+
* Returns the highest valued object in the queue; i.e the first object;
60+
* Removal will only be done all representation of the object has been accounted for.
61+
*/
62+
public T pop() {
63+
if (dq.isEmpty()) {
64+
return null;
65+
}
66+
return dq.poll(); // Returns & remove head of deque
67+
}
68+
}

src/dataStructures/stackAndQueue/queue/Queue.java renamed to src/dataStructures/queue/Queue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src.dataStructures.stackAndQueue.queue;
1+
package src.dataStructures.queue;
22

33
/**
44
* Implementation of a queue structure using LinkedList.

src/dataStructures/queue/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Queue
2+
3+
A queue is a linear data structure that restricts the order in which operations can be performed on its elements.
4+
5+
### Operation Orders
6+
7+
![Queue](https://media.geeksforgeeks.org/wp-content/cdn-uploads/20221213113312/Queue-Data-Structures.png)
8+
9+
*Source: GeeksForGeeks*
10+
11+
Queue follows a FIFO, first in first out order.
12+
This means the earliest element
13+
added to the stack is the one operations are conducted on first.
14+
15+
A [stack](../stack/README.md) is a queue with operations conducted in an opposite manner.
16+
17+
## Analysis
18+
19+
As a queue only interacts with either the first or last element regardless during its operations,
20+
it only needs to keep the pointers of the two element at hand, which is constantly updated as more
21+
elements are removed / added. This allows stack operations to only incur a *O(1)* time complexity.
22+
23+
## Notes
24+
25+
### Stack vs Queues
26+
27+
Stack and queues only differ in terms of operation order, you should aim to use a stack when
28+
you want the most recent elements to be operated on.
29+
Some situations where a stack would work well include build redo / undo systems and backtracking problems.
30+
31+
On the other hand, a queue allows you to operate on elements that enter first. Some situations where
32+
this would be useful include Breadth First Search algorithms and task / resource allocation systems.
33+
34+
### Arrays vs Linked List
35+
It is worth noting that queues can be implemented with either a array or with a [linked list](../linkedList/README.md).
36+
In the context of ordered operations, the lookup is only restricted to the first element.
37+
38+
Hence, there is not much advantage in using a array, which only has a better lookup speed (*O(1)* time complexity)
39+
to implement a queue. Especially when using a linked list to construct your queue
40+
would allow you to grow or shrink the queue as you wish.
41+
42+
### Queue Variants
43+
44+
These are some variants of queue that are commonly used.
45+
46+
#### Double Ended Queue (Deque)
47+
48+
![Deque](https://media.geeksforgeeks.org/wp-content/uploads/anod.png)
49+
50+
*Source: GeeksForGeeks*
51+
52+
Deque is a variant of queue where elements can be removed or added from the head and tail of the queue.
53+
Deque could come in handy when trying to solve sliding window problems.
54+
55+
A deque can be implemented in multiple ways, using doubly linked lists, arrays or two stacks.
56+
57+
#### Monotonic Queue
58+
59+
This is a variant of queue where elements within the queue are either strictly increasing or decreasing.
60+
Monotonic queues are often implemented with a deque.
61+
62+
Within a increasing monotonic queue, any element that is smaller than the current minimum is removed.
63+
Within a decreasing monotonic queue, any element that is larger than the current maximum is removed.
64+
65+
It is worth mentioning that the most elements added to the monotonic queue would always be in a
66+
increasing / decreasing order,
67+
hence, we only need to compare down the monotonic queue from the back when adding new elements.
68+

src/dataStructures/stack/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Stack
2+
Stack is a linear data structure that restricts
3+
the order in which operations can be performed on its elements.
4+
5+
![Stack data structure](https://cdn.programiz.com/sites/tutorial2program/files/stack.png)
6+
7+
*Source: Programiz*
8+
9+
### Operation Orders
10+
11+
Stack follows a LIFO, last in first out order.
12+
This means the most recent element
13+
added to the stack is the one operations are conducted on first.
14+
15+
A [queue](../queue/README.md) conducts operations in the opposite order.
16+
17+
## Analysis
18+
19+
As a stack only interacts with the most recent element regardless of operation,
20+
it keeps the pointer of the most recent element at hand, which is constantly updated as more
21+
elements are removed / added. This allows stack operations to only incur a *O(1)* time complexity.
22+
23+
## Notes
24+
25+
### Stack vs Queues
26+
27+
Stack and queues only differ in terms of operation order, you should aim to use a stack when
28+
you want the most recent elements to be operated on.
29+
Some situations where a stack would work well include build redo / undo systems and backtracking problems.
30+
31+
On the other hand, a queue allows you to operate on elements that enter first. Some situations where
32+
this would be useful include Breadth First Search algorithms and task / resource allocation systems.
33+
34+
### Arrays vs Linked List
35+
It is worth noting that stacks can be implemented with either a array or with a [linked list](../linkedList/README.md).
36+
In the context of ordered operations, the lookup is only restricted to the first element.
37+
38+
Hence, there is not much advantage in using a array, which only has a better lookup speed (*O(1)* time complexity)
39+
to implement a stack. Especially when using a linked list to construct your stack
40+
would allow you to grow or shrink the stack as you wish.

src/dataStructures/stackAndQueue/stack/Stack.java renamed to src/dataStructures/stack/Stack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src.dataStructures.stackAndQueue.stack;
1+
package src.dataStructures.stack;
22

33
import java.util.List;
44
import java.util.ArrayList;

src/dataStructures/stackAndQueue/monotonicQueue/MonotonicQueue.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)