Created
August 23, 2019 01:01
-
-
Save PaulWoodIII/a4e33b2a180b68ee8afbcc2a6b5f544d to your computer and use it in GitHub Desktop.
Binary Search visualized on the Apple Watch, Swiftsy Bitsy Datastructures and Algorithms
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
// | |
// BinarySearchView.swift | |
// WatchSort WatchKit Extension | |
// | |
// Created by Paul Wood on 8/22/19. | |
// Copyright © 2019 Paul Wood. All rights reserved. | |
// | |
import SwiftUI | |
import Combine | |
class BinarySearchAlgorithmDisplayable: ObservableObject { | |
var displayResults: [[BinarySearchNode<Int>]]! | |
let count: Int = 15 | |
var values: [BinarySearchNode<Int>] { | |
return displayResults[currentIndex] | |
} | |
func displayNext() { | |
guard !isSortedInternal else { return } | |
objectWillChange.send() | |
currentIndex += 1 | |
if currentIndex >= displayResults.count - 1 { | |
isSortedInternal = true | |
} | |
} | |
var isSorted: Bool { | |
return isSortedInternal | |
} | |
private var currentIndex = 0 | |
private var isSortedInternal = false | |
public var objectWillChange = PassthroughSubject<Void, Never>() | |
private var search: Int = 1 | |
public var searching: Binding<Double>! | |
init() { | |
let b = Binding<Double>( | |
get:{ | |
return Double(self.search) | |
}, set: { | |
self.objectWillChange.send() | |
self.search = Int(round($0)) | |
self.resultsForTree(searching: Int(round($0))) | |
}) | |
self.searching = b | |
resultsForTree(searching: search) | |
} | |
func resultsForTree(searching: Int){ | |
currentIndex = 0 | |
isSortedInternal = false | |
let tree = BinarySearchTree<Int>(value: 08) | |
_ = ([04,12,02,06,10,14,01,03,05,07,09,11,13,15]).map { v in | |
tree.insert(value: v) | |
} | |
displayResults = BinarySearchAlgorithmDisplayable.recordSearch(tree, | |
value: searching) | |
} | |
} | |
extension BinarySearchAlgorithmDisplayable { | |
static func recordSearch<T>(_ tree: BinarySearchTree<T>, | |
value: T) -> [[BinarySearchNode<T>]] { | |
var results = [[BinarySearchNode<T>]]() | |
var node: BinarySearchTree? = tree | |
func createRecord(tree: BinarySearchTree<T>, | |
currentNodeValue: T) -> [BinarySearchNode<T>] { | |
var list = [BinarySearchNode<T>]() | |
tree.traversePreOrder { value in | |
list.append(BinarySearchNode<T>(index: 0, | |
content: value, | |
wasSearched: false, | |
isSearched: value == currentNodeValue)) | |
} | |
return list | |
} | |
while let n = node { | |
results.append(createRecord(tree: tree, currentNodeValue: n.value)) | |
if value < n.value { | |
node = n.left | |
} else if value > n.value { | |
node = n.right | |
} else { | |
break | |
//return node | |
} | |
} | |
return results | |
} | |
} | |
struct BinarySearchNode<T: Comparable> { | |
var index: Int | |
var content: T | |
var wasSearched: Bool | |
var isSearched: Bool | |
} | |
struct BinarySearchView: View { | |
@ObservedObject var bsAlgorithm = BinarySearchAlgorithmDisplayable() | |
private func handleTap() { | |
bsAlgorithm.displayNext() | |
} | |
@State var quantity: Double = 0 | |
var body: some View { | |
VStack{ | |
HStack{ | |
IndexView(bsAlgorithm.values[0]) | |
} | |
HStack{ | |
Spacer() | |
IndexView(bsAlgorithm.values[1]) | |
Spacer() | |
IndexView(bsAlgorithm.values[8]) | |
Spacer() | |
} | |
HStack{ | |
Spacer() | |
IndexView(bsAlgorithm.values[2]) | |
Spacer() | |
IndexView(bsAlgorithm.values[5]) | |
Spacer() | |
IndexView(bsAlgorithm.values[9]) | |
Spacer() | |
IndexView(bsAlgorithm.values[12]) | |
Spacer() | |
} | |
HStack(spacing:1){ | |
IndexView(bsAlgorithm.values[3]) | |
IndexView(bsAlgorithm.values[4]) | |
IndexView(bsAlgorithm.values[6]) | |
IndexView(bsAlgorithm.values[7]) | |
IndexView(bsAlgorithm.values[10]) | |
IndexView(bsAlgorithm.values[11]) | |
IndexView(bsAlgorithm.values[13]) | |
IndexView(bsAlgorithm.values[14]) | |
} | |
Slider(value: bsAlgorithm.searching, in: ClosedRange(uncheckedBounds: (1,15)), step: 1) | |
}.navigationBarTitle( Text("Binary Search: \(Int(bsAlgorithm.searching.wrappedValue))")) | |
.onTapGesture(perform: handleTap) | |
.animation(.default) | |
} | |
private func IndexView(_ node: BinarySearchNode<Int>) -> some View { | |
return Text(String(node.content)) | |
.frame(width: 18, height: 20, alignment: .center) | |
.background( | |
RoundedRectangle(cornerRadius: 1.0) | |
.border(node.isSearched ? Color.green : Color.white, width: 1) | |
.foregroundColor(Color.gray) | |
) | |
} | |
} | |
struct BinarySearchView_Previews: PreviewProvider { | |
static var previews: some View { | |
BinarySearchView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment