Swift Tips
public typealias RBNode = RBTreeNode<T>
重命名
private enum RBTreeColor { case red case black }
枚举
public class RBTreeNode<T: Comparable>: Equatable { }
public convenience init(key: T?) { self.init(key: key, leftChild: RBNode(), rightChild: RBNode(), parent: RBNode()) }
public init(key: T?, leftChild: RBNode?, rightChild: RBNode?, parent: RBNode?) { }
var isLeaf: Bool { return rightChild == nil && leftChild == nil } 不是函数哈
var isLeaf: Bool { return rightChild == nil && leftChild == nil }
fileprivate(set) var root: RBNode
extension RBTreeNode { static public func == <T>(lhs: RBTreeNode<T>, rhs: RBTreeNode<T>) -> Bool { return lhs.key == rhs.key } }
if let leftChild = leftChild, !leftChild.isNullLeaf { return leftChild.maximum() }
// If node nil -> key not found guard let node = node else { return nil }
guard let inputKey = input.key, let nodeKey = node.key else { return }
// MARK: - Inserting new nodes
extension RBTreeNode: CustomStringConvertible { public var description: String { var s = "" if isNullLeaf { s += "nullLeaf" } else { if let left = leftChild { s += "((left.description)) <- " } return s } }
extension TreeNode where T: Equatable { }
private func insert(_ key: KeyType, val: Any) { }
private var cache: [KeyType: Any] = [:]
public mutating func sort() -> [T] {}
private var orderCriteria: (T, T) -> Bool
/**
- Creates an empty heap.
- The sort function determines whether this is a min-heap or max-heap.
- For comparable data types, > makes a max-heap, < makes a min-heap. */ public init(sort: @escaping (T, T) -> Bool) { self.orderCriteria = sort }
@inline(__always) internal func parentIndex(ofIndex i: Int) -> Int { return (i - 1) / 2 }
public mutating func insert<S: Sequence>(_ sequence: S) where S.Iterator.Element == T { for value in sequence { insert(value) } }
@discardableResult public mutating func remove() -> T? { }
var h1 = Heap(array: [5, 13, 2, 25, 7, 17, 20, 8, 4], sort: >)
func partitionDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {