🐣(75)
-
[Swift] (자료구조) 이진 탐색 트리 / Binary Search Tree
import Foundation struct BinarySearchTree { var root: TreeNode? mutating func add(data: T) { let node = TreeNode(data: data) if root == nil { self.root = node return } self.add(newNode: node, under: root) } func search(data: T) -> TreeNode? { return self.search(data: data, in: root) } func delete(data: T) -> T? { return delete(data: data, at: root) } private func delete(data: T, at node: TreeNod..
2022.09.01 -
[Swift] (자료구조) 트리와 이진 트리 / Binary Tree
import Foundationstruct BinaryTree { var root: TreeNode? mutating func add(data: T) { let treeNode = TreeNode(data: data) if self.root == nil { self.root = treeNode return } self.add(newNode: treeNode, to: root!) } func printInorder() { print("\n==== Inorder ====") self.printInorder(node: self.root) print(..
2022.09.01 -
[Swift] (자료구조) 우선순위 큐 / Priority Queue
import Foundationstruct PriorityQueue { var heap: Heap init(_ elements: [T] = [], _ sort: @escaping (T, T) -> Bool) { heap = Heap(elements: elements, sortFunction: sort) } var count : Int { return heap.count } var isEmpty : Bool { return heap.isEmpty } func top () -> T? { return heap.peek } mutating func clear () { whil..
2022.08.26 -
[Swift] (자료구조) 큐 / Queue
import Foundationclass Queue { var enQueue: [T] = [] var deQueue: [T] = [] var count: Int { return enQueue.count + deQueue.count } var isEmpty: Bool { return enQueue.isEmpty && deQueue.isEmpty } init() { } func push(_ element: T) { enQueue.append(element) } func pop() -> T { if deQueue.isEmpty { deQueue = enQueue.reversed() enQueue.removeAll() } return deQueue.popLast()! }} import Foun..
2022.08.06 -
[Swift] (자료구조) 힙 / Heap
import Foundation struct Heap { private var elements: [T] = [] private let sortFunction: (T, T) -> Bool var isEmpty: Bool { return self.elements.count == 1 } var peek: T? { if self.isEmpty { return nil } return self.elements.last! } var count: Int { return self.elements.count - 1 } init(elements: [T] = [], sortFunction: @escaping (T, T) -> Bool) { if !elements.isEmpty { self.elements = [elements..
2022.08.01