The BTreeSet is a B-tree data structure that stores values in an
ordered way. See ProgramΒ 1.
The main features of a BTreeSet are:
The values must implement the Ord trait.
Each node of a B-tree has a maximum of values.
Iteration is made in value order, and has πͺ() complexity.
ββββββ¬βββββ¬βββββ¬βββββ
β 10 β 16 β 23 β β
ββββββ΄βββββ΄βββββ΄βββββ
β± β² β²βββββ
β± β² β²
ββββββ¬βββββ¬βββββ¬βββββ β² ββββββ¬βββββ¬βββββ¬βββββ
β 2 β 5 β β β β β 17 β 19 β 21 β 22 β
ββββββ΄βββββ΄βββββ΄βββββ β ββββββ΄βββββ΄βββββ΄βββββ
ββββββ¬βββββ¬βββββ¬βββββ
β 11 β 13 β 15 β β
ββββββ΄βββββ΄βββββ΄βββββProgramΒ 1:A BTreeSet with integer values, using a maximum of 4 values per node.
The main methods of the BTreeSet are presented in
TableΒ 1.
TableΒ 1:Some methods of BTreeSet
Method | Description |
|---|---|
| Moves the elements of another set into this set. |
| Removes all elements. |
| Returns |
| Returns an iterator over the values that are in this set but not in the other. |
| Returns a reference to the value contained in this set, if it
exists. The returned type is |
| Returns a reference on the first element. |
| Inserts a new value. πͺ(1) time. |
| Returns an iterator over the values that are both in this set and the other. |
| Returns |
| Returns |
| Returns |
| Returns |
| Returns an iterator over all values. Order is undefined. |
| Returns a reference on the last element. |
| Returns the number of elements. |
| Creates a new set. |
| Removes the specified value from the set. |
| Replaces an existing value with a new value |
| Returns an iterator over the values that are in this set or in the other but not in both. |
| Removes and returns a value from the set. |
| Returns an iterator over the values of this set and the other, without duplicates. |
29.2.1Creating a new instanceΒΆ
To create a new B-tree set BTreeSet<V>, an easy way, like for the
Vec type, is to let the compiler infer the type. Inserting
at least one value will give the compiler the information it needs:
use std::collections::BTreeSet;
let mut names = BTreeSet::new();
names.insert("John");
names.insert("Paul");
namesCouldn't automatically determine type of variable `names`.
Please give it an explicit type.Note that objects moved into the set are either copied or moved, depending on their type.
A BTreeSet may also be created from a vector or an array:
let other_names = BTreeSet::from(["George", "Ringo"]);
other_names29.2.2Testing if a value existsΒΆ
To test if a value exists, we use the contains() method:
names.contains("Paul")29.2.3Combining methodsΒΆ
The union() method returns an iterator over all elements from both
sets, eliminating duplicates:
let all_names: BTreeSet<_> = names.union(&other_names).copied().collect();
all_namesExplanations:
The underscore (
_) inside theBTreeSet<_>type asks the compiler that we wantcollect()to insert elements inside a newBTreeSetand to infer the type of the elements inside theBTreeSet.The type returned by
union()isUnion<&T>. That is an iterator on references on elements of both sets. In our case the elements returned byunion()are of type&&strsinceTis&str. In fact, nothing is copied here since our type is a slice.The
copied()method is here to convert&&strinto&str. A call tocloned()would have had the same effect for our case, and would also work for all types implementing theClonetrait.The
collect()method gathers all elements returned byunion()and puts them inside the newBTreeSet<&str>.
The intersection() method returns an iterator over the elements that
are inside the both sets:
all_names.intersection(&other_names).cloned().collect::<Vec<_>>()The difference() method returns an iterator over the elements of a
set that are not inside another:
all_names.difference(&other_names).cloned().collect::<Vec<_>>()The symmetric_difference() method returns an iterator over elements
that are in one set or the other but not both:
names.symmetric_difference(&all_names).cloned().collect::<Vec<_>>()29.2.4Comparing methodsΒΆ
The is_subset() method tests if a set is a sub-set of another:
names.is_subset(&other_names)The is_superset() method tests if a set is a super-set of another:
all_names.is_superset(&other_names)The is_disjoint() method tests if two sets have no element in
common:
names.is_disjoint(&other_names)