DSA icon indicating copy to clipboard operation
DSA copied to clipboard

Swift Tips

Open lefex opened this issue 6 years ago • 21 comments

public typealias RBNode = RBTreeNode<T>

重命名

lefex avatar Jul 17 '19 02:07 lefex

private enum RBTreeColor { case red case black }

枚举

lefex avatar Jul 17 '19 02:07 lefex

public class RBTreeNode<T: Comparable>: Equatable { }

lefex avatar Jul 17 '19 02:07 lefex

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?) { }

lefex avatar Jul 17 '19 02:07 lefex

var isLeaf: Bool { return rightChild == nil && leftChild == nil } 不是函数哈

lefex avatar Jul 17 '19 02:07 lefex

var isLeaf: Bool { return rightChild == nil && leftChild == nil }

lefex avatar Jul 17 '19 02:07 lefex

fileprivate(set) var root: RBNode

lefex avatar Jul 17 '19 02:07 lefex

extension RBTreeNode { static public func == <T>(lhs: RBTreeNode<T>, rhs: RBTreeNode<T>) -> Bool { return lhs.key == rhs.key } }

lefex avatar Jul 17 '19 02:07 lefex

if let leftChild = leftChild, !leftChild.isNullLeaf { return leftChild.maximum() }

lefex avatar Jul 17 '19 02:07 lefex

// If node nil -> key not found guard let node = node else { return nil }

guard let inputKey = input.key, let nodeKey = node.key else { return }

lefex avatar Jul 17 '19 02:07 lefex

// MARK: - Inserting new nodes

lefex avatar Jul 17 '19 02:07 lefex

extension RBTreeNode: CustomStringConvertible { public var description: String { var s = "" if isNullLeaf { s += "nullLeaf" } else { if let left = leftChild { s += "((left.description)) <- " } return s } }

lefex avatar Jul 17 '19 02:07 lefex

extension TreeNode where T: Equatable { }

lefex avatar Jul 17 '19 02:07 lefex

private func insert(_ key: KeyType, val: Any) { }

lefex avatar Jul 17 '19 02:07 lefex

private var cache: [KeyType: Any] = [:]

lefex avatar Jul 17 '19 02:07 lefex

public mutating func sort() -> [T] {}

lefex avatar Jul 17 '19 02:07 lefex

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 }

lefex avatar Jul 17 '19 02:07 lefex

@inline(__always) internal func parentIndex(ofIndex i: Int) -> Int { return (i - 1) / 2 }

lefex avatar Jul 17 '19 02:07 lefex

public mutating func insert<S: Sequence>(_ sequence: S) where S.Iterator.Element == T { for value in sequence { insert(value) } }

lefex avatar Jul 17 '19 02:07 lefex

@discardableResult public mutating func remove() -> T? { }

lefex avatar Jul 17 '19 02:07 lefex

var h1 = Heap(array: [5, 13, 2, 25, 7, 17, 20, 8, 4], sort: >)

lefex avatar Jul 17 '19 02:07 lefex

func partitionDutchFlag<T: Comparable>(_ a: inout [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {

lefex avatar Jul 17 '19 02:07 lefex