Set is the basic structure of data structure, it is completely consistent with the set in mathematics, then let's see what is the set in mathematics?
1. What is a collection
A collection refers to a collective of concrete or abstract objects with a certain nature. Among them, these objects constituting the set are called the elements of the set.
To cite a few examples, if humans are taken as a set, the Chinese are an element in the set. If the 0-100
number of nature as a collection, 0
is an element in the set, denoted 0∈100
, 101
not the elements of the collection, it is denoted by 0∉100
. For more details, please open the first chapter of Gaoshu (1) and take a closer look.
Second, the characteristics of the collection
The collection has the following three characteristics:
-
Certainty: Given a set, any given element, the element belongs to or does not belong to the set, the two must be one of the two, no ambiguity is allowed.
-
Mutuality: In a set, any two elements are considered different, that is, each element can only appear once. Sometimes it is necessary to characterize the situation where the same element appears multiple times, and multiple sets can be used, in which elements are allowed to appear multiple times.
-
Disorder: In a set, the status of each element is the same, and the elements are disordered. The order relationship can be defined on the set. After the order relationship is defined, the elements can be sorted according to the order relationship. But as far as the characteristics of the set itself are concerned, there is no inevitable order among the elements.
Three, the type of collection
1. empty set
If a set does not contain any elements, it is called an empty set and recorded as ∅
.
The code can be represented as an empty array (for ease of understanding, are to demonstrate all of the following Python
example to explain).
In [1]: data = set()
In [2]: data
Out[2]: set()
2. Subset
If the set of A
all elements belonging to the set B
, then the A
set is a B
subset of the set.
In [3]: data = set({1, 2, 3, 4, 5})
In [4]: data
Out[4]: {1, 2, 3, 4, 5}
In [5]: 1 in data
Out[5]: True
In [6]: 0 in data
Out[6]: False
3. Intersection
All belong to the set A
and the set belonging to B
a collection of elements called the composition of the intersection, denoted by A∩B
.
In [7]: data = set({1, 2, 3, 4, 5})
In [8]: s = set({2, 4, 6})
In [9]: data & s
Out[9]: {2, 4}
4. Union
Belonging to the set of all A
or part of the set of B
set of elements consisting of called and set, denoted by A∪B
.
In [10]: data | s
Out[10]: {1, 2, 3, 4, 5, 6}
5. Subtraction
All belong to the set A
not belonging to the set B
set of elements consisting of a set of known A
with respect to the set of B
difference sets, denoted A-B
.
In [11]: data - s
Out[11]: {1, 3, 5}
In [12]: s - data
Out[12]: {6}
6. Complement
Generally refers to complement absolute complement, if A
is B
a subset (subset of the concept of looking up ↑
), B
all do not belong to A
the elements consisting of a collection called A
the B
complement of. It can be said that the complement set is a special case of the difference set.
A
It is B
a subset of B
a A
superset.
In [13]: s1 = set({2, 4})
In [14]: data - s1
Out[14]: {1, 3, 5}
7. Symmetric Difference
Set of A
the set B
are not all the A∩B
elements of the collection is called symmetric difference, denoted A△B
, namely: A△B=(A∪B)-(A∩B)
.
In [13]: data ^ s
Out[13]: {1, 3, 5, 6}
Fourth, the algorithm implementation of the collection
The above code is used Python
in data collection has been achieved good structure, then you are familiar with our language this set of data structures implemented algorithm.
This is really simple, I won't write it here (maybe I will write it when I'm not busy in the future). There are a few points to note, only using arrays as the basic data structure to achieve, the characteristics and types of the collection must all be satisfied, performance and efficiency do not need to be considered for the time being, after learning the string matching algorithm and sorting algorithm, etc., come back to optimize.