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

Version Path
trackler-2.0.5.9 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.8 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.7 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.6 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.5 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.4 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.3 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.2 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.1 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.5.0 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.4.0 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.9 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.8 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.7 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.6 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.5 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.4 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.3 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.2 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift
trackler-2.0.3.1 tracks/swift/exercises/binary-search-tree/BinarySearchTreeExample.swift