AVLTreeSet\<T\> class (Nixill.Collections)
The AVLTreeSet<T>
class is a navigable key set backed by an AVL binary tree. It is an automatically sorted set inspired by Java’s TreeSet<E>
class, created because C#’s native SortedSet<T>
lacks key trawling and searching methods.
Type parameters
T
: The type of elements stored in the set.
Constructors
This class contains six constructor overloads:
()
(IComparer<T>)
(Comparison<T>)
(IEnumerable<T>)
(IEnumerable<T>, IComparer<T>)
(IEnumerable<T>, Comparison<T>)
The parameters of these constructors are as follows:
IEnumerable<T>
elems
: The elements with which to pre-populate the set. Overloads that don’t have this parameter create an emptyAVLTreeSet
.IComparer<T>
comparer
orComparison<T>
comparer
: The comparer or function that compares the elements of this set. Overloads that don’t have either of these parameters useT
’s naturel comparer, and throw anInvalidOperationException
ifT
isn’t naturally comparable.
Properties
Count
int
- Gets the number of elements in this AVLTreeSet<T>
.
IsReadOnly
bool
- Gets whether or not this AVLTreeSet<T>
is read-only (which is always false
).
Methods
Empty check
The IsEmpty()
method simply returns true
iff the AVLTreeSet<T>
contains no elements, and false
otherwise.
Nearby items
The SearchAround(T)
method takes any value T
for the parameter value
, and returns a NodeTriplet<T>
with the requested value, the next-lower entry in the set, and the next-higher entry in the set (if the AVLTreeSet<T>
contains them, respectively).
Navigation
Through the INavigableSet<T>
interface, AVLTreeSet<T>
implements several methods for getting values higher or lower than a given value.
For all of these methods:
- “lowest” refers to the lowest value in the set.
- “lower” refers to the highest value less than the given value.
- “floor” refers to the highest value less than or equal to the given value.
- “ceiling” refers to the lowest value greater than or equal to the given value.
- “higher” refers to the lowest value greater than the given value.
- “highest” refers to the highest value in the set.
- “a relative value” just refers to whichever of those is applicable to the particular methods in question.
Contains relative value
The methods ContainsLower
, ContainsFloor
, ContainsCeiling
, and ContainsHigher
are all bool
s.
These methods all have one parameter, T
from
, which is the value relative to which another should be found.
They return true
iff such a relative value exists, and false
otherwise.
Try get relative value
The methods TryGetLower
, TryGetFloor
, TryGetCeiling
, and TryGetHigher
are all bool
s.
These methods have two parameters:
T
from
- The value relative to which another should be found.out T
value
- When the method returnstrue
, this contains the found value. When the method returnsfalse
, this containsdefault(T)
.
They return true
iff such a relative value exists, as well as setting value
to that value, and false
otherwise.
Get relative value
The methods Lower
, Floor
, Ceiling
, and Higher
all directly get a relative value.
All of these methods have one parameter, T
from
, which is the value relative to which another should be found.
They return a T
which is that relative value.
If the relative value doesn’t exist, these methods throw an exception:
InvalidOperationException
: If theAVLTreeSet<T>
is empty.ArgumentOutOfRangeException
: If there is no such relative value.
Get bounding values
The methods LowestValue
and HighestValue
return the bounding values of the set.
Both of these methods are parameterless, and return a T
which is that bounding entry.
If the AVLTreeSet<T>
is empty, an InvalidOperationException
is thrown.
Google Code methods
These methods were included with the original entry on Google Code:
bool Delete(T arg)
- If theAVLTreeSet<T>
containsarg
, removes it from the set and returnstrue
. Otherwise, returnsfalse
.bool DeleteMax()
- If theAVLTreeSet<T>
contains any values, removes the lowest one and returnstrue
. Otherwise, returnsfalse
.bool DeleteMin()
- If theAVLTreeSet<T>
contains any values, removes the highest one and returnstrue
. Otherwise, returnsfalse
.int GetHeightLogN()
- Returns the height of the tree.bool GetMax(out T value)
- If theAVLTreeSet<T>
contains any values, setsvalue
to the highest value and returnstrue
. Otherwise, setsvalue
todefault(T)
and returns false.bool GetMin(out T value)
- If theAVLTreeSet<T>
contains any values, setsvalue
to the lowest value and returnstrue
. Otherwise, setsvalue
todefault(T)
and returns false.void Print()
- Writes this instance’s data usingConsole.Write
andConsole.WriteLine
.void ReplaceValue(T from, T to)
- If theAVLTreeSet<T>
containsfrom
, replaces it withto
. Throws anInvalidOperationException
if theAVLTreeSet<T>
doesn’t containfrom
, or anArgumentOutOfRangeException
ifto
is lower thanLower(from)
or higher thanHigher(from)
.
Standard Set methods
These behave in a similar manner to other ISet<T>
implementations and will just be summarized here:
bool Add(T arg)
- Ifarg
isn’t already in the set, adds it and returnstrue
. Otherwise, returnsfalse
.void ICollection<T>.Add(T arg)
- Ifarg
isn’t already in the set, adds it. Otherwise, does nothing.void Clear()
- Clears theAVLTreeSet<T>
.bool Contains(T arg)
- Returnstrue
iff theAVLTreeSet<T>
containsarg
andfalse
otherwise.void CopyTo(T[] array, int index)
- Copes the elements of theAVLTreeSet<T>
into an array.void ExceptWith(IEnumerable<T> elems)
- Removes all elements from theAVLTreeSet<T>
that are also inelems
.IEnumerator<T> GetEnumerator()
- Gets an enumerator over the elements of theAVLTreeSet<T>
.IEnumerator IEnumerable.GetEnumerator()
- Gets a typeless enumerator over the elements of theAVLTreeSet<T>
.void IntersectWith(IEnumerable<T> elems)
- Removes all elements from theAVLTreeSet<T>
that are not also inelems
.bool IsProperSubsetOf(IEnumerable<T> other)
- Returns true iff thisAVLTreeSet<T>
is a proper subset ofother
; that is, all elements of this set are also inother
, and there are some elements ofother
that are not in this set.bool IsProperSupersetOf(IEnumerable<T> other)
- Returns true iff thisAVLTreeSet<T>
is a proper superset ofother
; that is, all elements ofother
are also in this set, and there are some elements of this set that are not inother
.bool IsSubsetOf(IEnumerable<T> other)
- Returns true iff thisAVLTreeSet<T>
is a subset ofother
; that is, all elements of this set are also inother
.bool IsSupersetOf(IEnumerable<T> other)
- Returns true iff thisAVLTreeSet<T>
is a superset ofother
; that is, all elements ofother
are also in this set.bool Overlaps(IEnumerable<T> other)
- Returns true iff thisAVLTreeSet<T>
overlapsother
; that is, the two sets have any elements in common.bool Remove(T item)
- Ifitem
is part of thisAVLTreeSet<T>
, removes it and returnstrue
. Otherwise, returnsfalse
.bool SetEquals(IEnumerable<T> other)
- Returnstrue
iff thisAVLTreeSet<T>
contains exactly the same set of items asother
. Otherwise, returnsfalse
.void SymmetricExceptWith(IEnumerable<T> elems)
- Removes all elements from thisAVLTreeSet<T>
that are inelems
, and adds all elements fromelems
that are not in thisAVLTreeSet<T>
.void UnionWith(IEnumerable<T> elems)
- Adds all elements fromelems
to thisAVLTreeSet<T>
that are not already in it.