Queue, Binary Tree and BFS in iOS Swift
Following on from my last post, this is a short post with code samples to achieve the above on Swift. I won’t talk too much about the background to Binary Search Trees or the BFS algorithm, instead just focus on the code. If you need to get more of a background to this post, then best have a read of my previous post, Breadth-First Search in Typescript and Javascript. The code Just like my previous post , this code solves the exact same problem, except here it’s done in Swift instead of Typescript or Javascript. class Node<T: Comparable> { var key: T! var left: Node? var right: Node? init(key:T) { self.key = key } } class Queue<T> { private var items = [T]() func add(item: T) { items.append(item) } func pop() -> T? { return items.removeFirst() } func isEmpty() -> Bool { return (items.count == 0) } } class BinaryTree<H:Comparable> { private var rootNode: Node...