-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Swift tools version to 6.0 and refactor BinarySearchTree imple…
…mentation
- Loading branch information
1 parent
fa878ba
commit b351a88
Showing
6 changed files
with
337 additions
and
295 deletions.
There are no files selected for viewing
108 changes: 57 additions & 51 deletions
108
.../practice/binary-search-tree/.meta/Sources/BinarySearchTree/BinarySearchTreeExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,72 @@ | ||
indirect enum BinarySearchTree<Element: Comparable> { | ||
|
||
var data: Element? { | ||
guard case let .node(_, value, _) = self else { return nil } | ||
return value | ||
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) | ||
} | ||
|
||
var left: BinarySearchTree<Element>? { | ||
guard case let .node(left, _, _) = self else { return nil } | ||
return left | ||
if value <= newValue { | ||
return | ||
BinarySearchTree(left: left, value, right: insert(right, newValue)) | ||
} | ||
|
||
var right: BinarySearchTree<Element>? { | ||
guard case let .node(_, _, right) = self else { return nil } | ||
return right | ||
} | ||
return .node(left, value, right) | ||
} | ||
mutating func insert(_ newValue: Element) { | ||
|
||
case empty | ||
case node(BinarySearchTree<Element>, Element, BinarySearchTree<Element>) | ||
guard case let .node(left, value, right) = insert(self, newValue) else { return } | ||
|
||
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) | ||
self = .node(left, value, right) | ||
} | ||
|
||
guard case let .node(left, value, right) = into else { return bailOut } | ||
private func getAllData() -> [Element] { | ||
|
||
if newValue <= value { return | ||
BinarySearchTree(left: insert(left, newValue), value, right: right) | ||
} | ||
guard case let .node(left, value, right) = self else { return [] } | ||
var result = [value] | ||
|
||
if value <= newValue { return | ||
BinarySearchTree(left: left, value, right: insert(right, newValue)) | ||
} | ||
result += left.getAllData() | ||
result += right.getAllData() | ||
|
||
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) | ||
} | ||
return result | ||
|
||
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: <) | ||
} | ||
func allData() -> [Element] { | ||
return getAllData().sorted(by: <) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
// swift-tools-version:5.3 | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "BinarySearchTree", | ||
products: [ | ||
.library( | ||
name: "BinarySearchTree", | ||
targets: ["BinarySearchTree"]), | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target( | ||
name: "BinarySearchTree", | ||
dependencies: []), | ||
.testTarget( | ||
name: "BinarySearchTreeTests", | ||
dependencies: ["BinarySearchTree"]), | ||
] | ||
name: "BinarySearchTree", | ||
products: [ | ||
.library( | ||
name: "BinarySearchTree", | ||
targets: ["BinarySearchTree"]) | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target( | ||
name: "BinarySearchTree", | ||
dependencies: []), | ||
.testTarget( | ||
name: "BinarySearchTreeTests", | ||
dependencies: ["BinarySearchTree"]), | ||
] | ||
) |
137 changes: 75 additions & 62 deletions
137
...cises/practice/binary-search-tree/Tests/BinarySearchTreeTests/BinarySearchTreeTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,87 @@ | ||
import XCTest | ||
import Foundation | ||
import Testing | ||
|
||
@testable import BinarySearchTree | ||
|
||
class BinarySearchTreeTests: XCTestCase { | ||
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL"] ?? "fa,se") ?? false | ||
|
||
@Suite struct BinarySearchTreeTests { | ||
|
||
func testDataIsRetained() { | ||
XCTAssertEqual(4, BinarySearchTree(4).data) | ||
} | ||
@Test("Data is retained") | ||
func testDataIsRetained() { | ||
#expect(4 == BinarySearchTree(4).data) | ||
} | ||
|
||
func testInsertingLess() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
XCTAssertEqual(4, four.data) | ||
XCTAssertEqual(2, four.left?.data) | ||
} | ||
@Test("Inserting less", .enabled(if: RUNALL)) | ||
func testInsertingLess() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
#expect(4 == four.data) | ||
#expect(2 == four.left?.data) | ||
} | ||
|
||
func testInsertingSame() { | ||
var four = BinarySearchTree(4) | ||
four.insert(4) | ||
XCTAssertEqual(4, four.data) | ||
XCTAssertEqual(4, four.left?.data) | ||
} | ||
@Test("Inserting same", .enabled(if: RUNALL)) | ||
func testInsertingSame() { | ||
var four = BinarySearchTree(4) | ||
four.insert(4) | ||
#expect(4 == four.data) | ||
#expect(4 == four.left?.data) | ||
} | ||
|
||
func testInsertingRight() { | ||
var four = BinarySearchTree(4) | ||
four.insert(5) | ||
XCTAssertEqual(4, four.data) | ||
XCTAssertEqual(5, four.right?.data) | ||
} | ||
@Test("Inserting right", .enabled(if: RUNALL)) | ||
func testInsertingRight() { | ||
var four = BinarySearchTree(4) | ||
four.insert(5) | ||
#expect(4 == four.data) | ||
#expect(5 == four.right?.data) | ||
} | ||
|
||
func testComplexTree() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
four.insert(6) | ||
four.insert(1) | ||
four.insert(3) | ||
four.insert(7) | ||
four.insert(5) | ||
XCTAssertEqual(4, four.data) | ||
XCTAssertEqual(2, four.left?.data) | ||
XCTAssertEqual(1, four.left?.left?.data) | ||
XCTAssertEqual(3, four.left?.right?.data) | ||
XCTAssertEqual(6, four.right?.data) | ||
XCTAssertEqual(5, four.right?.left?.data) | ||
XCTAssertEqual(7, four.right?.right?.data) | ||
} | ||
@Test("Complex tree", .enabled(if: RUNALL)) | ||
func testComplexTree() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
four.insert(6) | ||
four.insert(1) | ||
four.insert(3) | ||
four.insert(7) | ||
four.insert(5) | ||
#expect(4 == four.data) | ||
#expect(2 == four.left?.data) | ||
#expect(1 == four.left?.left?.data) | ||
#expect(3 == four.left?.right?.data) | ||
#expect(6 == four.right?.data) | ||
#expect(5 == four.right?.left?.data) | ||
#expect(7 == four.right?.right?.data) | ||
} | ||
|
||
func testAllDataForOneElement() { | ||
XCTAssertEqual([4], BinarySearchTree(4).allData()) | ||
} | ||
@Test("All data for one element", .enabled(if: RUNALL)) | ||
func testAllDataForOneElement() { | ||
#expect([4] == BinarySearchTree(4).allData()) | ||
} | ||
|
||
func testAllDataForSmallerElement() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
XCTAssertEqual([2, 4], four.allData()) | ||
} | ||
@Test("All data for smaller element", .enabled(if: RUNALL)) | ||
func testAllDataForSmallerElement() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
#expect([2, 4] == four.allData()) | ||
} | ||
|
||
func testAllDataForLargerElement() { | ||
var four = BinarySearchTree(4) | ||
four.insert(5) | ||
XCTAssertEqual([4, 5], four.allData()) | ||
} | ||
@Test("All data for larger element", .enabled(if: RUNALL)) | ||
func testAllDataForLargerElement() { | ||
var four = BinarySearchTree(4) | ||
four.insert(5) | ||
#expect([4, 5] == four.allData()) | ||
} | ||
|
||
func testAllDataForComplexTree() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
four.insert(1) | ||
four.insert(3) | ||
four.insert(6) | ||
four.insert(7) | ||
four.insert(5) | ||
XCTAssertEqual([1, 2, 3, 4, 5, 6, 7], four.allData()) | ||
} | ||
@Test("All data for complex tree", .enabled(if: RUNALL)) | ||
func testAllDataForComplexTree() { | ||
var four = BinarySearchTree(4) | ||
four.insert(2) | ||
four.insert(1) | ||
four.insert(3) | ||
four.insert(6) | ||
four.insert(7) | ||
four.insert(5) | ||
#expect([1, 2, 3, 4, 5, 6, 7] == four.allData()) | ||
} | ||
} |
Oops, something went wrong.