Sha256: 473ef18af69d1626a818615c5c8c1717e646cf7c50fdd9886584b7841b451923
Contents?: true
Size: 1.84 KB
Versions: 146
Compression:
Stored size: 1.84 KB
Contents
indirect enum BinarySearchTree<Element: Comparable> { var data: Element? { guard case let .node(_, value, _) = self else { return nil } return value } var left: BinarySearchTree<Element>? { guard case let .node(left, _, _) = self else { return nil } return left } var right: BinarySearchTree<Element>? { guard case let .node(_, _, right) = self else { return nil } return right } case empty case node(BinarySearchTree<Element>, Element, BinarySearchTree<Element>) init(left: BinarySearchTree<Element> = .empty, _ value: Element, right: BinarySearchTree<Element>= .empty ) { self = .node(left, value, right) } init() { self = .empty } private func insert ( _ into: BinarySearchTree<Element>, _ newValue: Element ) -> BinarySearchTree<Element> { let bailOut = BinarySearchTree<Element>(left: .empty, newValue, right: .empty) guard case let .node(left, value, right) = into else { return bailOut } if newValue <= value { return BinarySearchTree(left: insert(left, newValue), value, right: right) } if value <= newValue { return BinarySearchTree(left: left, value, right: insert(right, newValue)) } return .node(left, value, right) } mutating func insert( _ newValue: Element ) { guard case let .node(left, value, right) = insert(self, newValue) else { return } self = .node(left, value, right) } private func getAllData() -> [Element] { guard case let .node( left, value, right) = self else { return [] } var result = [value] result += left.getAllData() result += right.getAllData() return result } func allData() -> [Element] { return getAllData().sorted(by: <) } }
Version data entries
146 entries across 146 versions & 1 rubygems