Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift Tips #51

Open
lefex opened this issue Jul 17, 2019 · 21 comments
Open

Swift Tips #51

lefex opened this issue Jul 17, 2019 · 21 comments

Comments

@lefex
Copy link
Owner

lefex commented Jul 17, 2019

public typealias RBNode = RBTreeNode

重命名

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

private enum RBTreeColor {
case red
case black
}

枚举

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

fileprivate(set) var root: RBNode

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

// 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
Copy link
Owner Author

lefex commented Jul 17, 2019

// MARK: - Inserting new nodes

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

extension TreeNode where T: Equatable { }

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

@lefex
Copy link
Owner Author

lefex commented Jul 17, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant