🐣/자료구조(9)
-
[Swift] (자료구조) 해쉬 / Hash
보호되어 있는 글입니다.
2024.03.06 -
[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 Foundation struct 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("\n") } func printPreorder() { print("\n==== Preorder ====") self.printPreorder(node: self..
2022.09.01 -
[Swift] (자료구조) 우선순위 큐 / Priority Queue
import Foundation struct 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 () { while !heap.isEmpty { _ = heap.remove() } } mutating func pop() -> T? { return h..
2022.08.26 -
[Swift] (자료구조) 큐 / Queue
import Foundation class 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 Foundation s..
2022.08.06