Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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:

                β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
                β”‚ 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

append(&other)

Moves the elements of another set into this set.

clear()

Removes all elements.

contains(&v)

Returns true if this set contains the value v. π’ͺ(1) time.

difference(&other)

Returns an iterator over the values that are in this set but not in the other.

get(&v)

Returns a reference to the value contained in this set, if it exists. The returned type is Option<&V>. π’ͺ(1) time.

first()

Returns a reference on the first element.

insert(v)

Inserts a new value. π’ͺ(1) time.

intersection(&other)

Returns an iterator over the values that are both in this set and the other.

is_disjoint(&other)

Returns true if this set has no element in common with the other. π’ͺ(n) time.

is_empty()

Returns true if this set contains no elements.

is_subset(&other)

Returns true if the other set contains all values of this set. π’ͺ(n) time.

is_superset(&other)

Returns true if this set contains all values of the other set. π’ͺ(n) time.

iter()

Returns an iterator over all values. Order is undefined.

last()

Returns a reference on the last element.

len()

Returns the number of elements.

new()

Creates a new set.

remove(&v)

Removes the specified value from the set.

replace(v)

Replaces an existing value with a new value v.

symmetric_difference(&other)

Returns an iterator over the values that are in this set or in the other but not in both.

take(&v)

Removes and returns a value from the set.

union(&other)

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:

Couldn'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:

29.2.2Testing if a value existsΒΆ

To test if a value exists, we use the contains() method:

29.2.3Combining methodsΒΆ

The union() method returns an iterator over all elements from both sets, eliminating duplicates:

Explanations:

The intersection() method returns an iterator over the elements that are inside the both sets:

The difference() method returns an iterator over the elements of a set that are not inside another:

The symmetric_difference() method returns an iterator over elements that are in one set or the other but not both:

29.2.4Comparing methodsΒΆ

The is_subset() method tests if a set is a sub-set of another:

The is_superset() method tests if a set is a super-set of another:

The is_disjoint() method tests if two sets have no element in common: