Tree
A red-black binary search tree. Adapted from Airspeed Velocity’s implementation, Chris Okasaki’s Purely Functional Data Structures, and Stefan Kahrs’ Red-black trees with types, which is implemented in the Haskell standard library.
Elements must be comparable with Strict total order.
Full documentation is available here.
-
Create an empty
Tree.Declaration
Swift
public init() { self = .Empty } -
Create a
Treefrom a sequenceDeclaration
Swift
public init<S : SequenceType where S.Generator.Element == Element>(_ seq: S) -
Create a
TreeofelementsDeclaration
Swift
public init(arrayLiteral elements: Element...)
-
A description of
self, suitable for debuggingDeclaration
Swift
public var debugDescription: String
-
Returns the smallest element in
selfif it’s present, ornilifselfis empty- Complexity: O(log n)
Declaration
Swift
public var first: Element? -
Returns the largest element in
selfif it’s present, ornilifselfis empty- Complexity: O(log n)
Declaration
Swift
public var last: Element? -
Returns
trueiffselfis emptyDeclaration
Swift
public var isEmpty: Bool -
Returns the number of elements in
self- Complexity: O(
count)
Declaration
Swift
public var count: Int - Complexity: O(
-
Returns
trueiffselfcontainsx- Complexity: O(log n)
Declaration
Swift
public func contains(x: Element) -> Bool
-
Inserts
xintoself- Complexity: O(log n)
Declaration
Swift
public mutating func insert(x: Element)
-
Runs a
TreeGeneratorover the elements ofself. (The elements are presented in order, from smallest to largest)Declaration
Swift
public func generate() -> TreeGenerator<Element>
-
Returns the smallest element in
selfif it’s present, ornilifselfis empty- Complexity: O(log n)
Declaration
Swift
public func minElement() -> Element? -
Returns the largest element in
selfif it’s present, ornilifselfis empty- Complexity: O(log n)
Declaration
Swift
public func maxElement() -> Element? -
Removes the smallest element from
selfand returns it if it exists, or returnsnilifselfis empty.- Complexity: O(log n)
Declaration
Swift
public mutating func popFirst() -> Element? -
Removes the smallest element from
selfand returns it.- Complexity: O(log n)
- Precondition:
!self.isEmpty
Declaration
Swift
public mutating func removeFirst() -> Element? -
Removes the largest element from
selfand returns it if it exists, or returnsnilifselfis empty.- Complexity: O(log n)
Declaration
Swift
public mutating func popLast() -> Element? -
Removes the largest element from
selfand returns it.- Complexity: O(log n)
- Precondition:
!self.isEmpty
Declaration
Swift
public mutating func removeLast() -> Element
-
Removes
xfromselfand returns it if it is present, ornilif it is not.- Complexity: O(log n)
Declaration
Swift
public mutating func remove(x: Element) -> Element?
-
Returns a sequence of the elements of
selffrom largest to smallestDeclaration
Swift
public func reverse() -> ReverseTreeGenerator<Element>
-
Remove the member if it was present, insert it if it was not.
Declaration
Swift
public mutating func XOR(x: Element)
View on GitHub
Tree Extension Reference