Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// BreadthFirstSearch.swift
// swift-ds-algorithms
//
// Created by Aaron on 8/16/26.
// Copyright © 2026. All rights reserved.
//

public enum BreadthFirstSearch {
public static func bfsValues(root: BinaryTreeNode<Int>?) -> [Int] {
var values = [Int]()

guard let root else {
return values
}

let queue = Queue<BinaryTreeNode<Int>>()
queue.enqueue(root)

while let node = queue.deque() {
values.append(node.value)
if let left = node.left {
queue.enqueue(left)
}
if let right = node.right {
queue.enqueue(right)
}
}

return values
}
}
51 changes: 51 additions & 0 deletions Sources/swift-ds-algorithms/BinaryTree/DepthFirstSearch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// DepthFirstSearch.swift
// swift-ds-algorithms
//
// Created by Aaron on 8/16/26.
// Copyright © 2026. All rights reserved.
//

public enum DepthFirstSearch {
public static func dfsRecursive(root: BinaryTreeNode<Int>?) -> [Int] {
guard let root else {
return []
}

var values = [root.value]

if let left = root.left {
values.append(contentsOf: dfsRecursive(root: left))
}

if let right = root.right {
values.append(contentsOf: dfsRecursive(root: right))
}

return values
}

public static func dfs(root: BinaryTreeNode<Int>?) -> [Int] {
var values = [Int]()

guard let root else {
return values
}

var stack = [root]

while stack.isEmpty == false {
let node = stack.removeLast()
values.append(node.value)

if let right = node.right {
stack.append(right)
}

if let left = node.left {
stack.append(left)
}
}

return values
}
}
43 changes: 43 additions & 0 deletions Sources/swift-ds-algorithms/BinaryTree/TreeSum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// TreeSum.swift
// swift-ds-algorithms
//
// Created by Aaron on 8/17/26.
// Copyright © 2026. All rights reserved.
//

public enum TreeSum {
public static func treeSumIterative(root: BinaryTreeNode<Int>?) -> Int {
var total = 0

guard let root else {
return total
}

var stack = [root]

while let node = stack.popLast() {
total += node.value

if let right = node.right {
stack.append(right)
}

if let left = node.left {
stack.append(left)
}
}

return total
}

public static func treeSumRecursive(root: BinaryTreeNode<Int>?) -> Int {
guard let root else {
return 0
}

var total = root.value
total += treeSumRecursive(root: root.left)
total += treeSumRecursive(root: root.right)
return total
}
}
87 changes: 0 additions & 87 deletions Sources/swift-ds-algorithms/Helpers.swift

This file was deleted.

88 changes: 88 additions & 0 deletions Sources/swift-ds-algorithms/TestData/BinaryTreeData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// BinaryTree.swift
// swift-ds-algorithms
//
// Created by Aaron on 8/17/26.
// Copyright © 2026. All rights reserved.
//

public enum BinaryTreeData {
public static func balanced(depth: Int) -> BinaryTreeNode<Int>? {
guard depth >= 0 else { return nil }

let counter = IntegerCounter()
let root = BinaryTreeNode(value: counter.next())
var currentDepth = 0

let queue = Queue<BinaryTreeNode<Int>>()
queue.enqueue(root)

while currentDepth < depth {
let count = queue.count

for _ in 0..<count {
guard let node = queue.deque() else {
continue
}
let left = BinaryTreeNode(value: counter.next())
let right = BinaryTreeNode(value: counter.next())
queue.enqueue(left)
queue.enqueue(right)
node.left = left
node.right = right
}
currentDepth += 1
}

return root
}

public static func inOrderValues<T>(root: BinaryTreeNode<T>?) -> [T] {
return inOrder(node: root)
}

private static func inOrder<T>(node: BinaryTreeNode<T>?) -> [T] {
var values = [T]()

guard let node else {
return values
}

values.append(contentsOf: inOrder(node: node.left))
values.append(node.value)
values.append(contentsOf: inOrder(node: node.right))
return values
}

public static func levelOrderValues<T>(root: BinaryTreeNode<T>?) -> [T] {
var result = [T]()

guard let root else {
return result
}

let queue = Queue<BinaryTreeNode<T>>()
queue.enqueue(root)

while let node = queue.deque() {
result.append(node.value)
if let left = node.left {
queue.enqueue(left)
}
if let right = node.right {
queue.enqueue(right)
}
}

return result
}

public static func unbalanced() -> BinaryTreeNode<Int> {
let c = BinaryTreeNode(value: 3)
let d = BinaryTreeNode(value: 4)
let e = BinaryTreeNode(value: 5)
let a = BinaryTreeNode(value: 1, left: c, right: d)
let b = BinaryTreeNode(value: 2, left: nil, right: e)
let root = BinaryTreeNode(value: 0, left: a, right: b)
return root
}
}
36 changes: 36 additions & 0 deletions Sources/swift-ds-algorithms/TestData/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Helpers.swift
// swift-algorithmsTests
//
// Created by Aaron on 8/1/17.
//

import Foundation
import GameKit

public enum Helpers {
public enum HelperError: Error, CustomStringConvertible {
case invalidInput(message: String)

public var description: String {
switch self {
case .invalidInput(message: let message):
return message
}
}
}

public static func generateRandomString(ofLength length: Int = 1000000) -> String {
let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
let charCount = characters.count
var wordArray = [Character](repeating: "0", count: length)
let randomGenerator = GKRandomSource.sharedRandom()

for index in 0..<length {
let charIndex = characters.index(characters.startIndex,
offsetBy: randomGenerator.nextInt(upperBound: charCount))
wordArray[index] = characters[charIndex]
}
return String(wordArray)
}
}
19 changes: 19 additions & 0 deletions Sources/swift-ds-algorithms/TestData/IntegerCounter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// NumberGenerator.swift
// swift-ds-algorithms
//
// Created by Aaron on 8/17/26.
// Copyright © 2026. All rights reserved.
//

public class IntegerCounter {
private var current: Int

public init(start: Int = 0) {
self.current = start
}

public func next() -> Int {
defer { current += 1 }
return current
}
}
Loading