Skip to content

GH-129: Set in C++ #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ A data structure is a data organization, management, and storage format that is
<tr>
<td>Set</td>
<td>
<a href="/concepts/cpp/set.md"><code>cpp🐀</code></a>
<a href="/concepts/typescript/set.md"><code>ts</code></a>
</td>
</tr>
Expand Down
Binary file added concepts/abstract/data-structures/set_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 142 additions & 0 deletions concepts/cpp/set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Set

> A set is an abstract data type that is used to store a collection of elements. It is a container that stores a collection of unique elements in a specific order. The elements in a set are usually sorted according to their keys, which are used to identify the elements in the set.

![](../abstract/data-structures/set.png)

Sets are typically implemented using balanced binary search trees, such as red-black trees or AVL trees. These data structures allow for fast insertions, deletions, and searches in the set.

In addition to the basic operations of inserting, deleting, and searching for elements, sets also support a number of other operations, such as union, intersection, and difference.

![](../abstract/data-structures/set_2.png)

Sets are often used to implement logic operations in computer science, such as union-find algorithms, and are also useful for efficiently storing and manipulating large collections of data.

## 💻 Set Implementation in C++

In C++, the `std::set` is a container that stores a collection of unique elements in a specific order. The elements in a set are sorted according to their keys, which are stored in the set.

Here is an example of how to use `std::set` in C++:

```cpp
#include <iostream>
#include <set>

using namespace std;

int main() {
set<int> s;

// Insert elements into the set
s.insert(2);
s.insert(3);
s.insert(5);
s.insert(2); // This element is not inserted, since 2 is already in the set

cout << "Print the elements of the set" << endl;
for (int x : s) {
cout << x << endl;
}

// Remove the element with value 3 from the set
s.erase(3);

cout << "Set after remove value 3" << endl;
for (int x : s) {
cout << x << endl;
}

return 0;
}
```

Output:

```
Print the elements of the set
2
3
5
```

Here's an example of how you can use `set_union`, `set_intersection` and `set_difference` to perform basic set operations on C++

```cpp
#include <iostream>
#include <set>

using namespace std;

int main() {
set<int> A = {2, 3, 5};
set<int> B = {1, 4, 5};

cout << "Set Union" << endl;
set<int> C;
set_union(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()));
for(int x : C){
cout << x << " ";
}
cout << endl;

cout << "Set Intersection" << endl;
set<int> D;
set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(D, D.begin()));
for(int x : D){
cout << x << " ";
}
cout << endl;

cout << "Set Difference: A - B" << endl;
set<int> E1;
set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(E1, E1.begin()));
for(int x : E1){
cout << x << " ";
}
cout << endl;

cout << "Set Difference: B - A" << endl;
set<int> E2;
set_difference(B.begin(), B.end(), A.begin(), A.end(), inserter(E2, E2.begin()));
for(int x : E2){
cout << x << " ";
}
cout << endl;

return 0;
}
```

Output:

```
Set Union
1 2 3 4 5
Set Intersection
5
Set Difference: A - B
2 3
Set Difference: B - A
1 4
```

## 📈 Complexity Analysis of Set

| Operation | Method | Time complexity |
|----------------|-----------------|-----------------|
| Insert | insert | $$O(log n)$$ |
| Delete | erase | $$O(log n)$$ |
| Union | set_union | $$O(m log n)$$ |
| Intersection | set_intersection| $$O(m log n)$$ |
| Difference | set_difference | $$O(m log n)$$ |

## 🔗 Further Reading

* [Set (abstract data type)](https://en.wikipedia.org/wiki/Set_(abstract_data_type)), wikipedia
* ▶️ [Set in C++ STL](https://www.youtube.com/watch?v=4FJrP6aAwSs&ab_channel=Codevolution), GeeksforGeeks
* [set/set](https://cplusplus.com/reference/set/set/), cplusplus.com
* [set_union](https://cplusplus.com/reference/algorithm/set_union/), cplusplus.com
* [set_union](https://cplusplus.com/reference/algorithm/set_union/), cplusplus.com
* [set_intersection](https://cplusplus.com/reference/algorithm/set_intersection/), cplusplus.com
* [set_intersection](https://cplusplus.com/reference/algorithm/set_intersection/), cplusplus.com
* [set_difference](https://cplusplus.com/reference/algorithm/set_difference/), cplusplus.com
2 changes: 1 addition & 1 deletion concepts/typescript/linked-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ console.log(linked_list_2.toString());
```
## 🔗 Further Reading

* 2022, ▶️ [Data Structures in Typescript #5 - Linked List Intro](https://www.youtube.com/watch?v=oXXLFvtG6-Q&list=PLn4fTSbSpY5cL4_0MP83wq5khbmG3IKKd&index=5&ab_channel=JeffZhang), Jeff Zhang
* ▶️ [Data Structures in Typescript #5 - Linked List Intro](https://www.youtube.com/watch?v=oXXLFvtG6-Q&list=PLn4fTSbSpY5cL4_0MP83wq5khbmG3IKKd&index=5&ab_channel=JeffZhang), Jeff Zhang
* 2017, [LinkedList implementation in Typescript](https://stackoverflow.com/questions/42588925/linkedlist-implementation-in-typescript), stackoverflow.com
* [Linked Lists Introduction](https://www.youtube.com/watch?v=-Yn5DU0_-lw&t=7s&ab_channel=WilliamFiset), WilliamFiset
6 changes: 1 addition & 5 deletions tools/rules.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
- name: File must start with Heading 1
type: regex
search: "^# .*\n"
- name: Content must have "From wikipedia" line
type: regex
search: "From wikipedia\n"
# Section "Further Reading"
- name: Content must have "Further Reading" section
type: regex
search: "## 🔗 Further Reading\n"
Expand All @@ -13,4 +9,4 @@
search: "## 🔗 Further Reading\n\n(.*\n)*(\\*.*\n){3,}"
- name: Section "Further Reading" should contains at least 1 video
type: regex
search: "## 🔗 Further Reading\n\n(.*\n)*\\*(.*\n.*)*.*▶️"
search: "## 🔗 Further Reading\n\n.*\\* ▶️"
4 changes: 2 additions & 2 deletions tools/style_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def check_content(file, rules):

for rule in rules:
search = rule['search']
result = re.search(search, content, re.UNICODE | re.MULTILINE)
result = re.search(search, content, re.UNICODE | re.MULTILINE | re.DOTALL)
if not result:
print(f"File: {file}")
message = f"[❌] {rule['name']}\n{rule}"
Expand All @@ -32,4 +32,4 @@ def check_content(file, rules):
content_files = glob.glob("concepts/**/*.md")
for content_file in content_files:
check_content(content_file, rules)
print("✅ Check content files")
print("✅ All checks were successful.")
2 changes: 1 addition & 1 deletion tools/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ File [**`example.cpp`**](code/example.cpp)

* yyyy, [Awesome Reference Link](Awesome Reference Link), awesome.reference
* yyyy, [Awesome Reference Link](Awesome Reference Link), awesome.reference
* yyyy, ▶️ [Awesome Reference Link](Awesome Reference Link), awesome.reference
* ▶️ [Awesome Reference Link](Awesome Reference Link), awesome.reference