├── .gitignore ├── BinarySearchTree ├── BinarySearchTree.xcodeproj │ └── project.pbxproj └── BinarySearchTree │ ├── BinarySearchTree.swift │ ├── TreeNode.swift │ └── main.swift ├── BinaryTree ├── BinaryTree.xcodeproj │ └── project.pbxproj └── BinaryTree │ ├── BinaryTree.swift │ ├── TreeNode.swift │ └── main.swift ├── DoublyLinkedList ├── DoublyLinkedList.xcodeproj │ └── project.pbxproj └── DoublyLinkedList │ ├── DoublyLinkedList.swift │ ├── Node.swift │ └── main.swift ├── Heap ├── Heap.xcodeproj │ └── project.pbxproj └── Heap │ ├── Heap.swift │ └── main.swift ├── LinkedList ├── LinkedList.xcodeproj │ └── project.pbxproj └── LinkedList │ ├── LinkedList.swift │ ├── Node.swift │ └── main.swift ├── PriorityQueue ├── PriorityQueue.xcodeproj │ └── project.pbxproj └── PriorityQueue │ ├── Heap.swift │ ├── PriorityQueue.swift │ └── main.swift ├── Queue ├── Queue.xcodeproj │ └── project.pbxproj └── Queue │ ├── Node.swift │ ├── Queue.swift │ ├── TwoPointerLinkedList.swift │ └── main.swift ├── README.md └── Stack ├── Stack.xcodeproj └── project.pbxproj └── Stack └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # OS X Finder 2 | .DS_Store 3 | 4 | # Xcode per-user config 5 | *.mode1 6 | *.mode1v3 7 | *.mode2v3 8 | *.perspective 9 | *.perspectivev3 10 | *.pbxuser 11 | *.xcworkspace 12 | xcuserdata 13 | 14 | # Build products 15 | build/ 16 | *.o 17 | *.LinkFileList 18 | *.hmap 19 | 20 | # Automatic backup files 21 | *~.nib/ 22 | *.swp 23 | *~ 24 | *.dat 25 | *.dep -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047B212765DD6600CFB38A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047B202765DD6600CFB38A /* main.swift */; }; 11 | B0047B282765DDC100CFB38A /* TreeNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047B272765DDC100CFB38A /* TreeNode.swift */; }; 12 | B0047B2A2765DDF100CFB38A /* BinarySearchTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047B292765DDF100CFB38A /* BinarySearchTree.swift */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | B0047B1B2765DD6600CFB38A /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | ); 23 | runOnlyForDeploymentPostprocessing = 1; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | B0047B1D2765DD6600CFB38A /* BinarySearchTree */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = BinarySearchTree; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | B0047B202765DD6600CFB38A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 30 | B0047B272765DDC100CFB38A /* TreeNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreeNode.swift; sourceTree = ""; }; 31 | B0047B292765DDF100CFB38A /* BinarySearchTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinarySearchTree.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | B0047B1A2765DD6600CFB38A /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | B0047B142765DD6600CFB38A = { 46 | isa = PBXGroup; 47 | children = ( 48 | B0047B1F2765DD6600CFB38A /* BinarySearchTree */, 49 | B0047B1E2765DD6600CFB38A /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | B0047B1E2765DD6600CFB38A /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | B0047B1D2765DD6600CFB38A /* BinarySearchTree */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | B0047B1F2765DD6600CFB38A /* BinarySearchTree */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | B0047B202765DD6600CFB38A /* main.swift */, 65 | B0047B272765DDC100CFB38A /* TreeNode.swift */, 66 | B0047B292765DDF100CFB38A /* BinarySearchTree.swift */, 67 | ); 68 | path = BinarySearchTree; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | B0047B1C2765DD6600CFB38A /* BinarySearchTree */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = B0047B242765DD6600CFB38A /* Build configuration list for PBXNativeTarget "BinarySearchTree" */; 77 | buildPhases = ( 78 | B0047B192765DD6600CFB38A /* Sources */, 79 | B0047B1A2765DD6600CFB38A /* Frameworks */, 80 | B0047B1B2765DD6600CFB38A /* CopyFiles */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = BinarySearchTree; 87 | productName = BinarySearchTree; 88 | productReference = B0047B1D2765DD6600CFB38A /* BinarySearchTree */; 89 | productType = "com.apple.product-type.tool"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | B0047B152765DD6600CFB38A /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | BuildIndependentTargetsInParallel = 1; 98 | LastSwiftUpdateCheck = 1310; 99 | LastUpgradeCheck = 1310; 100 | TargetAttributes = { 101 | B0047B1C2765DD6600CFB38A = { 102 | CreatedOnToolsVersion = 13.1; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = B0047B182765DD6600CFB38A /* Build configuration list for PBXProject "BinarySearchTree" */; 107 | compatibilityVersion = "Xcode 13.0"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = B0047B142765DD6600CFB38A; 115 | productRefGroup = B0047B1E2765DD6600CFB38A /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | B0047B1C2765DD6600CFB38A /* BinarySearchTree */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXSourcesBuildPhase section */ 125 | B0047B192765DD6600CFB38A /* Sources */ = { 126 | isa = PBXSourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | B0047B212765DD6600CFB38A /* main.swift in Sources */, 130 | B0047B282765DDC100CFB38A /* TreeNode.swift in Sources */, 131 | B0047B2A2765DDF100CFB38A /* BinarySearchTree.swift in Sources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXSourcesBuildPhase section */ 136 | 137 | /* Begin XCBuildConfiguration section */ 138 | B0047B222765DD6600CFB38A /* Debug */ = { 139 | isa = XCBuildConfiguration; 140 | buildSettings = { 141 | ALWAYS_SEARCH_USER_PATHS = NO; 142 | CLANG_ANALYZER_NONNULL = YES; 143 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 144 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 145 | CLANG_CXX_LIBRARY = "libc++"; 146 | CLANG_ENABLE_MODULES = YES; 147 | CLANG_ENABLE_OBJC_ARC = YES; 148 | CLANG_ENABLE_OBJC_WEAK = YES; 149 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 150 | CLANG_WARN_BOOL_CONVERSION = YES; 151 | CLANG_WARN_COMMA = YES; 152 | CLANG_WARN_CONSTANT_CONVERSION = YES; 153 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 154 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 155 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 156 | CLANG_WARN_EMPTY_BODY = YES; 157 | CLANG_WARN_ENUM_CONVERSION = YES; 158 | CLANG_WARN_INFINITE_RECURSION = YES; 159 | CLANG_WARN_INT_CONVERSION = YES; 160 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 161 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 162 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 163 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 164 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 165 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 166 | CLANG_WARN_STRICT_PROTOTYPES = YES; 167 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 168 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 169 | CLANG_WARN_UNREACHABLE_CODE = YES; 170 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 171 | COPY_PHASE_STRIP = NO; 172 | DEBUG_INFORMATION_FORMAT = dwarf; 173 | ENABLE_STRICT_OBJC_MSGSEND = YES; 174 | ENABLE_TESTABILITY = YES; 175 | GCC_C_LANGUAGE_STANDARD = gnu11; 176 | GCC_DYNAMIC_NO_PIC = NO; 177 | GCC_NO_COMMON_BLOCKS = YES; 178 | GCC_OPTIMIZATION_LEVEL = 0; 179 | GCC_PREPROCESSOR_DEFINITIONS = ( 180 | "DEBUG=1", 181 | "$(inherited)", 182 | ); 183 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 184 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 185 | GCC_WARN_UNDECLARED_SELECTOR = YES; 186 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 187 | GCC_WARN_UNUSED_FUNCTION = YES; 188 | GCC_WARN_UNUSED_VARIABLE = YES; 189 | MACOSX_DEPLOYMENT_TARGET = 12.0; 190 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 191 | MTL_FAST_MATH = YES; 192 | ONLY_ACTIVE_ARCH = YES; 193 | SDKROOT = macosx; 194 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 195 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 196 | }; 197 | name = Debug; 198 | }; 199 | B0047B232765DD6600CFB38A /* Release */ = { 200 | isa = XCBuildConfiguration; 201 | buildSettings = { 202 | ALWAYS_SEARCH_USER_PATHS = NO; 203 | CLANG_ANALYZER_NONNULL = YES; 204 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 205 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 206 | CLANG_CXX_LIBRARY = "libc++"; 207 | CLANG_ENABLE_MODULES = YES; 208 | CLANG_ENABLE_OBJC_ARC = YES; 209 | CLANG_ENABLE_OBJC_WEAK = YES; 210 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 211 | CLANG_WARN_BOOL_CONVERSION = YES; 212 | CLANG_WARN_COMMA = YES; 213 | CLANG_WARN_CONSTANT_CONVERSION = YES; 214 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 215 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 216 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 217 | CLANG_WARN_EMPTY_BODY = YES; 218 | CLANG_WARN_ENUM_CONVERSION = YES; 219 | CLANG_WARN_INFINITE_RECURSION = YES; 220 | CLANG_WARN_INT_CONVERSION = YES; 221 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 222 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 223 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 226 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 227 | CLANG_WARN_STRICT_PROTOTYPES = YES; 228 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 229 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 230 | CLANG_WARN_UNREACHABLE_CODE = YES; 231 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 232 | COPY_PHASE_STRIP = NO; 233 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 234 | ENABLE_NS_ASSERTIONS = NO; 235 | ENABLE_STRICT_OBJC_MSGSEND = YES; 236 | GCC_C_LANGUAGE_STANDARD = gnu11; 237 | GCC_NO_COMMON_BLOCKS = YES; 238 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 239 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 240 | GCC_WARN_UNDECLARED_SELECTOR = YES; 241 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 242 | GCC_WARN_UNUSED_FUNCTION = YES; 243 | GCC_WARN_UNUSED_VARIABLE = YES; 244 | MACOSX_DEPLOYMENT_TARGET = 12.0; 245 | MTL_ENABLE_DEBUG_INFO = NO; 246 | MTL_FAST_MATH = YES; 247 | SDKROOT = macosx; 248 | SWIFT_COMPILATION_MODE = wholemodule; 249 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 250 | }; 251 | name = Release; 252 | }; 253 | B0047B252765DD6600CFB38A /* Debug */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | CODE_SIGN_STYLE = Automatic; 257 | DEVELOPMENT_TEAM = CH6R3C4FBD; 258 | ENABLE_HARDENED_RUNTIME = YES; 259 | PRODUCT_NAME = "$(TARGET_NAME)"; 260 | SWIFT_VERSION = 5.0; 261 | }; 262 | name = Debug; 263 | }; 264 | B0047B262765DD6600CFB38A /* Release */ = { 265 | isa = XCBuildConfiguration; 266 | buildSettings = { 267 | CODE_SIGN_STYLE = Automatic; 268 | DEVELOPMENT_TEAM = CH6R3C4FBD; 269 | ENABLE_HARDENED_RUNTIME = YES; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | SWIFT_VERSION = 5.0; 272 | }; 273 | name = Release; 274 | }; 275 | /* End XCBuildConfiguration section */ 276 | 277 | /* Begin XCConfigurationList section */ 278 | B0047B182765DD6600CFB38A /* Build configuration list for PBXProject "BinarySearchTree" */ = { 279 | isa = XCConfigurationList; 280 | buildConfigurations = ( 281 | B0047B222765DD6600CFB38A /* Debug */, 282 | B0047B232765DD6600CFB38A /* Release */, 283 | ); 284 | defaultConfigurationIsVisible = 0; 285 | defaultConfigurationName = Release; 286 | }; 287 | B0047B242765DD6600CFB38A /* Build configuration list for PBXNativeTarget "BinarySearchTree" */ = { 288 | isa = XCConfigurationList; 289 | buildConfigurations = ( 290 | B0047B252765DD6600CFB38A /* Debug */, 291 | B0047B262765DD6600CFB38A /* Release */, 292 | ); 293 | defaultConfigurationIsVisible = 0; 294 | defaultConfigurationName = Release; 295 | }; 296 | /* End XCConfigurationList section */ 297 | }; 298 | rootObject = B0047B152765DD6600CFB38A /* Project object */; 299 | } 300 | -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree/BinarySearchTree.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BinarySearchTree.swift 3 | // BinarySearchTree 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct BinarySearchTree { 11 | var root: TreeNode? 12 | 13 | mutating func add(data: T) { 14 | let node = TreeNode(data: data) 15 | if root == nil { 16 | self.root = node 17 | return 18 | } 19 | self.add(newNode: node, under: root) 20 | } 21 | 22 | func search(data: T) -> TreeNode? { 23 | return self.search(data: data, in: root) 24 | } 25 | 26 | func delete(data: T) -> T? { 27 | return delete(data: data, at: root) 28 | } 29 | 30 | private func delete(data: T, at node: TreeNode?) -> T? { 31 | guard let targetNode = self.search(data: data, in: root) else { return nil } 32 | 33 | // target is leaf node 34 | if targetNode.isLeafNode { 35 | let parent = self.findParent(of: targetNode, under: root) 36 | parent?.rightChild === targetNode 37 | ? (parent?.rightChild = nil) 38 | : (parent?.leftChild = nil) 39 | return targetNode.data 40 | } 41 | 42 | // target has single child node 43 | if targetNode.hasSingleChild { 44 | let child = targetNode.leftChild != nil ? targetNode.leftChild : targetNode.rightChild 45 | guard let parent = self.findParent(of: targetNode, under: root) else { return nil } 46 | parent.data > child!.data ? (parent.leftChild = child) : (parent.rightChild = child) 47 | return targetNode.data 48 | } 49 | 50 | // target has two child nodes 51 | if targetNode.hasTwoChild { 52 | let deleted = targetNode.data 53 | guard let successor = self.findSuccessor(of: targetNode.rightChild) else { return nil } 54 | if !successor.isLeafNode { 55 | let parent = self.findParent(of: successor, under: root) 56 | parent?.leftChild = successor.rightChild 57 | } 58 | _ = self.delete(data: successor.data) 59 | targetNode.update(data: successor.data) 60 | return deleted 61 | } 62 | 63 | return nil 64 | } 65 | 66 | private func findSuccessor(of node: TreeNode?) -> TreeNode? { 67 | if node?.leftChild == nil { return node } 68 | return self.findSuccessor(of: node?.leftChild) 69 | } 70 | 71 | private func findParent(of node: TreeNode, under parentNode: TreeNode?) -> TreeNode? { 72 | if parentNode == nil { return nil } 73 | if parentNode?.leftChild === node || parentNode?.rightChild === node { 74 | return parentNode 75 | } 76 | if let parent = findParent(of: node, under: parentNode?.leftChild) { 77 | return parent 78 | } 79 | return findParent(of: node, under: parentNode?.rightChild) 80 | } 81 | 82 | private func search(data: T, in node: TreeNode?) -> TreeNode? { 83 | if node == nil || node?.data == data { return node } 84 | 85 | return node!.data > data 86 | ? search(data: data, in: node?.leftChild) 87 | : search(data: data, in: node?.rightChild) 88 | } 89 | 90 | @discardableResult 91 | private func add(newNode: TreeNode, under parentNode: TreeNode?) -> TreeNode? { 92 | guard newNode.data != parentNode?.data else { return nil } 93 | if parentNode == nil { 94 | return newNode 95 | } 96 | parentNode!.data > newNode.data 97 | ? (parentNode?.leftChild = add(newNode: newNode, under: parentNode?.leftChild)) 98 | : (parentNode?.rightChild = add(newNode: newNode, under: parentNode?.rightChild)) 99 | return parentNode 100 | } 101 | 102 | func printTree() { 103 | print(root!.asString) 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree/TreeNode.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TreeNode.swift 3 | // BinarySearchTree 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | final class TreeNode { 11 | private(set) var data: T 12 | var leftChild: TreeNode? 13 | var rightChild: TreeNode? 14 | var isLeafNode: Bool { 15 | return self.leftChild == nil && self.rightChild == nil 16 | } 17 | var hasSingleChild: Bool { 18 | return (self.leftChild != nil && self.rightChild == nil) 19 | || (self.leftChild == nil && self.rightChild != nil) 20 | } 21 | var hasTwoChild: Bool { 22 | return self.leftChild != nil && self.rightChild != nil 23 | } 24 | 25 | init(data: T) { 26 | self.data = data 27 | } 28 | 29 | func update(data: T) { 30 | self.data = data 31 | } 32 | 33 | var asString:String { return treeString(self){("\($0.data)",$0.leftChild,$0.rightChild)} } 34 | } 35 | 36 | // https://stackoverflow.com/questions/43898440/how-to-draw-a-binary-tree-in-console/43903427 37 | public func treeString(_ node:T, reversed:Bool=false, isTop:Bool=true, using nodeInfo:(T)->(String,T?,T?)) -> String 38 | { 39 | // node value string and sub nodes 40 | let (stringValue, leftNode, rightNode) = nodeInfo(node) 41 | 42 | let stringValueWidth = stringValue.count 43 | 44 | // recurse to sub nodes to obtain line blocks on left and right 45 | let leftTextBlock = leftNode == nil ? [] 46 | : treeString(leftNode!,reversed:reversed,isTop:false,using:nodeInfo) 47 | .components(separatedBy:"\n") 48 | 49 | let rightTextBlock = rightNode == nil ? [] 50 | : treeString(rightNode!,reversed:reversed,isTop:false,using:nodeInfo) 51 | .components(separatedBy:"\n") 52 | 53 | // count common and maximum number of sub node lines 54 | let commonLines = min(leftTextBlock.count,rightTextBlock.count) 55 | let subLevelLines = max(rightTextBlock.count,leftTextBlock.count) 56 | 57 | // extend lines on shallower side to get same number of lines on both sides 58 | let leftSubLines = leftTextBlock 59 | + Array(repeating:"", count: subLevelLines-leftTextBlock.count) 60 | let rightSubLines = rightTextBlock 61 | + Array(repeating:"", count: subLevelLines-rightTextBlock.count) 62 | 63 | // compute location of value or link bar for all left and right sub nodes 64 | // * left node's value ends at line's width 65 | // * right node's value starts after initial spaces 66 | let leftLineWidths = leftSubLines.map{$0.count} 67 | let rightLineIndents = rightSubLines.map{$0.prefix{$0==" "}.count } 68 | 69 | // top line value locations, will be used to determine position of current node & link bars 70 | let firstLeftWidth = leftLineWidths.first ?? 0 71 | let firstRightIndent = rightLineIndents.first ?? 0 72 | 73 | 74 | // width of sub node link under node value (i.e. with slashes if any) 75 | // aims to center link bars under the value if value is wide enough 76 | // 77 | // ValueLine: v vv vvvvvv vvvvv 78 | // LinkLine: / \ / \ / \ / \ 79 | // 80 | let linkSpacing = min(stringValueWidth, 2 - stringValueWidth % 2) 81 | let leftLinkBar = leftNode == nil ? 0 : 1 82 | let rightLinkBar = rightNode == nil ? 0 : 1 83 | let minLinkWidth = leftLinkBar + linkSpacing + rightLinkBar 84 | let valueOffset = (stringValueWidth - linkSpacing) / 2 85 | 86 | // find optimal position for right side top node 87 | // * must allow room for link bars above and between left and right top nodes 88 | // * must not overlap lower level nodes on any given line (allow gap of minSpacing) 89 | // * can be offset to the left if lower subNodes of right node 90 | // have no overlap with subNodes of left node 91 | let minSpacing = 2 92 | let rightNodePosition = zip(leftLineWidths,rightLineIndents[0..() 11 | 12 | tree.add(data: 6) 13 | tree.add(data: 5) 14 | tree.add(data: 1) 15 | tree.add(data: 2) 16 | tree.add(data: 4) 17 | tree.add(data: 0) 18 | tree.add(data: 3) 19 | tree.add(data: 9) 20 | tree.add(data: 7) 21 | tree.add(data: 8) 22 | tree.add(data: 10) 23 | tree.add(data: 16) 24 | tree.add(data: 12) 25 | tree.add(data: 13) 26 | 27 | 28 | 29 | tree.printTree() 30 | 31 | // 1 32 | // / \ 33 | // 0 2 34 | // \ 35 | // 5 36 | // / \ 37 | // 3 7 38 | // \ \ 39 | // 4 10 40 | 41 | let searchedNode = tree.search(data: 2) 42 | print(searchedNode!.data) // 2 43 | print(tree.delete(data: 10)) 44 | tree.printTree() 45 | print(tree.delete(data: 5)) 46 | tree.printTree() 47 | print(tree.delete(data: 9)) 48 | tree.printTree() 49 | 50 | //Optional(9) 51 | // 6 52 | // ___/ \__ 53 | // 1 12 54 | // / \ __/ \_ 55 | // 0 2 7 16 56 | // \ \ / 57 | // 4 8 13 58 | // / 59 | // 3 60 | -------------------------------------------------------------------------------- /BinaryTree/BinaryTree.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047AF02765CAB000CFB38A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AEF2765CAB000CFB38A /* main.swift */; }; 11 | B0047AF72765CAB700CFB38A /* TreeNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AF62765CAB700CFB38A /* TreeNode.swift */; }; 12 | B0047AF92765CB1100CFB38A /* BinaryTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AF82765CB1100CFB38A /* BinaryTree.swift */; }; 13 | B0047B112765D74100CFB38A /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047B0D2765D72000CFB38A /* Queue.swift */; }; 14 | B0047B122765D74300CFB38A /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047B0F2765D72000CFB38A /* Node.swift */; }; 15 | B0047B132765D74500CFB38A /* TwoPointerLinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047B102765D72000CFB38A /* TwoPointerLinkedList.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXCopyFilesBuildPhase section */ 19 | B0047AEA2765CAB000CFB38A /* CopyFiles */ = { 20 | isa = PBXCopyFilesBuildPhase; 21 | buildActionMask = 2147483647; 22 | dstPath = /usr/share/man/man1/; 23 | dstSubfolderSpec = 0; 24 | files = ( 25 | ); 26 | runOnlyForDeploymentPostprocessing = 1; 27 | }; 28 | /* End PBXCopyFilesBuildPhase section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | B0047AEC2765CAB000CFB38A /* BinaryTree */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = BinaryTree; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | B0047AEF2765CAB000CFB38A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 33 | B0047AF62765CAB700CFB38A /* TreeNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreeNode.swift; sourceTree = ""; }; 34 | B0047AF82765CB1100CFB38A /* BinaryTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryTree.swift; sourceTree = ""; }; 35 | B0047B0D2765D72000CFB38A /* Queue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queue.swift; sourceTree = ""; }; 36 | B0047B0F2765D72000CFB38A /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; 37 | B0047B102765D72000CFB38A /* TwoPointerLinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPointerLinkedList.swift; sourceTree = ""; }; 38 | /* End PBXFileReference section */ 39 | 40 | /* Begin PBXFrameworksBuildPhase section */ 41 | B0047AE92765CAB000CFB38A /* Frameworks */ = { 42 | isa = PBXFrameworksBuildPhase; 43 | buildActionMask = 2147483647; 44 | files = ( 45 | ); 46 | runOnlyForDeploymentPostprocessing = 0; 47 | }; 48 | /* End PBXFrameworksBuildPhase section */ 49 | 50 | /* Begin PBXGroup section */ 51 | B0047AE32765CAB000CFB38A = { 52 | isa = PBXGroup; 53 | children = ( 54 | B0047B0C2765D72000CFB38A /* Queue */, 55 | B0047AEE2765CAB000CFB38A /* BinaryTree */, 56 | B0047AED2765CAB000CFB38A /* Products */, 57 | ); 58 | sourceTree = ""; 59 | }; 60 | B0047AED2765CAB000CFB38A /* Products */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | B0047AEC2765CAB000CFB38A /* BinaryTree */, 64 | ); 65 | name = Products; 66 | sourceTree = ""; 67 | }; 68 | B0047AEE2765CAB000CFB38A /* BinaryTree */ = { 69 | isa = PBXGroup; 70 | children = ( 71 | B0047AEF2765CAB000CFB38A /* main.swift */, 72 | B0047AF62765CAB700CFB38A /* TreeNode.swift */, 73 | B0047AF82765CB1100CFB38A /* BinaryTree.swift */, 74 | ); 75 | path = BinaryTree; 76 | sourceTree = ""; 77 | }; 78 | B0047B0C2765D72000CFB38A /* Queue */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | B0047B0D2765D72000CFB38A /* Queue.swift */, 82 | B0047B0F2765D72000CFB38A /* Node.swift */, 83 | B0047B102765D72000CFB38A /* TwoPointerLinkedList.swift */, 84 | ); 85 | name = Queue; 86 | path = ../Queue/Queue; 87 | sourceTree = ""; 88 | }; 89 | /* End PBXGroup section */ 90 | 91 | /* Begin PBXNativeTarget section */ 92 | B0047AEB2765CAB000CFB38A /* BinaryTree */ = { 93 | isa = PBXNativeTarget; 94 | buildConfigurationList = B0047AF32765CAB000CFB38A /* Build configuration list for PBXNativeTarget "BinaryTree" */; 95 | buildPhases = ( 96 | B0047AE82765CAB000CFB38A /* Sources */, 97 | B0047AE92765CAB000CFB38A /* Frameworks */, 98 | B0047AEA2765CAB000CFB38A /* CopyFiles */, 99 | ); 100 | buildRules = ( 101 | ); 102 | dependencies = ( 103 | ); 104 | name = BinaryTree; 105 | productName = BinaryTree; 106 | productReference = B0047AEC2765CAB000CFB38A /* BinaryTree */; 107 | productType = "com.apple.product-type.tool"; 108 | }; 109 | /* End PBXNativeTarget section */ 110 | 111 | /* Begin PBXProject section */ 112 | B0047AE42765CAB000CFB38A /* Project object */ = { 113 | isa = PBXProject; 114 | attributes = { 115 | BuildIndependentTargetsInParallel = 1; 116 | LastSwiftUpdateCheck = 1310; 117 | LastUpgradeCheck = 1310; 118 | TargetAttributes = { 119 | B0047AEB2765CAB000CFB38A = { 120 | CreatedOnToolsVersion = 13.1; 121 | }; 122 | }; 123 | }; 124 | buildConfigurationList = B0047AE72765CAB000CFB38A /* Build configuration list for PBXProject "BinaryTree" */; 125 | compatibilityVersion = "Xcode 13.0"; 126 | developmentRegion = en; 127 | hasScannedForEncodings = 0; 128 | knownRegions = ( 129 | en, 130 | Base, 131 | ); 132 | mainGroup = B0047AE32765CAB000CFB38A; 133 | productRefGroup = B0047AED2765CAB000CFB38A /* Products */; 134 | projectDirPath = ""; 135 | projectRoot = ""; 136 | targets = ( 137 | B0047AEB2765CAB000CFB38A /* BinaryTree */, 138 | ); 139 | }; 140 | /* End PBXProject section */ 141 | 142 | /* Begin PBXSourcesBuildPhase section */ 143 | B0047AE82765CAB000CFB38A /* Sources */ = { 144 | isa = PBXSourcesBuildPhase; 145 | buildActionMask = 2147483647; 146 | files = ( 147 | B0047AF02765CAB000CFB38A /* main.swift in Sources */, 148 | B0047B122765D74300CFB38A /* Node.swift in Sources */, 149 | B0047AF72765CAB700CFB38A /* TreeNode.swift in Sources */, 150 | B0047B132765D74500CFB38A /* TwoPointerLinkedList.swift in Sources */, 151 | B0047AF92765CB1100CFB38A /* BinaryTree.swift in Sources */, 152 | B0047B112765D74100CFB38A /* Queue.swift in Sources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXSourcesBuildPhase section */ 157 | 158 | /* Begin XCBuildConfiguration section */ 159 | B0047AF12765CAB000CFB38A /* Debug */ = { 160 | isa = XCBuildConfiguration; 161 | buildSettings = { 162 | ALWAYS_SEARCH_USER_PATHS = NO; 163 | CLANG_ANALYZER_NONNULL = YES; 164 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 165 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 166 | CLANG_CXX_LIBRARY = "libc++"; 167 | CLANG_ENABLE_MODULES = YES; 168 | CLANG_ENABLE_OBJC_ARC = YES; 169 | CLANG_ENABLE_OBJC_WEAK = YES; 170 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 171 | CLANG_WARN_BOOL_CONVERSION = YES; 172 | CLANG_WARN_COMMA = YES; 173 | CLANG_WARN_CONSTANT_CONVERSION = YES; 174 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 175 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 176 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 177 | CLANG_WARN_EMPTY_BODY = YES; 178 | CLANG_WARN_ENUM_CONVERSION = YES; 179 | CLANG_WARN_INFINITE_RECURSION = YES; 180 | CLANG_WARN_INT_CONVERSION = YES; 181 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 182 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 183 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 184 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 185 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 186 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 187 | CLANG_WARN_STRICT_PROTOTYPES = YES; 188 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 189 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | COPY_PHASE_STRIP = NO; 193 | DEBUG_INFORMATION_FORMAT = dwarf; 194 | ENABLE_STRICT_OBJC_MSGSEND = YES; 195 | ENABLE_TESTABILITY = YES; 196 | GCC_C_LANGUAGE_STANDARD = gnu11; 197 | GCC_DYNAMIC_NO_PIC = NO; 198 | GCC_NO_COMMON_BLOCKS = YES; 199 | GCC_OPTIMIZATION_LEVEL = 0; 200 | GCC_PREPROCESSOR_DEFINITIONS = ( 201 | "DEBUG=1", 202 | "$(inherited)", 203 | ); 204 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 205 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 206 | GCC_WARN_UNDECLARED_SELECTOR = YES; 207 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 208 | GCC_WARN_UNUSED_FUNCTION = YES; 209 | GCC_WARN_UNUSED_VARIABLE = YES; 210 | MACOSX_DEPLOYMENT_TARGET = 12.0; 211 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 212 | MTL_FAST_MATH = YES; 213 | ONLY_ACTIVE_ARCH = YES; 214 | SDKROOT = macosx; 215 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 216 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 217 | }; 218 | name = Debug; 219 | }; 220 | B0047AF22765CAB000CFB38A /* Release */ = { 221 | isa = XCBuildConfiguration; 222 | buildSettings = { 223 | ALWAYS_SEARCH_USER_PATHS = NO; 224 | CLANG_ANALYZER_NONNULL = YES; 225 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 226 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 227 | CLANG_CXX_LIBRARY = "libc++"; 228 | CLANG_ENABLE_MODULES = YES; 229 | CLANG_ENABLE_OBJC_ARC = YES; 230 | CLANG_ENABLE_OBJC_WEAK = YES; 231 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 232 | CLANG_WARN_BOOL_CONVERSION = YES; 233 | CLANG_WARN_COMMA = YES; 234 | CLANG_WARN_CONSTANT_CONVERSION = YES; 235 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 236 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 237 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 238 | CLANG_WARN_EMPTY_BODY = YES; 239 | CLANG_WARN_ENUM_CONVERSION = YES; 240 | CLANG_WARN_INFINITE_RECURSION = YES; 241 | CLANG_WARN_INT_CONVERSION = YES; 242 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 243 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 244 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 245 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 246 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 247 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 248 | CLANG_WARN_STRICT_PROTOTYPES = YES; 249 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 250 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 251 | CLANG_WARN_UNREACHABLE_CODE = YES; 252 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 253 | COPY_PHASE_STRIP = NO; 254 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 255 | ENABLE_NS_ASSERTIONS = NO; 256 | ENABLE_STRICT_OBJC_MSGSEND = YES; 257 | GCC_C_LANGUAGE_STANDARD = gnu11; 258 | GCC_NO_COMMON_BLOCKS = YES; 259 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 260 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 261 | GCC_WARN_UNDECLARED_SELECTOR = YES; 262 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 263 | GCC_WARN_UNUSED_FUNCTION = YES; 264 | GCC_WARN_UNUSED_VARIABLE = YES; 265 | MACOSX_DEPLOYMENT_TARGET = 12.0; 266 | MTL_ENABLE_DEBUG_INFO = NO; 267 | MTL_FAST_MATH = YES; 268 | SDKROOT = macosx; 269 | SWIFT_COMPILATION_MODE = wholemodule; 270 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 271 | }; 272 | name = Release; 273 | }; 274 | B0047AF42765CAB000CFB38A /* Debug */ = { 275 | isa = XCBuildConfiguration; 276 | buildSettings = { 277 | CODE_SIGN_STYLE = Automatic; 278 | DEVELOPMENT_TEAM = CH6R3C4FBD; 279 | ENABLE_HARDENED_RUNTIME = YES; 280 | PRODUCT_NAME = "$(TARGET_NAME)"; 281 | SWIFT_VERSION = 5.0; 282 | }; 283 | name = Debug; 284 | }; 285 | B0047AF52765CAB000CFB38A /* Release */ = { 286 | isa = XCBuildConfiguration; 287 | buildSettings = { 288 | CODE_SIGN_STYLE = Automatic; 289 | DEVELOPMENT_TEAM = CH6R3C4FBD; 290 | ENABLE_HARDENED_RUNTIME = YES; 291 | PRODUCT_NAME = "$(TARGET_NAME)"; 292 | SWIFT_VERSION = 5.0; 293 | }; 294 | name = Release; 295 | }; 296 | /* End XCBuildConfiguration section */ 297 | 298 | /* Begin XCConfigurationList section */ 299 | B0047AE72765CAB000CFB38A /* Build configuration list for PBXProject "BinaryTree" */ = { 300 | isa = XCConfigurationList; 301 | buildConfigurations = ( 302 | B0047AF12765CAB000CFB38A /* Debug */, 303 | B0047AF22765CAB000CFB38A /* Release */, 304 | ); 305 | defaultConfigurationIsVisible = 0; 306 | defaultConfigurationName = Release; 307 | }; 308 | B0047AF32765CAB000CFB38A /* Build configuration list for PBXNativeTarget "BinaryTree" */ = { 309 | isa = XCConfigurationList; 310 | buildConfigurations = ( 311 | B0047AF42765CAB000CFB38A /* Debug */, 312 | B0047AF52765CAB000CFB38A /* Release */, 313 | ); 314 | defaultConfigurationIsVisible = 0; 315 | defaultConfigurationName = Release; 316 | }; 317 | /* End XCConfigurationList section */ 318 | }; 319 | rootObject = B0047AE42765CAB000CFB38A /* Project object */; 320 | } 321 | -------------------------------------------------------------------------------- /BinaryTree/BinaryTree/BinaryTree.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BinaryTree.swift 3 | // BinaryTree 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct BinaryTree { 11 | var root: TreeNode? 12 | 13 | mutating func add(data: T) { 14 | let treeNode = TreeNode(data: data) 15 | if self.root == nil { 16 | self.root = treeNode 17 | return 18 | } 19 | self.add(newNode: treeNode, to: root!) 20 | } 21 | 22 | func printInorder() { 23 | print("\n==== Inorder ====") 24 | self.printInorder(node: self.root) 25 | print("\n") 26 | } 27 | 28 | func printPreorder() { 29 | print("\n==== Preorder ====") 30 | self.printPreorder(node: self.root) 31 | print("\n") 32 | } 33 | 34 | func printPostorder() { 35 | print("\n==== Preorder ====") 36 | self.printPostorder(node: self.root) 37 | print("\n") 38 | } 39 | 40 | private func printPreorder(node: TreeNode?) { 41 | guard let node = node else { return } 42 | print(node.data, terminator: " ") 43 | self.printPreorder(node: node.leftChild) 44 | self.printPreorder(node: node.rightChild) 45 | } 46 | 47 | private func printInorder(node: TreeNode?) { 48 | guard let node = node else { return } 49 | self.printInorder(node: node.leftChild) 50 | print(node.data, terminator: " ") 51 | self.printInorder(node: node.rightChild) 52 | } 53 | 54 | private func printPostorder(node: TreeNode?) { 55 | guard let node = node else { return } 56 | self.printPostorder(node: node.leftChild) 57 | self.printPostorder(node: node.rightChild) 58 | print(node.data, terminator: " ") 59 | } 60 | 61 | private func add(newNode: TreeNode, to node: TreeNode) { 62 | var queue = Queue> () 63 | queue.push(root!) 64 | 65 | while !queue.isEmpty { 66 | let now = queue.pop() 67 | if now?.leftChild == nil { 68 | now?.leftChild = newNode 69 | return 70 | } 71 | if now?.rightChild == nil { 72 | now?.rightChild = newNode 73 | return 74 | } 75 | queue.push(now!.leftChild!) 76 | queue.push(now!.rightChild!) 77 | } 78 | } 79 | 80 | func printTree() { 81 | print(root!.asString) 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /BinaryTree/BinaryTree/TreeNode.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TreeNode.swift 3 | // BinaryTree 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | final class TreeNode { 11 | let data: T 12 | var leftChild: TreeNode? 13 | var rightChild: TreeNode? 14 | 15 | init(data: T) { 16 | self.data = data 17 | } 18 | 19 | var asString:String { return treeString(self){("\($0.data)",$0.leftChild,$0.rightChild)} } 20 | } 21 | 22 | // https://stackoverflow.com/questions/43898440/how-to-draw-a-binary-tree-in-console/43903427 23 | public func treeString(_ node:T, reversed:Bool=false, isTop:Bool=true, using nodeInfo:(T)->(String,T?,T?)) -> String 24 | { 25 | // node value string and sub nodes 26 | let (stringValue, leftNode, rightNode) = nodeInfo(node) 27 | 28 | let stringValueWidth = stringValue.count 29 | 30 | // recurse to sub nodes to obtain line blocks on left and right 31 | let leftTextBlock = leftNode == nil ? [] 32 | : treeString(leftNode!,reversed:reversed,isTop:false,using:nodeInfo) 33 | .components(separatedBy:"\n") 34 | 35 | let rightTextBlock = rightNode == nil ? [] 36 | : treeString(rightNode!,reversed:reversed,isTop:false,using:nodeInfo) 37 | .components(separatedBy:"\n") 38 | 39 | // count common and maximum number of sub node lines 40 | let commonLines = min(leftTextBlock.count,rightTextBlock.count) 41 | let subLevelLines = max(rightTextBlock.count,leftTextBlock.count) 42 | 43 | // extend lines on shallower side to get same number of lines on both sides 44 | let leftSubLines = leftTextBlock 45 | + Array(repeating:"", count: subLevelLines-leftTextBlock.count) 46 | let rightSubLines = rightTextBlock 47 | + Array(repeating:"", count: subLevelLines-rightTextBlock.count) 48 | 49 | // compute location of value or link bar for all left and right sub nodes 50 | // * left node's value ends at line's width 51 | // * right node's value starts after initial spaces 52 | let leftLineWidths = leftSubLines.map{$0.count} 53 | let rightLineIndents = rightSubLines.map{$0.prefix{$0==" "}.count } 54 | 55 | // top line value locations, will be used to determine position of current node & link bars 56 | let firstLeftWidth = leftLineWidths.first ?? 0 57 | let firstRightIndent = rightLineIndents.first ?? 0 58 | 59 | 60 | // width of sub node link under node value (i.e. with slashes if any) 61 | // aims to center link bars under the value if value is wide enough 62 | // 63 | // ValueLine: v vv vvvvvv vvvvv 64 | // LinkLine: / \ / \ / \ / \ 65 | // 66 | let linkSpacing = min(stringValueWidth, 2 - stringValueWidth % 2) 67 | let leftLinkBar = leftNode == nil ? 0 : 1 68 | let rightLinkBar = rightNode == nil ? 0 : 1 69 | let minLinkWidth = leftLinkBar + linkSpacing + rightLinkBar 70 | let valueOffset = (stringValueWidth - linkSpacing) / 2 71 | 72 | // find optimal position for right side top node 73 | // * must allow room for link bars above and between left and right top nodes 74 | // * must not overlap lower level nodes on any given line (allow gap of minSpacing) 75 | // * can be offset to the left if lower subNodes of right node 76 | // have no overlap with subNodes of left node 77 | let minSpacing = 2 78 | let rightNodePosition = zip(leftLineWidths,rightLineIndents[0..() 11 | 12 | for i in 0..<30 { 13 | tree.add(data: i) 14 | } 15 | tree.printTree() 16 | 17 | tree.printInorder() 18 | tree.printPreorder() 19 | tree.printPostorder() 20 | 21 | // 0 22 | // __________________/ \_________________ 23 | // 1 2 24 | // _______/ \_______ ________/ \________ 25 | // 3 4 5 6 26 | // ___/ \__ ___/ \__ ___/ \__ ___/ \__ 27 | // 7 8 9 10 11 12 13 14 28 | // / \ / \ / \ / \ / \ / \ / \ / 29 | // 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // 31 | //==== Inorder ==== 32 | //15 7 16 3 17 8 18 1 19 9 20 4 21 10 22 0 23 11 24 5 25 12 26 2 27 13 28 6 29 14 33 | // 34 | // 35 | //==== Preorder ==== 36 | //0 1 3 7 15 16 8 17 18 4 9 19 20 10 21 22 2 5 11 23 24 12 25 26 6 13 27 28 14 29 37 | // 38 | // 39 | //==== Preorder ==== 40 | //15 16 7 17 18 8 3 19 20 9 21 22 10 4 1 23 24 11 25 26 12 5 27 28 13 29 14 6 2 0 41 | // 42 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedList.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047AD52764D87600CFB38A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AD42764D87600CFB38A /* main.swift */; }; 11 | B0047ADC2764D89800CFB38A /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047ADB2764D89800CFB38A /* Node.swift */; }; 12 | B0047ADE2764D8B000CFB38A /* DoublyLinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047ADD2764D8B000CFB38A /* DoublyLinkedList.swift */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | B0047ACF2764D87600CFB38A /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | ); 23 | runOnlyForDeploymentPostprocessing = 1; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | B0047AD12764D87600CFB38A /* DoublyLinkedList */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = DoublyLinkedList; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | B0047AD42764D87600CFB38A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 30 | B0047ADB2764D89800CFB38A /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; 31 | B0047ADD2764D8B000CFB38A /* DoublyLinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoublyLinkedList.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | B0047ACE2764D87600CFB38A /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | B0047AC82764D87600CFB38A = { 46 | isa = PBXGroup; 47 | children = ( 48 | B0047AD32764D87600CFB38A /* DoublyLinkedList */, 49 | B0047AD22764D87600CFB38A /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | B0047AD22764D87600CFB38A /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | B0047AD12764D87600CFB38A /* DoublyLinkedList */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | B0047AD32764D87600CFB38A /* DoublyLinkedList */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | B0047AD42764D87600CFB38A /* main.swift */, 65 | B0047ADB2764D89800CFB38A /* Node.swift */, 66 | B0047ADD2764D8B000CFB38A /* DoublyLinkedList.swift */, 67 | ); 68 | path = DoublyLinkedList; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | B0047AD02764D87600CFB38A /* DoublyLinkedList */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = B0047AD82764D87600CFB38A /* Build configuration list for PBXNativeTarget "DoublyLinkedList" */; 77 | buildPhases = ( 78 | B0047ACD2764D87600CFB38A /* Sources */, 79 | B0047ACE2764D87600CFB38A /* Frameworks */, 80 | B0047ACF2764D87600CFB38A /* CopyFiles */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = DoublyLinkedList; 87 | productName = DoublyLinkedList; 88 | productReference = B0047AD12764D87600CFB38A /* DoublyLinkedList */; 89 | productType = "com.apple.product-type.tool"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | B0047AC92764D87600CFB38A /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | BuildIndependentTargetsInParallel = 1; 98 | LastSwiftUpdateCheck = 1310; 99 | LastUpgradeCheck = 1310; 100 | TargetAttributes = { 101 | B0047AD02764D87600CFB38A = { 102 | CreatedOnToolsVersion = 13.1; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = B0047ACC2764D87600CFB38A /* Build configuration list for PBXProject "DoublyLinkedList" */; 107 | compatibilityVersion = "Xcode 13.0"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = B0047AC82764D87600CFB38A; 115 | productRefGroup = B0047AD22764D87600CFB38A /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | B0047AD02764D87600CFB38A /* DoublyLinkedList */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXSourcesBuildPhase section */ 125 | B0047ACD2764D87600CFB38A /* Sources */ = { 126 | isa = PBXSourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | B0047AD52764D87600CFB38A /* main.swift in Sources */, 130 | B0047ADE2764D8B000CFB38A /* DoublyLinkedList.swift in Sources */, 131 | B0047ADC2764D89800CFB38A /* Node.swift in Sources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXSourcesBuildPhase section */ 136 | 137 | /* Begin XCBuildConfiguration section */ 138 | B0047AD62764D87600CFB38A /* Debug */ = { 139 | isa = XCBuildConfiguration; 140 | buildSettings = { 141 | ALWAYS_SEARCH_USER_PATHS = NO; 142 | CLANG_ANALYZER_NONNULL = YES; 143 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 144 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 145 | CLANG_CXX_LIBRARY = "libc++"; 146 | CLANG_ENABLE_MODULES = YES; 147 | CLANG_ENABLE_OBJC_ARC = YES; 148 | CLANG_ENABLE_OBJC_WEAK = YES; 149 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 150 | CLANG_WARN_BOOL_CONVERSION = YES; 151 | CLANG_WARN_COMMA = YES; 152 | CLANG_WARN_CONSTANT_CONVERSION = YES; 153 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 154 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 155 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 156 | CLANG_WARN_EMPTY_BODY = YES; 157 | CLANG_WARN_ENUM_CONVERSION = YES; 158 | CLANG_WARN_INFINITE_RECURSION = YES; 159 | CLANG_WARN_INT_CONVERSION = YES; 160 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 161 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 162 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 163 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 164 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 165 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 166 | CLANG_WARN_STRICT_PROTOTYPES = YES; 167 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 168 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 169 | CLANG_WARN_UNREACHABLE_CODE = YES; 170 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 171 | COPY_PHASE_STRIP = NO; 172 | DEBUG_INFORMATION_FORMAT = dwarf; 173 | ENABLE_STRICT_OBJC_MSGSEND = YES; 174 | ENABLE_TESTABILITY = YES; 175 | GCC_C_LANGUAGE_STANDARD = gnu11; 176 | GCC_DYNAMIC_NO_PIC = NO; 177 | GCC_NO_COMMON_BLOCKS = YES; 178 | GCC_OPTIMIZATION_LEVEL = 0; 179 | GCC_PREPROCESSOR_DEFINITIONS = ( 180 | "DEBUG=1", 181 | "$(inherited)", 182 | ); 183 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 184 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 185 | GCC_WARN_UNDECLARED_SELECTOR = YES; 186 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 187 | GCC_WARN_UNUSED_FUNCTION = YES; 188 | GCC_WARN_UNUSED_VARIABLE = YES; 189 | MACOSX_DEPLOYMENT_TARGET = 12.0; 190 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 191 | MTL_FAST_MATH = YES; 192 | ONLY_ACTIVE_ARCH = YES; 193 | SDKROOT = macosx; 194 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 195 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 196 | }; 197 | name = Debug; 198 | }; 199 | B0047AD72764D87600CFB38A /* Release */ = { 200 | isa = XCBuildConfiguration; 201 | buildSettings = { 202 | ALWAYS_SEARCH_USER_PATHS = NO; 203 | CLANG_ANALYZER_NONNULL = YES; 204 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 205 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 206 | CLANG_CXX_LIBRARY = "libc++"; 207 | CLANG_ENABLE_MODULES = YES; 208 | CLANG_ENABLE_OBJC_ARC = YES; 209 | CLANG_ENABLE_OBJC_WEAK = YES; 210 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 211 | CLANG_WARN_BOOL_CONVERSION = YES; 212 | CLANG_WARN_COMMA = YES; 213 | CLANG_WARN_CONSTANT_CONVERSION = YES; 214 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 215 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 216 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 217 | CLANG_WARN_EMPTY_BODY = YES; 218 | CLANG_WARN_ENUM_CONVERSION = YES; 219 | CLANG_WARN_INFINITE_RECURSION = YES; 220 | CLANG_WARN_INT_CONVERSION = YES; 221 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 222 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 223 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 226 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 227 | CLANG_WARN_STRICT_PROTOTYPES = YES; 228 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 229 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 230 | CLANG_WARN_UNREACHABLE_CODE = YES; 231 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 232 | COPY_PHASE_STRIP = NO; 233 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 234 | ENABLE_NS_ASSERTIONS = NO; 235 | ENABLE_STRICT_OBJC_MSGSEND = YES; 236 | GCC_C_LANGUAGE_STANDARD = gnu11; 237 | GCC_NO_COMMON_BLOCKS = YES; 238 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 239 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 240 | GCC_WARN_UNDECLARED_SELECTOR = YES; 241 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 242 | GCC_WARN_UNUSED_FUNCTION = YES; 243 | GCC_WARN_UNUSED_VARIABLE = YES; 244 | MACOSX_DEPLOYMENT_TARGET = 12.0; 245 | MTL_ENABLE_DEBUG_INFO = NO; 246 | MTL_FAST_MATH = YES; 247 | SDKROOT = macosx; 248 | SWIFT_COMPILATION_MODE = wholemodule; 249 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 250 | }; 251 | name = Release; 252 | }; 253 | B0047AD92764D87600CFB38A /* Debug */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | CODE_SIGN_STYLE = Automatic; 257 | DEVELOPMENT_TEAM = CH6R3C4FBD; 258 | ENABLE_HARDENED_RUNTIME = YES; 259 | PRODUCT_NAME = "$(TARGET_NAME)"; 260 | SWIFT_VERSION = 5.0; 261 | }; 262 | name = Debug; 263 | }; 264 | B0047ADA2764D87600CFB38A /* Release */ = { 265 | isa = XCBuildConfiguration; 266 | buildSettings = { 267 | CODE_SIGN_STYLE = Automatic; 268 | DEVELOPMENT_TEAM = CH6R3C4FBD; 269 | ENABLE_HARDENED_RUNTIME = YES; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | SWIFT_VERSION = 5.0; 272 | }; 273 | name = Release; 274 | }; 275 | /* End XCBuildConfiguration section */ 276 | 277 | /* Begin XCConfigurationList section */ 278 | B0047ACC2764D87600CFB38A /* Build configuration list for PBXProject "DoublyLinkedList" */ = { 279 | isa = XCConfigurationList; 280 | buildConfigurations = ( 281 | B0047AD62764D87600CFB38A /* Debug */, 282 | B0047AD72764D87600CFB38A /* Release */, 283 | ); 284 | defaultConfigurationIsVisible = 0; 285 | defaultConfigurationName = Release; 286 | }; 287 | B0047AD82764D87600CFB38A /* Build configuration list for PBXNativeTarget "DoublyLinkedList" */ = { 288 | isa = XCConfigurationList; 289 | buildConfigurations = ( 290 | B0047AD92764D87600CFB38A /* Debug */, 291 | B0047ADA2764D87600CFB38A /* Release */, 292 | ); 293 | defaultConfigurationIsVisible = 0; 294 | defaultConfigurationName = Release; 295 | }; 296 | /* End XCConfigurationList section */ 297 | }; 298 | rootObject = B0047AC92764D87600CFB38A /* Project object */; 299 | } 300 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedList/DoublyLinkedList.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DoublyLinkedList.swift 3 | // DoublyLinkedList 4 | // 5 | // Created by 전여훈 on 2021/12/11. 6 | // 7 | 8 | import Foundation 9 | 10 | struct DoublyLinkedList { 11 | var head: Node? 12 | var tail: Node? 13 | 14 | var front: Node? { 15 | return head 16 | } 17 | 18 | var back: Node? { 19 | return tail 20 | } 21 | 22 | mutating func add(node: Node) { 23 | if self.head == nil { 24 | self.head = node 25 | self.tail = node 26 | return 27 | } 28 | 29 | self.tail?.next = node 30 | node.prev = self.tail 31 | self.tail = node 32 | } 33 | 34 | mutating func searchNode(with data: T) -> Node? { 35 | var now = self.head 36 | while now?.data != data && now !== tail { 37 | now = now?.next 38 | } 39 | return now 40 | } 41 | 42 | mutating func deleteNode(with id: Int) -> Node? { 43 | var now = self.head 44 | 45 | if id == self.tail?.id { 46 | now = self.tail 47 | } else { 48 | while now?.id != id && now != nil { 49 | now = now?.next 50 | } 51 | } 52 | 53 | let deleted = now 54 | deleted?.next?.prev = deleted?.prev 55 | deleted?.prev?.next = deleted?.next 56 | 57 | if deleted === head { 58 | self.head = deleted?.next 59 | } 60 | 61 | if deleted === tail { 62 | self.tail = deleted?.prev 63 | } 64 | 65 | return deleted 66 | } 67 | 68 | mutating func insert(node: Node, after id: Int) { 69 | guard head != nil else { return } 70 | var now = self.head 71 | if id == self.tail!.id { 72 | self.add(node: node) 73 | } 74 | 75 | while now?.id != id && now != nil { 76 | now = now?.next 77 | } 78 | if now === self.tail { 79 | self.tail = node 80 | } 81 | node.next = now?.next 82 | now?.next?.prev = node 83 | now?.next = node 84 | node.prev = now 85 | } 86 | 87 | mutating func insert(node: Node, before id: Int) { 88 | guard head != nil else { return } 89 | var now = self.head 90 | 91 | if id == self.tail?.id { 92 | now = self.tail 93 | } else { 94 | while now?.id != id && now != nil { 95 | now = now?.next 96 | } 97 | } 98 | 99 | if now === self.head { 100 | self.head = node 101 | } 102 | 103 | now?.prev?.next = node 104 | node.prev = now?.prev 105 | now?.prev = node 106 | node.next = now 107 | } 108 | 109 | mutating func reverse() { 110 | var now = head 111 | while now != nil { 112 | let nowNext = now?.next 113 | now?.next = now?.prev 114 | now?.prev = nowNext 115 | now = now?.prev 116 | } 117 | let nowHead = self.head 118 | self.head = self.tail 119 | self.tail = nowHead 120 | } 121 | 122 | func showAll() { 123 | var now = head 124 | print("===== Linked List ======") 125 | while now != nil { 126 | now?.next == nil 127 | ? print("id: \(now!.id) | data: \(now!.data)") 128 | : print("id: \(now!.id) | data: \(now!.data) -> ") 129 | now = now?.next 130 | } 131 | print("========================") 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedList/Node.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Node.swift 3 | // DoublyLinkedList 4 | // 5 | // Created by 전여훈 on 2021/12/11. 6 | // 7 | 8 | import Foundation 9 | 10 | class Node { 11 | let id: Int 12 | let data: T 13 | var next: Node? = nil 14 | var prev: Node? = nil 15 | 16 | init(id: Int, data: T) { 17 | self.id = id 18 | self.data = data 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DoublyLinkedList/DoublyLinkedList/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // DoublyLinkedList 4 | // 5 | // Created by 전여훈 on 2021/12/11. 6 | // 7 | 8 | import Foundation 9 | 10 | var list = DoublyLinkedList() 11 | 12 | for id in 0..<30 { 13 | list.add(node: Node(id: id, data: (0...100).randomElement()!)) 14 | } 15 | 16 | list.showAll() 17 | 18 | print("insert node with id -1 before node with id 0") 19 | list.insert(node: Node(id: -1, data: 0), before: 0) 20 | list.showAll() 21 | 22 | print("insert node with id 100 before node with id 29") 23 | list.insert(node: Node(id: 100, data: 0), before: 29) 24 | list.showAll() 25 | 26 | print("\ncommand: reverse") 27 | list.reverse() 28 | list.showAll() 29 | 30 | let deletedzero = list.deleteNode(with: 0) 31 | print("deleted node: id: \(deletedzero?.id) data: \(deletedzero?.data)") 32 | list.showAll() 33 | 34 | let deletedtwenty = list.deleteNode(with: 20) 35 | print("deleted node: id: \(deletedtwenty?.id) data: \(deletedtwenty?.data)") 36 | list.showAll() 37 | 38 | print("insert node with id 30 before node with id 21") 39 | list.insert(node: Node(id: 30, data: 0), before: 21) 40 | list.showAll() 41 | 42 | print("insert node with id 31 after node with id 29") 43 | list.insert(node: Node(id: 31, data: 0), after: 29) 44 | list.showAll() 45 | 46 | print("reverse") 47 | list.reverse() 48 | list.showAll() 49 | 50 | print("insert node with id 0 before node with id 31") 51 | list.insert(node: Node(id: 0, data: 1), before: 31) 52 | list.showAll() 53 | 54 | print("node at front") 55 | print("id: \(list.front?.id) data: \(list.front?.data)") 56 | 57 | print("node at back") 58 | print("id: \(list.back?.id) data: \(list.back?.data)") 59 | 60 | list.deleteNode(with: 29) 61 | list.showAll() 62 | 63 | //===== Linked List ====== 64 | //id: 0 | data: 18 -> 65 | //id: 1 | data: 6 -> 66 | //id: 2 | data: 13 -> 67 | //id: 3 | data: 46 -> 68 | //id: 4 | data: 37 -> 69 | //id: 5 | data: 79 -> 70 | //id: 6 | data: 70 -> 71 | //id: 7 | data: 42 -> 72 | //id: 8 | data: 23 -> 73 | //id: 9 | data: 59 -> 74 | //id: 10 | data: 60 -> 75 | //id: 11 | data: 44 -> 76 | //id: 12 | data: 12 -> 77 | //id: 13 | data: 18 -> 78 | //id: 14 | data: 4 -> 79 | //id: 15 | data: 54 -> 80 | //id: 16 | data: 16 -> 81 | //id: 17 | data: 5 -> 82 | //id: 18 | data: 69 -> 83 | //id: 19 | data: 69 -> 84 | //id: 20 | data: 35 -> 85 | //id: 21 | data: 24 -> 86 | //id: 22 | data: 32 -> 87 | //id: 23 | data: 14 -> 88 | //id: 24 | data: 99 -> 89 | //id: 25 | data: 31 -> 90 | //id: 26 | data: 27 -> 91 | //id: 27 | data: 16 -> 92 | //id: 28 | data: 68 -> 93 | //id: 29 | data: 50 94 | //======================== 95 | // 96 | //command: reverse 97 | //===== Linked List ====== 98 | //id: 29 | data: 50 -> 99 | //id: 28 | data: 68 -> 100 | //id: 27 | data: 16 -> 101 | //id: 26 | data: 27 -> 102 | //id: 25 | data: 31 -> 103 | //id: 24 | data: 99 -> 104 | //id: 23 | data: 14 -> 105 | //id: 22 | data: 32 -> 106 | //id: 21 | data: 24 -> 107 | //id: 20 | data: 35 -> 108 | //id: 19 | data: 69 -> 109 | //id: 18 | data: 69 -> 110 | //id: 17 | data: 5 -> 111 | //id: 16 | data: 16 -> 112 | //id: 15 | data: 54 -> 113 | //id: 14 | data: 4 -> 114 | //id: 13 | data: 18 -> 115 | //id: 12 | data: 12 -> 116 | //id: 11 | data: 44 -> 117 | //id: 10 | data: 60 -> 118 | //id: 9 | data: 59 -> 119 | //id: 8 | data: 23 -> 120 | //id: 7 | data: 42 -> 121 | //id: 6 | data: 70 -> 122 | //id: 5 | data: 79 -> 123 | //id: 4 | data: 37 -> 124 | //id: 3 | data: 46 -> 125 | //id: 2 | data: 13 -> 126 | //id: 1 | data: 6 -> 127 | //id: 0 | data: 18 128 | //======================== 129 | //deleted node: id: Optional(0) data: Optional(18) 130 | //===== Linked List ====== 131 | //id: 29 | data: 50 -> 132 | //id: 28 | data: 68 -> 133 | //id: 27 | data: 16 -> 134 | //id: 26 | data: 27 -> 135 | //id: 25 | data: 31 -> 136 | //id: 24 | data: 99 -> 137 | //id: 23 | data: 14 -> 138 | //id: 22 | data: 32 -> 139 | //id: 21 | data: 24 -> 140 | //id: 20 | data: 35 -> 141 | //id: 19 | data: 69 -> 142 | //id: 18 | data: 69 -> 143 | //id: 17 | data: 5 -> 144 | //id: 16 | data: 16 -> 145 | //id: 15 | data: 54 -> 146 | //id: 14 | data: 4 -> 147 | //id: 13 | data: 18 -> 148 | //id: 12 | data: 12 -> 149 | //id: 11 | data: 44 -> 150 | //id: 10 | data: 60 -> 151 | //id: 9 | data: 59 -> 152 | //id: 8 | data: 23 -> 153 | //id: 7 | data: 42 -> 154 | //id: 6 | data: 70 -> 155 | //id: 5 | data: 79 -> 156 | //id: 4 | data: 37 -> 157 | //id: 3 | data: 46 -> 158 | //id: 2 | data: 13 -> 159 | //id: 1 | data: 6 160 | //======================== 161 | //deleted node: id: Optional(20) data: Optional(35) 162 | //===== Linked List ====== 163 | //id: 29 | data: 50 -> 164 | //id: 28 | data: 68 -> 165 | //id: 27 | data: 16 -> 166 | //id: 26 | data: 27 -> 167 | //id: 25 | data: 31 -> 168 | //id: 24 | data: 99 -> 169 | //id: 23 | data: 14 -> 170 | //id: 22 | data: 32 -> 171 | //id: 21 | data: 24 -> 172 | //id: 19 | data: 69 -> 173 | //id: 18 | data: 69 -> 174 | //id: 17 | data: 5 -> 175 | //id: 16 | data: 16 -> 176 | //id: 15 | data: 54 -> 177 | //id: 14 | data: 4 -> 178 | //id: 13 | data: 18 -> 179 | //id: 12 | data: 12 -> 180 | //id: 11 | data: 44 -> 181 | //id: 10 | data: 60 -> 182 | //id: 9 | data: 59 -> 183 | //id: 8 | data: 23 -> 184 | //id: 7 | data: 42 -> 185 | //id: 6 | data: 70 -> 186 | //id: 5 | data: 79 -> 187 | //id: 4 | data: 37 -> 188 | //id: 3 | data: 46 -> 189 | //id: 2 | data: 13 -> 190 | //id: 1 | data: 6 191 | //======================== 192 | //insert node with id 30 before node with id 21 193 | //===== Linked List ====== 194 | //id: 29 | data: 50 -> 195 | //id: 28 | data: 68 -> 196 | //id: 27 | data: 16 -> 197 | //id: 26 | data: 27 -> 198 | //id: 25 | data: 31 -> 199 | //id: 24 | data: 99 -> 200 | //id: 23 | data: 14 -> 201 | //id: 22 | data: 32 -> 202 | //id: 30 | data: 0 -> 203 | //id: 21 | data: 24 -> 204 | //id: 19 | data: 69 -> 205 | //id: 18 | data: 69 -> 206 | //id: 17 | data: 5 -> 207 | //id: 16 | data: 16 -> 208 | //id: 15 | data: 54 -> 209 | //id: 14 | data: 4 -> 210 | //id: 13 | data: 18 -> 211 | //id: 12 | data: 12 -> 212 | //id: 11 | data: 44 -> 213 | //id: 10 | data: 60 -> 214 | //id: 9 | data: 59 -> 215 | //id: 8 | data: 23 -> 216 | //id: 7 | data: 42 -> 217 | //id: 6 | data: 70 -> 218 | //id: 5 | data: 79 -> 219 | //id: 4 | data: 37 -> 220 | //id: 3 | data: 46 -> 221 | //id: 2 | data: 13 -> 222 | //id: 1 | data: 6 223 | //======================== 224 | //insert node with id 31 after node with id 29 225 | //===== Linked List ====== 226 | //id: 29 | data: 50 -> 227 | //id: 31 | data: 0 -> 228 | //id: 28 | data: 68 -> 229 | //id: 27 | data: 16 -> 230 | //id: 26 | data: 27 -> 231 | //id: 25 | data: 31 -> 232 | //id: 24 | data: 99 -> 233 | //id: 23 | data: 14 -> 234 | //id: 22 | data: 32 -> 235 | //id: 30 | data: 0 -> 236 | //id: 21 | data: 24 -> 237 | //id: 19 | data: 69 -> 238 | //id: 18 | data: 69 -> 239 | //id: 17 | data: 5 -> 240 | //id: 16 | data: 16 -> 241 | //id: 15 | data: 54 -> 242 | //id: 14 | data: 4 -> 243 | //id: 13 | data: 18 -> 244 | //id: 12 | data: 12 -> 245 | //id: 11 | data: 44 -> 246 | //id: 10 | data: 60 -> 247 | //id: 9 | data: 59 -> 248 | //id: 8 | data: 23 -> 249 | //id: 7 | data: 42 -> 250 | //id: 6 | data: 70 -> 251 | //id: 5 | data: 79 -> 252 | //id: 4 | data: 37 -> 253 | //id: 3 | data: 46 -> 254 | //id: 2 | data: 13 -> 255 | //id: 1 | data: 6 256 | //======================== 257 | //reverse 258 | //===== Linked List ====== 259 | //id: 1 | data: 6 -> 260 | //id: 2 | data: 13 -> 261 | //id: 3 | data: 46 -> 262 | //id: 4 | data: 37 -> 263 | //id: 5 | data: 79 -> 264 | //id: 6 | data: 70 -> 265 | //id: 7 | data: 42 -> 266 | //id: 8 | data: 23 -> 267 | //id: 9 | data: 59 -> 268 | //id: 10 | data: 60 -> 269 | //id: 11 | data: 44 -> 270 | //id: 12 | data: 12 -> 271 | //id: 13 | data: 18 -> 272 | //id: 14 | data: 4 -> 273 | //id: 15 | data: 54 -> 274 | //id: 16 | data: 16 -> 275 | //id: 17 | data: 5 -> 276 | //id: 18 | data: 69 -> 277 | //id: 19 | data: 69 -> 278 | //id: 21 | data: 24 -> 279 | //id: 30 | data: 0 -> 280 | //id: 22 | data: 32 -> 281 | //id: 23 | data: 14 -> 282 | //id: 24 | data: 99 -> 283 | //id: 25 | data: 31 -> 284 | //id: 26 | data: 27 -> 285 | //id: 27 | data: 16 -> 286 | //id: 28 | data: 68 -> 287 | //id: 31 | data: 0 -> 288 | //id: 29 | data: 50 289 | //======================== 290 | //insert node with id 0 before node with id 31 291 | //===== Linked List ====== 292 | //id: 1 | data: 6 -> 293 | //id: 2 | data: 13 -> 294 | //id: 3 | data: 46 -> 295 | //id: 4 | data: 37 -> 296 | //id: 5 | data: 79 -> 297 | //id: 6 | data: 70 -> 298 | //id: 7 | data: 42 -> 299 | //id: 8 | data: 23 -> 300 | //id: 9 | data: 59 -> 301 | //id: 10 | data: 60 -> 302 | //id: 11 | data: 44 -> 303 | //id: 12 | data: 12 -> 304 | //id: 13 | data: 18 -> 305 | //id: 14 | data: 4 -> 306 | //id: 15 | data: 54 -> 307 | //id: 16 | data: 16 -> 308 | //id: 17 | data: 5 -> 309 | //id: 18 | data: 69 -> 310 | //id: 19 | data: 69 -> 311 | //id: 21 | data: 24 -> 312 | //id: 30 | data: 0 -> 313 | //id: 22 | data: 32 -> 314 | //id: 23 | data: 14 -> 315 | //id: 24 | data: 99 -> 316 | //id: 25 | data: 31 -> 317 | //id: 26 | data: 27 -> 318 | //id: 27 | data: 16 -> 319 | //id: 28 | data: 68 -> 320 | //id: 0 | data: 1 -> 321 | //id: 31 | data: 0 -> 322 | //id: 29 | data: 50 323 | //======================== 324 | //node at front 325 | //id: Optional(1) data: Optional(6) 326 | //node at back 327 | //id: Optional(29) data: Optional(50) 328 | //Program ended with exit code: 0 329 | -------------------------------------------------------------------------------- /Heap/Heap.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047AE02765C18200CFB38A /* Heap.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047ADF2765C18200CFB38A /* Heap.swift */; }; 11 | B09324AB26AD41590051E175 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09324AA26AD41590051E175 /* main.swift */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXCopyFilesBuildPhase section */ 15 | B09324A526AD41590051E175 /* CopyFiles */ = { 16 | isa = PBXCopyFilesBuildPhase; 17 | buildActionMask = 2147483647; 18 | dstPath = /usr/share/man/man1/; 19 | dstSubfolderSpec = 0; 20 | files = ( 21 | ); 22 | runOnlyForDeploymentPostprocessing = 1; 23 | }; 24 | /* End PBXCopyFilesBuildPhase section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | B0047ADF2765C18200CFB38A /* Heap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Heap.swift; sourceTree = ""; }; 28 | B09324A726AD41590051E175 /* Heap */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Heap; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | B09324AA26AD41590051E175 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 30 | /* End PBXFileReference section */ 31 | 32 | /* Begin PBXFrameworksBuildPhase section */ 33 | B09324A426AD41590051E175 /* Frameworks */ = { 34 | isa = PBXFrameworksBuildPhase; 35 | buildActionMask = 2147483647; 36 | files = ( 37 | ); 38 | runOnlyForDeploymentPostprocessing = 0; 39 | }; 40 | /* End PBXFrameworksBuildPhase section */ 41 | 42 | /* Begin PBXGroup section */ 43 | B093249E26AD41590051E175 = { 44 | isa = PBXGroup; 45 | children = ( 46 | B09324A926AD41590051E175 /* Heap */, 47 | B09324A826AD41590051E175 /* Products */, 48 | ); 49 | sourceTree = ""; 50 | }; 51 | B09324A826AD41590051E175 /* Products */ = { 52 | isa = PBXGroup; 53 | children = ( 54 | B09324A726AD41590051E175 /* Heap */, 55 | ); 56 | name = Products; 57 | sourceTree = ""; 58 | }; 59 | B09324A926AD41590051E175 /* Heap */ = { 60 | isa = PBXGroup; 61 | children = ( 62 | B09324AA26AD41590051E175 /* main.swift */, 63 | B0047ADF2765C18200CFB38A /* Heap.swift */, 64 | ); 65 | path = Heap; 66 | sourceTree = ""; 67 | }; 68 | /* End PBXGroup section */ 69 | 70 | /* Begin PBXNativeTarget section */ 71 | B09324A626AD41590051E175 /* Heap */ = { 72 | isa = PBXNativeTarget; 73 | buildConfigurationList = B09324AE26AD41590051E175 /* Build configuration list for PBXNativeTarget "Heap" */; 74 | buildPhases = ( 75 | B09324A326AD41590051E175 /* Sources */, 76 | B09324A426AD41590051E175 /* Frameworks */, 77 | B09324A526AD41590051E175 /* CopyFiles */, 78 | ); 79 | buildRules = ( 80 | ); 81 | dependencies = ( 82 | ); 83 | name = Heap; 84 | productName = Heap; 85 | productReference = B09324A726AD41590051E175 /* Heap */; 86 | productType = "com.apple.product-type.tool"; 87 | }; 88 | /* End PBXNativeTarget section */ 89 | 90 | /* Begin PBXProject section */ 91 | B093249F26AD41590051E175 /* Project object */ = { 92 | isa = PBXProject; 93 | attributes = { 94 | LastSwiftUpdateCheck = 1250; 95 | LastUpgradeCheck = 1250; 96 | TargetAttributes = { 97 | B09324A626AD41590051E175 = { 98 | CreatedOnToolsVersion = 12.5.1; 99 | }; 100 | }; 101 | }; 102 | buildConfigurationList = B09324A226AD41590051E175 /* Build configuration list for PBXProject "Heap" */; 103 | compatibilityVersion = "Xcode 9.3"; 104 | developmentRegion = en; 105 | hasScannedForEncodings = 0; 106 | knownRegions = ( 107 | en, 108 | Base, 109 | ); 110 | mainGroup = B093249E26AD41590051E175; 111 | productRefGroup = B09324A826AD41590051E175 /* Products */; 112 | projectDirPath = ""; 113 | projectRoot = ""; 114 | targets = ( 115 | B09324A626AD41590051E175 /* Heap */, 116 | ); 117 | }; 118 | /* End PBXProject section */ 119 | 120 | /* Begin PBXSourcesBuildPhase section */ 121 | B09324A326AD41590051E175 /* Sources */ = { 122 | isa = PBXSourcesBuildPhase; 123 | buildActionMask = 2147483647; 124 | files = ( 125 | B09324AB26AD41590051E175 /* main.swift in Sources */, 126 | B0047AE02765C18200CFB38A /* Heap.swift in Sources */, 127 | ); 128 | runOnlyForDeploymentPostprocessing = 0; 129 | }; 130 | /* End PBXSourcesBuildPhase section */ 131 | 132 | /* Begin XCBuildConfiguration section */ 133 | B09324AC26AD41590051E175 /* Debug */ = { 134 | isa = XCBuildConfiguration; 135 | buildSettings = { 136 | ALWAYS_SEARCH_USER_PATHS = NO; 137 | CLANG_ANALYZER_NONNULL = YES; 138 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 139 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 140 | CLANG_CXX_LIBRARY = "libc++"; 141 | CLANG_ENABLE_MODULES = YES; 142 | CLANG_ENABLE_OBJC_ARC = YES; 143 | CLANG_ENABLE_OBJC_WEAK = YES; 144 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 145 | CLANG_WARN_BOOL_CONVERSION = YES; 146 | CLANG_WARN_COMMA = YES; 147 | CLANG_WARN_CONSTANT_CONVERSION = YES; 148 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 149 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 150 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 151 | CLANG_WARN_EMPTY_BODY = YES; 152 | CLANG_WARN_ENUM_CONVERSION = YES; 153 | CLANG_WARN_INFINITE_RECURSION = YES; 154 | CLANG_WARN_INT_CONVERSION = YES; 155 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 156 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 157 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 158 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 159 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 160 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 161 | CLANG_WARN_STRICT_PROTOTYPES = YES; 162 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 163 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 164 | CLANG_WARN_UNREACHABLE_CODE = YES; 165 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 166 | COPY_PHASE_STRIP = NO; 167 | DEBUG_INFORMATION_FORMAT = dwarf; 168 | ENABLE_STRICT_OBJC_MSGSEND = YES; 169 | ENABLE_TESTABILITY = YES; 170 | GCC_C_LANGUAGE_STANDARD = gnu11; 171 | GCC_DYNAMIC_NO_PIC = NO; 172 | GCC_NO_COMMON_BLOCKS = YES; 173 | GCC_OPTIMIZATION_LEVEL = 0; 174 | GCC_PREPROCESSOR_DEFINITIONS = ( 175 | "DEBUG=1", 176 | "$(inherited)", 177 | ); 178 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 179 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 180 | GCC_WARN_UNDECLARED_SELECTOR = YES; 181 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 182 | GCC_WARN_UNUSED_FUNCTION = YES; 183 | GCC_WARN_UNUSED_VARIABLE = YES; 184 | MACOSX_DEPLOYMENT_TARGET = 11.3; 185 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 186 | MTL_FAST_MATH = YES; 187 | ONLY_ACTIVE_ARCH = YES; 188 | SDKROOT = macosx; 189 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 190 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 191 | }; 192 | name = Debug; 193 | }; 194 | B09324AD26AD41590051E175 /* Release */ = { 195 | isa = XCBuildConfiguration; 196 | buildSettings = { 197 | ALWAYS_SEARCH_USER_PATHS = NO; 198 | CLANG_ANALYZER_NONNULL = YES; 199 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 200 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 201 | CLANG_CXX_LIBRARY = "libc++"; 202 | CLANG_ENABLE_MODULES = YES; 203 | CLANG_ENABLE_OBJC_ARC = YES; 204 | CLANG_ENABLE_OBJC_WEAK = YES; 205 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 206 | CLANG_WARN_BOOL_CONVERSION = YES; 207 | CLANG_WARN_COMMA = YES; 208 | CLANG_WARN_CONSTANT_CONVERSION = YES; 209 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 210 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 211 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 212 | CLANG_WARN_EMPTY_BODY = YES; 213 | CLANG_WARN_ENUM_CONVERSION = YES; 214 | CLANG_WARN_INFINITE_RECURSION = YES; 215 | CLANG_WARN_INT_CONVERSION = YES; 216 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 217 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 218 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 219 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 220 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 221 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 222 | CLANG_WARN_STRICT_PROTOTYPES = YES; 223 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 224 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 225 | CLANG_WARN_UNREACHABLE_CODE = YES; 226 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 227 | COPY_PHASE_STRIP = NO; 228 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 229 | ENABLE_NS_ASSERTIONS = NO; 230 | ENABLE_STRICT_OBJC_MSGSEND = YES; 231 | GCC_C_LANGUAGE_STANDARD = gnu11; 232 | GCC_NO_COMMON_BLOCKS = YES; 233 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 234 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 235 | GCC_WARN_UNDECLARED_SELECTOR = YES; 236 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 237 | GCC_WARN_UNUSED_FUNCTION = YES; 238 | GCC_WARN_UNUSED_VARIABLE = YES; 239 | MACOSX_DEPLOYMENT_TARGET = 11.3; 240 | MTL_ENABLE_DEBUG_INFO = NO; 241 | MTL_FAST_MATH = YES; 242 | SDKROOT = macosx; 243 | SWIFT_COMPILATION_MODE = wholemodule; 244 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 245 | }; 246 | name = Release; 247 | }; 248 | B09324AF26AD41590051E175 /* Debug */ = { 249 | isa = XCBuildConfiguration; 250 | buildSettings = { 251 | CODE_SIGN_STYLE = Automatic; 252 | PRODUCT_NAME = "$(TARGET_NAME)"; 253 | SWIFT_VERSION = 5.0; 254 | }; 255 | name = Debug; 256 | }; 257 | B09324B026AD41590051E175 /* Release */ = { 258 | isa = XCBuildConfiguration; 259 | buildSettings = { 260 | CODE_SIGN_STYLE = Automatic; 261 | PRODUCT_NAME = "$(TARGET_NAME)"; 262 | SWIFT_VERSION = 5.0; 263 | }; 264 | name = Release; 265 | }; 266 | /* End XCBuildConfiguration section */ 267 | 268 | /* Begin XCConfigurationList section */ 269 | B09324A226AD41590051E175 /* Build configuration list for PBXProject "Heap" */ = { 270 | isa = XCConfigurationList; 271 | buildConfigurations = ( 272 | B09324AC26AD41590051E175 /* Debug */, 273 | B09324AD26AD41590051E175 /* Release */, 274 | ); 275 | defaultConfigurationIsVisible = 0; 276 | defaultConfigurationName = Release; 277 | }; 278 | B09324AE26AD41590051E175 /* Build configuration list for PBXNativeTarget "Heap" */ = { 279 | isa = XCConfigurationList; 280 | buildConfigurations = ( 281 | B09324AF26AD41590051E175 /* Debug */, 282 | B09324B026AD41590051E175 /* Release */, 283 | ); 284 | defaultConfigurationIsVisible = 0; 285 | defaultConfigurationName = Release; 286 | }; 287 | /* End XCConfigurationList section */ 288 | }; 289 | rootObject = B093249F26AD41590051E175 /* Project object */; 290 | } 291 | -------------------------------------------------------------------------------- /Heap/Heap/Heap.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Heap.swift 3 | // Heap 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct Heap { 11 | private var elements: [T] = [] 12 | private let sortFunction: (T, T) -> Bool 13 | 14 | var isEmpty: Bool { 15 | return self.elements.count == 1 16 | } 17 | var peek: T? { 18 | if self.isEmpty { return nil } 19 | return self.elements.last! 20 | } 21 | var count: Int { 22 | return self.elements.count - 1 23 | } 24 | 25 | init(elements: [T] = [], sortFunction: @escaping (T, T) -> Bool) { 26 | if !elements.isEmpty { 27 | self.elements = [elements.first!] + elements 28 | } else { 29 | self.elements = elements 30 | } 31 | self.sortFunction = sortFunction 32 | if elements.count > 1 { 33 | self.buildHeap() 34 | } 35 | } 36 | 37 | func leftChild(of index: Int) -> Int { 38 | return index * 2 39 | } 40 | func rightChild(of index: Int) -> Int { 41 | return index * 2 + 1 42 | } 43 | func parent(of index: Int) -> Int { 44 | return (index) / 2 45 | } 46 | mutating func add(element: T) { 47 | self.elements.append(element) 48 | } 49 | mutating func diveDown(from index: Int) { 50 | var higherPriority = index 51 | let leftIndex = self.leftChild(of: index) 52 | let rightIndex = self.rightChild(of: index) 53 | 54 | if leftIndex < self.elements.endIndex && self.sortFunction(self.elements[leftIndex], self.elements[higherPriority]) { 55 | higherPriority = leftIndex 56 | } 57 | if rightIndex < self.elements.endIndex && self.sortFunction(self.elements[rightIndex], self.elements[higherPriority]) { 58 | higherPriority = rightIndex 59 | } 60 | if higherPriority != index { 61 | self.elements.swapAt(higherPriority, index) 62 | self.diveDown(from: higherPriority) 63 | } 64 | } 65 | mutating func swimUp(from index: Int) { 66 | var index = index 67 | while index != 1 && self.sortFunction(self.elements[index], self.elements[self.parent(of: index)]) { 68 | self.elements.swapAt(index, self.parent(of: index)) 69 | index = self.parent(of: index) 70 | } 71 | } 72 | mutating func buildHeap() { 73 | for index in (1...(self.elements.count / 2)).reversed() { 74 | self.diveDown(from: index) 75 | } 76 | } 77 | mutating func insert(node: T) { 78 | if self.elements.isEmpty { 79 | self.elements.append(node) 80 | } 81 | self.elements.append(node) 82 | self.swimUp(from: self.elements.endIndex - 1) 83 | } 84 | mutating func remove() -> T? { 85 | if self.isEmpty { return nil } 86 | self.elements.swapAt(1, elements.endIndex - 1) 87 | let deleted = elements.removeLast() 88 | self.diveDown(from: 1) 89 | 90 | return deleted 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /Heap/Heap/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Heap 4 | // Created by 전여훈 on 2021/07/25. 5 | // 6 | 7 | import Foundation 8 | 9 | print("\n======Test Min Heap ======") 10 | var heap = Heap(sortFunction: <) 11 | 12 | for number in [0, 5, 4, 2, 1, 7, 9, 8, 3, 6] { 13 | heap.insert(node: number) 14 | } 15 | while !heap.isEmpty { 16 | print(heap.remove()) 17 | } 18 | 19 | //Optional(0) 20 | //Optional(1) 21 | //Optional(2) 22 | //Optional(3) 23 | //Optional(4) 24 | //Optional(5) 25 | //Optional(6) 26 | //Optional(7) 27 | //Optional(8) 28 | //Optional(9) 29 | 30 | print("\n======Test Max Heap ======") 31 | heap = Heap(sortFunction: >) 32 | 33 | for number in [0, 5, 4, 2, 1, 7, 9, 8, 3, 6] { 34 | heap.insert(node: number) 35 | } 36 | while !heap.isEmpty { 37 | print(heap.remove()) 38 | } 39 | 40 | //Optional(9) 41 | //Optional(8) 42 | //Optional(7) 43 | //Optional(6) 44 | //Optional(5) 45 | //Optional(4) 46 | //Optional(3) 47 | //Optional(2) 48 | //Optional(1) 49 | //Optional(0) 50 | 51 | print("\n======Test Build Heap ======") 52 | heap = Heap(elements: [0, 5, 4, 2, 1, 7, 9, 8, 3, 6], sortFunction: <) 53 | 54 | while !heap.isEmpty { 55 | print(heap.remove()) 56 | } 57 | -------------------------------------------------------------------------------- /LinkedList/LinkedList.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047ABE2764990B00CFB38A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047ABD2764990B00CFB38A /* main.swift */; }; 11 | B0047AC52764991C00CFB38A /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AC42764991C00CFB38A /* Node.swift */; }; 12 | B0047AC72764A19A00CFB38A /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AC62764A19A00CFB38A /* LinkedList.swift */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | B0047AB82764990B00CFB38A /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | ); 23 | runOnlyForDeploymentPostprocessing = 1; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | B0047ABA2764990B00CFB38A /* LinkedList */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = LinkedList; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | B0047ABD2764990B00CFB38A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 30 | B0047AC42764991C00CFB38A /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; 31 | B0047AC62764A19A00CFB38A /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | B0047AB72764990B00CFB38A /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | B0047AB12764990B00CFB38A = { 46 | isa = PBXGroup; 47 | children = ( 48 | B0047ABC2764990B00CFB38A /* LinkedList */, 49 | B0047ABB2764990B00CFB38A /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | B0047ABB2764990B00CFB38A /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | B0047ABA2764990B00CFB38A /* LinkedList */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | B0047ABC2764990B00CFB38A /* LinkedList */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | B0047ABD2764990B00CFB38A /* main.swift */, 65 | B0047AC42764991C00CFB38A /* Node.swift */, 66 | B0047AC62764A19A00CFB38A /* LinkedList.swift */, 67 | ); 68 | path = LinkedList; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | B0047AB92764990B00CFB38A /* LinkedList */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = B0047AC12764990B00CFB38A /* Build configuration list for PBXNativeTarget "LinkedList" */; 77 | buildPhases = ( 78 | B0047AB62764990B00CFB38A /* Sources */, 79 | B0047AB72764990B00CFB38A /* Frameworks */, 80 | B0047AB82764990B00CFB38A /* CopyFiles */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = LinkedList; 87 | productName = LinkedList; 88 | productReference = B0047ABA2764990B00CFB38A /* LinkedList */; 89 | productType = "com.apple.product-type.tool"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | B0047AB22764990B00CFB38A /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | BuildIndependentTargetsInParallel = 1; 98 | LastSwiftUpdateCheck = 1310; 99 | LastUpgradeCheck = 1310; 100 | TargetAttributes = { 101 | B0047AB92764990B00CFB38A = { 102 | CreatedOnToolsVersion = 13.1; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = B0047AB52764990B00CFB38A /* Build configuration list for PBXProject "LinkedList" */; 107 | compatibilityVersion = "Xcode 13.0"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = B0047AB12764990B00CFB38A; 115 | productRefGroup = B0047ABB2764990B00CFB38A /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | B0047AB92764990B00CFB38A /* LinkedList */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXSourcesBuildPhase section */ 125 | B0047AB62764990B00CFB38A /* Sources */ = { 126 | isa = PBXSourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | B0047AC72764A19A00CFB38A /* LinkedList.swift in Sources */, 130 | B0047ABE2764990B00CFB38A /* main.swift in Sources */, 131 | B0047AC52764991C00CFB38A /* Node.swift in Sources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXSourcesBuildPhase section */ 136 | 137 | /* Begin XCBuildConfiguration section */ 138 | B0047ABF2764990B00CFB38A /* Debug */ = { 139 | isa = XCBuildConfiguration; 140 | buildSettings = { 141 | ALWAYS_SEARCH_USER_PATHS = NO; 142 | CLANG_ANALYZER_NONNULL = YES; 143 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 144 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 145 | CLANG_CXX_LIBRARY = "libc++"; 146 | CLANG_ENABLE_MODULES = YES; 147 | CLANG_ENABLE_OBJC_ARC = YES; 148 | CLANG_ENABLE_OBJC_WEAK = YES; 149 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 150 | CLANG_WARN_BOOL_CONVERSION = YES; 151 | CLANG_WARN_COMMA = YES; 152 | CLANG_WARN_CONSTANT_CONVERSION = YES; 153 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 154 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 155 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 156 | CLANG_WARN_EMPTY_BODY = YES; 157 | CLANG_WARN_ENUM_CONVERSION = YES; 158 | CLANG_WARN_INFINITE_RECURSION = YES; 159 | CLANG_WARN_INT_CONVERSION = YES; 160 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 161 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 162 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 163 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 164 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 165 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 166 | CLANG_WARN_STRICT_PROTOTYPES = YES; 167 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 168 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 169 | CLANG_WARN_UNREACHABLE_CODE = YES; 170 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 171 | COPY_PHASE_STRIP = NO; 172 | DEBUG_INFORMATION_FORMAT = dwarf; 173 | ENABLE_STRICT_OBJC_MSGSEND = YES; 174 | ENABLE_TESTABILITY = YES; 175 | GCC_C_LANGUAGE_STANDARD = gnu11; 176 | GCC_DYNAMIC_NO_PIC = NO; 177 | GCC_NO_COMMON_BLOCKS = YES; 178 | GCC_OPTIMIZATION_LEVEL = 0; 179 | GCC_PREPROCESSOR_DEFINITIONS = ( 180 | "DEBUG=1", 181 | "$(inherited)", 182 | ); 183 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 184 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 185 | GCC_WARN_UNDECLARED_SELECTOR = YES; 186 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 187 | GCC_WARN_UNUSED_FUNCTION = YES; 188 | GCC_WARN_UNUSED_VARIABLE = YES; 189 | MACOSX_DEPLOYMENT_TARGET = 12.0; 190 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 191 | MTL_FAST_MATH = YES; 192 | ONLY_ACTIVE_ARCH = YES; 193 | SDKROOT = macosx; 194 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 195 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 196 | }; 197 | name = Debug; 198 | }; 199 | B0047AC02764990B00CFB38A /* Release */ = { 200 | isa = XCBuildConfiguration; 201 | buildSettings = { 202 | ALWAYS_SEARCH_USER_PATHS = NO; 203 | CLANG_ANALYZER_NONNULL = YES; 204 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 205 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 206 | CLANG_CXX_LIBRARY = "libc++"; 207 | CLANG_ENABLE_MODULES = YES; 208 | CLANG_ENABLE_OBJC_ARC = YES; 209 | CLANG_ENABLE_OBJC_WEAK = YES; 210 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 211 | CLANG_WARN_BOOL_CONVERSION = YES; 212 | CLANG_WARN_COMMA = YES; 213 | CLANG_WARN_CONSTANT_CONVERSION = YES; 214 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 215 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 216 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 217 | CLANG_WARN_EMPTY_BODY = YES; 218 | CLANG_WARN_ENUM_CONVERSION = YES; 219 | CLANG_WARN_INFINITE_RECURSION = YES; 220 | CLANG_WARN_INT_CONVERSION = YES; 221 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 222 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 223 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 224 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 225 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 226 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 227 | CLANG_WARN_STRICT_PROTOTYPES = YES; 228 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 229 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 230 | CLANG_WARN_UNREACHABLE_CODE = YES; 231 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 232 | COPY_PHASE_STRIP = NO; 233 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 234 | ENABLE_NS_ASSERTIONS = NO; 235 | ENABLE_STRICT_OBJC_MSGSEND = YES; 236 | GCC_C_LANGUAGE_STANDARD = gnu11; 237 | GCC_NO_COMMON_BLOCKS = YES; 238 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 239 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 240 | GCC_WARN_UNDECLARED_SELECTOR = YES; 241 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 242 | GCC_WARN_UNUSED_FUNCTION = YES; 243 | GCC_WARN_UNUSED_VARIABLE = YES; 244 | MACOSX_DEPLOYMENT_TARGET = 12.0; 245 | MTL_ENABLE_DEBUG_INFO = NO; 246 | MTL_FAST_MATH = YES; 247 | SDKROOT = macosx; 248 | SWIFT_COMPILATION_MODE = wholemodule; 249 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 250 | }; 251 | name = Release; 252 | }; 253 | B0047AC22764990B00CFB38A /* Debug */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | CODE_SIGN_STYLE = Automatic; 257 | DEVELOPMENT_TEAM = CH6R3C4FBD; 258 | ENABLE_HARDENED_RUNTIME = YES; 259 | PRODUCT_NAME = "$(TARGET_NAME)"; 260 | SWIFT_VERSION = 5.0; 261 | }; 262 | name = Debug; 263 | }; 264 | B0047AC32764990B00CFB38A /* Release */ = { 265 | isa = XCBuildConfiguration; 266 | buildSettings = { 267 | CODE_SIGN_STYLE = Automatic; 268 | DEVELOPMENT_TEAM = CH6R3C4FBD; 269 | ENABLE_HARDENED_RUNTIME = YES; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | SWIFT_VERSION = 5.0; 272 | }; 273 | name = Release; 274 | }; 275 | /* End XCBuildConfiguration section */ 276 | 277 | /* Begin XCConfigurationList section */ 278 | B0047AB52764990B00CFB38A /* Build configuration list for PBXProject "LinkedList" */ = { 279 | isa = XCConfigurationList; 280 | buildConfigurations = ( 281 | B0047ABF2764990B00CFB38A /* Debug */, 282 | B0047AC02764990B00CFB38A /* Release */, 283 | ); 284 | defaultConfigurationIsVisible = 0; 285 | defaultConfigurationName = Release; 286 | }; 287 | B0047AC12764990B00CFB38A /* Build configuration list for PBXNativeTarget "LinkedList" */ = { 288 | isa = XCConfigurationList; 289 | buildConfigurations = ( 290 | B0047AC22764990B00CFB38A /* Debug */, 291 | B0047AC32764990B00CFB38A /* Release */, 292 | ); 293 | defaultConfigurationIsVisible = 0; 294 | defaultConfigurationName = Release; 295 | }; 296 | /* End XCConfigurationList section */ 297 | }; 298 | rootObject = B0047AB22764990B00CFB38A /* Project object */; 299 | } 300 | -------------------------------------------------------------------------------- /LinkedList/LinkedList/LinkedList.swift: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedList.swift 3 | // LinkedList 4 | // 5 | // Created by 전여훈 on 2021/12/11. 6 | // 7 | 8 | import Foundation 9 | 10 | struct LinkedList { 11 | var head: Node? 12 | var tail: Node? 13 | 14 | mutating func add(node: Node) { 15 | // head node does not exist 16 | if head == nil { 17 | head = node 18 | tail = node 19 | return 20 | } 21 | 22 | // search for last node and attatch new 23 | tail?.next = node 24 | tail = node 25 | } 26 | 27 | func searchNode(with data: T) -> Node? { 28 | var now = head 29 | while now?.data != data && now != nil { 30 | now = now?.next 31 | } 32 | return now 33 | } 34 | 35 | mutating func insert(node: Node, after id: Int) { 36 | var now = head 37 | while now?.id != id && now?.next != nil { 38 | now = now?.next 39 | } 40 | 41 | node.next = now?.next 42 | now?.next = node 43 | } 44 | 45 | mutating func insert(node: Node, before id: Int) { 46 | var now = head 47 | 48 | if now?.id == id { 49 | head = node 50 | node.next = now 51 | return 52 | } 53 | 54 | while now?.next?.id != id && now?.next != nil { 55 | now = now?.next 56 | } 57 | 58 | node.next = now?.next 59 | now?.next = node 60 | } 61 | 62 | mutating func delete(node: Node) -> Bool { 63 | var now = self.head 64 | 65 | // if target node is at head 66 | if now === node { 67 | if self.head === self.tail { self.tail = nil } 68 | self.head = now?.next 69 | return true 70 | } 71 | 72 | while now?.next !== node && now?.next != nil { 73 | now = now?.next 74 | } 75 | 76 | // no matching node to delete 77 | if now == nil { return false } 78 | 79 | if now?.next === tail { 80 | tail = now 81 | } 82 | 83 | // delete node 84 | now?.next = now?.next?.next 85 | return true 86 | } 87 | 88 | func showList() { 89 | var now = head 90 | print("===== Linked List ======") 91 | while now != nil { 92 | now?.next == nil 93 | ? print("id: \(now?.id) | data: \(now?.data)") 94 | : print("id: \(now?.id) | data: \(now?.data) -> ") 95 | now = now?.next 96 | } 97 | print("========================") 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /LinkedList/LinkedList/Node.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Node.swift 3 | // LinkedList 4 | // 5 | // Created by 전여훈 on 2021/12/11. 6 | // 7 | 8 | import Foundation 9 | 10 | class Node { 11 | let id: Int 12 | let data: T 13 | var next: Node? 14 | 15 | init(id: Int, data: T, next: Node? = nil) { 16 | self.id = id 17 | self.data = data 18 | self.next = next 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LinkedList/LinkedList/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // LinkedList 4 | // 5 | // Created by 전여훈 on 2021/12/11. 6 | // 7 | 8 | import Foundation 9 | 10 | var list = LinkedList() 11 | list.add(node: Node(id: 1, data: 0)) 12 | list.add(node: Node(id: 2, data: 1)) 13 | list.add(node: Node(id: 3, data: 2)) 14 | list.add(node: Node(id: 4, data: 3)) 15 | list.add(node: Node(id: 5, data: 6)) 16 | list.add(node: Node(id: 6, data: 5)) 17 | list.add(node: Node(id: 7, data: 10)) 18 | 19 | print("search node with data 0") 20 | let deletionTargetNode = list.searchNode(with: 0) 21 | list.showList() 22 | 23 | print("delete node with data 0") 24 | print(list.delete(node: deletionTargetNode!)) 25 | list.showList() 26 | 27 | print("search node with data 0") 28 | print(list.searchNode(with: 0)) 29 | 30 | print("delete node with data 10") 31 | list.delete(node: list.searchNode(with: 10)!) 32 | list.showList() 33 | 34 | 35 | print("insert node after node with id(2)") 36 | list.insert(node: Node(id: 30, data: 2), after: 2) 37 | list.showList() 38 | 39 | print("insert node before node with id (3)") 40 | list.insert(node: Node(id: 50, data: 3), before: 2) 41 | list.showList() 42 | 43 | // 44 | //search node with data 0 45 | //===== Linked List ====== 46 | //id: Optional(1) | data: Optional(0) -> 47 | //id: Optional(2) | data: Optional(1) -> 48 | //id: Optional(3) | data: Optional(2) -> 49 | //id: Optional(4) | data: Optional(3) -> 50 | //id: Optional(5) | data: Optional(6) -> 51 | //id: Optional(6) | data: Optional(5) -> 52 | //id: Optional(7) | data: Optional(10) 53 | //======================== 54 | //delete node with data 0 55 | //true 56 | //===== Linked List ====== 57 | //id: Optional(2) | data: Optional(1) -> 58 | //id: Optional(3) | data: Optional(2) -> 59 | //id: Optional(4) | data: Optional(3) -> 60 | //id: Optional(5) | data: Optional(6) -> 61 | //id: Optional(6) | data: Optional(5) -> 62 | //id: Optional(7) | data: Optional(10) 63 | //======================== 64 | //search node with data 0 65 | //nil 66 | //delete node with data 10 67 | //===== Linked List ====== 68 | //id: Optional(2) | data: Optional(1) -> 69 | //id: Optional(3) | data: Optional(2) -> 70 | //id: Optional(4) | data: Optional(3) -> 71 | //id: Optional(5) | data: Optional(6) -> 72 | //id: Optional(6) | data: Optional(5) 73 | //======================== 74 | //insert node after node with id(2) 75 | //===== Linked List ====== 76 | //id: Optional(2) | data: Optional(1) -> 77 | //id: Optional(30) | data: Optional(2) -> 78 | //id: Optional(3) | data: Optional(2) -> 79 | //id: Optional(4) | data: Optional(3) -> 80 | //id: Optional(5) | data: Optional(6) -> 81 | //id: Optional(6) | data: Optional(5) 82 | //======================== 83 | //insert node before node with id (3) 84 | //===== Linked List ====== 85 | //id: Optional(50) | data: Optional(3) -> 86 | //id: Optional(2) | data: Optional(1) -> 87 | //id: Optional(30) | data: Optional(2) -> 88 | //id: Optional(3) | data: Optional(2) -> 89 | //id: Optional(4) | data: Optional(3) -> 90 | //id: Optional(5) | data: Optional(6) -> 91 | //id: Optional(6) | data: Optional(5) 92 | //======================== 93 | //Program ended with exit code: 0 94 | -------------------------------------------------------------------------------- /PriorityQueue/PriorityQueue.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047AE22765C8C000CFB38A /* PriorityQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AE12765C8C000CFB38A /* PriorityQueue.swift */; }; 11 | B09324BE26AD4F800051E175 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09324BD26AD4F800051E175 /* main.swift */; }; 12 | B09324C526AD4F8F0051E175 /* Heap.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09324C426AD4F8F0051E175 /* Heap.swift */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | B09324B826AD4F800051E175 /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | ); 23 | runOnlyForDeploymentPostprocessing = 1; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | B0047AE12765C8C000CFB38A /* PriorityQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriorityQueue.swift; sourceTree = ""; }; 29 | B09324BA26AD4F800051E175 /* PriorityQueue */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = PriorityQueue; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | B09324BD26AD4F800051E175 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 31 | B09324C426AD4F8F0051E175 /* Heap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Heap.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | B09324B726AD4F800051E175 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | B09324B126AD4F800051E175 = { 46 | isa = PBXGroup; 47 | children = ( 48 | B09324BC26AD4F800051E175 /* PriorityQueue */, 49 | B09324BB26AD4F800051E175 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | B09324BB26AD4F800051E175 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | B09324BA26AD4F800051E175 /* PriorityQueue */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | B09324BC26AD4F800051E175 /* PriorityQueue */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | B09324BD26AD4F800051E175 /* main.swift */, 65 | B09324C426AD4F8F0051E175 /* Heap.swift */, 66 | B0047AE12765C8C000CFB38A /* PriorityQueue.swift */, 67 | ); 68 | path = PriorityQueue; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | B09324B926AD4F800051E175 /* PriorityQueue */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = B09324C126AD4F800051E175 /* Build configuration list for PBXNativeTarget "PriorityQueue" */; 77 | buildPhases = ( 78 | B09324B626AD4F800051E175 /* Sources */, 79 | B09324B726AD4F800051E175 /* Frameworks */, 80 | B09324B826AD4F800051E175 /* CopyFiles */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = PriorityQueue; 87 | productName = PriorityQueue; 88 | productReference = B09324BA26AD4F800051E175 /* PriorityQueue */; 89 | productType = "com.apple.product-type.tool"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | B09324B226AD4F800051E175 /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | LastSwiftUpdateCheck = 1250; 98 | LastUpgradeCheck = 1250; 99 | TargetAttributes = { 100 | B09324B926AD4F800051E175 = { 101 | CreatedOnToolsVersion = 12.5.1; 102 | }; 103 | }; 104 | }; 105 | buildConfigurationList = B09324B526AD4F800051E175 /* Build configuration list for PBXProject "PriorityQueue" */; 106 | compatibilityVersion = "Xcode 9.3"; 107 | developmentRegion = en; 108 | hasScannedForEncodings = 0; 109 | knownRegions = ( 110 | en, 111 | Base, 112 | ); 113 | mainGroup = B09324B126AD4F800051E175; 114 | productRefGroup = B09324BB26AD4F800051E175 /* Products */; 115 | projectDirPath = ""; 116 | projectRoot = ""; 117 | targets = ( 118 | B09324B926AD4F800051E175 /* PriorityQueue */, 119 | ); 120 | }; 121 | /* End PBXProject section */ 122 | 123 | /* Begin PBXSourcesBuildPhase section */ 124 | B09324B626AD4F800051E175 /* Sources */ = { 125 | isa = PBXSourcesBuildPhase; 126 | buildActionMask = 2147483647; 127 | files = ( 128 | B09324BE26AD4F800051E175 /* main.swift in Sources */, 129 | B09324C526AD4F8F0051E175 /* Heap.swift in Sources */, 130 | B0047AE22765C8C000CFB38A /* PriorityQueue.swift in Sources */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXSourcesBuildPhase section */ 135 | 136 | /* Begin XCBuildConfiguration section */ 137 | B09324BF26AD4F800051E175 /* Debug */ = { 138 | isa = XCBuildConfiguration; 139 | buildSettings = { 140 | ALWAYS_SEARCH_USER_PATHS = NO; 141 | CLANG_ANALYZER_NONNULL = YES; 142 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 143 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 144 | CLANG_CXX_LIBRARY = "libc++"; 145 | CLANG_ENABLE_MODULES = YES; 146 | CLANG_ENABLE_OBJC_ARC = YES; 147 | CLANG_ENABLE_OBJC_WEAK = YES; 148 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 149 | CLANG_WARN_BOOL_CONVERSION = YES; 150 | CLANG_WARN_COMMA = YES; 151 | CLANG_WARN_CONSTANT_CONVERSION = YES; 152 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 153 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 154 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 155 | CLANG_WARN_EMPTY_BODY = YES; 156 | CLANG_WARN_ENUM_CONVERSION = YES; 157 | CLANG_WARN_INFINITE_RECURSION = YES; 158 | CLANG_WARN_INT_CONVERSION = YES; 159 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 160 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 161 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 162 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 163 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 164 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 165 | CLANG_WARN_STRICT_PROTOTYPES = YES; 166 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 167 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 168 | CLANG_WARN_UNREACHABLE_CODE = YES; 169 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 170 | COPY_PHASE_STRIP = NO; 171 | DEBUG_INFORMATION_FORMAT = dwarf; 172 | ENABLE_STRICT_OBJC_MSGSEND = YES; 173 | ENABLE_TESTABILITY = YES; 174 | GCC_C_LANGUAGE_STANDARD = gnu11; 175 | GCC_DYNAMIC_NO_PIC = NO; 176 | GCC_NO_COMMON_BLOCKS = YES; 177 | GCC_OPTIMIZATION_LEVEL = 0; 178 | GCC_PREPROCESSOR_DEFINITIONS = ( 179 | "DEBUG=1", 180 | "$(inherited)", 181 | ); 182 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 183 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 184 | GCC_WARN_UNDECLARED_SELECTOR = YES; 185 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 186 | GCC_WARN_UNUSED_FUNCTION = YES; 187 | GCC_WARN_UNUSED_VARIABLE = YES; 188 | MACOSX_DEPLOYMENT_TARGET = 11.3; 189 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 190 | MTL_FAST_MATH = YES; 191 | ONLY_ACTIVE_ARCH = YES; 192 | SDKROOT = macosx; 193 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 194 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 195 | }; 196 | name = Debug; 197 | }; 198 | B09324C026AD4F800051E175 /* Release */ = { 199 | isa = XCBuildConfiguration; 200 | buildSettings = { 201 | ALWAYS_SEARCH_USER_PATHS = NO; 202 | CLANG_ANALYZER_NONNULL = YES; 203 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 204 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 205 | CLANG_CXX_LIBRARY = "libc++"; 206 | CLANG_ENABLE_MODULES = YES; 207 | CLANG_ENABLE_OBJC_ARC = YES; 208 | CLANG_ENABLE_OBJC_WEAK = YES; 209 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 210 | CLANG_WARN_BOOL_CONVERSION = YES; 211 | CLANG_WARN_COMMA = YES; 212 | CLANG_WARN_CONSTANT_CONVERSION = YES; 213 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 214 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 215 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 216 | CLANG_WARN_EMPTY_BODY = YES; 217 | CLANG_WARN_ENUM_CONVERSION = YES; 218 | CLANG_WARN_INFINITE_RECURSION = YES; 219 | CLANG_WARN_INT_CONVERSION = YES; 220 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 221 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 222 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 223 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 224 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 225 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 226 | CLANG_WARN_STRICT_PROTOTYPES = YES; 227 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 228 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 229 | CLANG_WARN_UNREACHABLE_CODE = YES; 230 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 231 | COPY_PHASE_STRIP = NO; 232 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 233 | ENABLE_NS_ASSERTIONS = NO; 234 | ENABLE_STRICT_OBJC_MSGSEND = YES; 235 | GCC_C_LANGUAGE_STANDARD = gnu11; 236 | GCC_NO_COMMON_BLOCKS = YES; 237 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 238 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 239 | GCC_WARN_UNDECLARED_SELECTOR = YES; 240 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 241 | GCC_WARN_UNUSED_FUNCTION = YES; 242 | GCC_WARN_UNUSED_VARIABLE = YES; 243 | MACOSX_DEPLOYMENT_TARGET = 11.3; 244 | MTL_ENABLE_DEBUG_INFO = NO; 245 | MTL_FAST_MATH = YES; 246 | SDKROOT = macosx; 247 | SWIFT_COMPILATION_MODE = wholemodule; 248 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 249 | }; 250 | name = Release; 251 | }; 252 | B09324C226AD4F800051E175 /* Debug */ = { 253 | isa = XCBuildConfiguration; 254 | buildSettings = { 255 | CODE_SIGN_STYLE = Automatic; 256 | PRODUCT_NAME = "$(TARGET_NAME)"; 257 | SWIFT_VERSION = 5.0; 258 | }; 259 | name = Debug; 260 | }; 261 | B09324C326AD4F800051E175 /* Release */ = { 262 | isa = XCBuildConfiguration; 263 | buildSettings = { 264 | CODE_SIGN_STYLE = Automatic; 265 | PRODUCT_NAME = "$(TARGET_NAME)"; 266 | SWIFT_VERSION = 5.0; 267 | }; 268 | name = Release; 269 | }; 270 | /* End XCBuildConfiguration section */ 271 | 272 | /* Begin XCConfigurationList section */ 273 | B09324B526AD4F800051E175 /* Build configuration list for PBXProject "PriorityQueue" */ = { 274 | isa = XCConfigurationList; 275 | buildConfigurations = ( 276 | B09324BF26AD4F800051E175 /* Debug */, 277 | B09324C026AD4F800051E175 /* Release */, 278 | ); 279 | defaultConfigurationIsVisible = 0; 280 | defaultConfigurationName = Release; 281 | }; 282 | B09324C126AD4F800051E175 /* Build configuration list for PBXNativeTarget "PriorityQueue" */ = { 283 | isa = XCConfigurationList; 284 | buildConfigurations = ( 285 | B09324C226AD4F800051E175 /* Debug */, 286 | B09324C326AD4F800051E175 /* Release */, 287 | ); 288 | defaultConfigurationIsVisible = 0; 289 | defaultConfigurationName = Release; 290 | }; 291 | /* End XCConfigurationList section */ 292 | }; 293 | rootObject = B09324B226AD4F800051E175 /* Project object */; 294 | } 295 | -------------------------------------------------------------------------------- /PriorityQueue/PriorityQueue/Heap.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Heap.swift 3 | // Heap 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct Heap { 11 | private var elements: [T] 12 | private let sortFunction: (T, T) -> Bool 13 | 14 | var isEmpty: Bool { 15 | return self.elements.isEmpty 16 | } 17 | var peek: T? { 18 | return self.elements.first 19 | } 20 | var count: Int { 21 | return self.elements.count 22 | } 23 | 24 | init(elements: [T] = [], sortFunction: @escaping (T, T) -> Bool) { 25 | self.elements = elements 26 | self.sortFunction = sortFunction 27 | if !elements.isEmpty { 28 | self.buildHeap() 29 | } 30 | } 31 | 32 | func leftChild(of index: Int) -> Int { 33 | return index * 2 34 | } 35 | func rightChild(of index: Int) -> Int { 36 | return index * 2 + 1 37 | } 38 | func parent(of index: Int) -> Int { 39 | return index / 2 40 | } 41 | mutating func add(element: T) { 42 | self.elements.append(element) 43 | } 44 | mutating func diveDown(from index: Int) { 45 | var higherPriority = index 46 | let leftIndex = self.leftChild(of: index) 47 | let rightIndex = self.rightChild(of: index) 48 | 49 | if leftIndex < self.elements.endIndex && self.sortFunction(self.elements[leftIndex], self.elements[higherPriority]) { 50 | higherPriority = leftIndex 51 | } 52 | if rightIndex < self.elements.endIndex && self.sortFunction(self.elements[rightIndex], self.elements[higherPriority]) { 53 | higherPriority = rightIndex 54 | } 55 | if higherPriority != index { 56 | self.elements.swapAt(higherPriority, index) 57 | self.diveDown(from: higherPriority) 58 | } 59 | } 60 | mutating func swimUp(from index: Int) { 61 | var index = index 62 | while index >= 0 && self.sortFunction(self.elements[index], self.elements[index/2]) { 63 | self.elements.swapAt(index/2, index) 64 | index /= 2 65 | } 66 | } 67 | mutating func buildHeap() { 68 | for index in (0...(self.elements.count / 2)).reversed() { 69 | self.diveDown(from: index) 70 | } 71 | } 72 | mutating func insert(node: T) { 73 | self.elements.append(node) 74 | self.swimUp(from: self.elements.endIndex - 1) 75 | } 76 | mutating func remove() -> T? { 77 | if self.elements.isEmpty { return nil } 78 | self.elements.swapAt(0, elements.endIndex - 1) 79 | let deleted = elements.removeLast() 80 | self.diveDown(from: 0) 81 | 82 | return deleted 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /PriorityQueue/PriorityQueue/PriorityQueue.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PriorityQueue.swift 3 | // PriorityQueue 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct PriorityQueue { 11 | var heap: Heap 12 | 13 | init(_ elements: [T] = [], _ sort: @escaping (T, T) -> Bool) { 14 | heap = Heap(elements: elements, sortFunction: sort) 15 | } 16 | 17 | var count : Int { 18 | return heap.count 19 | } 20 | var isEmpty : Bool { 21 | return heap.isEmpty 22 | } 23 | 24 | func top () -> T? { 25 | return heap.peek 26 | } 27 | mutating func clear () { 28 | while !heap.isEmpty { 29 | _ = heap.remove() 30 | } 31 | } 32 | mutating func pop() -> T? { 33 | return heap.remove() 34 | } 35 | mutating func push(_ element: T) { 36 | heap.insert(node: element) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /PriorityQueue/PriorityQueue/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // PriorityQueue 4 | // 5 | // Created by 전여훈 on 2021/07/25. 6 | // 7 | 8 | import Foundation 9 | 10 | var pq: PriorityQueue = PriorityQueue([], <) 11 | pq.push(1) 12 | pq.push(3) 13 | pq.push(2) 14 | pq.push(5) 15 | 16 | print(pq.pop()!) 17 | print(pq.top()!) 18 | 19 | 20 | while !pq.isEmpty { 21 | print(pq.pop()) 22 | } 23 | -------------------------------------------------------------------------------- /Queue/Queue.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0047AFB2765D31C00CFB38A /* TwoPointerLinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AFA2765D31C00CFB38A /* TwoPointerLinkedList.swift */; }; 11 | B0047AFD2765D33000CFB38A /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AFC2765D33000CFB38A /* Node.swift */; }; 12 | B0047AFF2765D41900CFB38A /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0047AFE2765D41900CFB38A /* Queue.swift */; }; 13 | B0C31F7726BFF55100687C28 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0C31F7626BFF55100687C28 /* main.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | B0C31F7126BFF55100687C28 /* CopyFiles */ = { 18 | isa = PBXCopyFilesBuildPhase; 19 | buildActionMask = 2147483647; 20 | dstPath = /usr/share/man/man1/; 21 | dstSubfolderSpec = 0; 22 | files = ( 23 | ); 24 | runOnlyForDeploymentPostprocessing = 1; 25 | }; 26 | /* End PBXCopyFilesBuildPhase section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | B0047AFA2765D31C00CFB38A /* TwoPointerLinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPointerLinkedList.swift; sourceTree = ""; }; 30 | B0047AFC2765D33000CFB38A /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; 31 | B0047AFE2765D41900CFB38A /* Queue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queue.swift; sourceTree = ""; }; 32 | B0C31F7326BFF55100687C28 /* Queue */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Queue; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | B0C31F7626BFF55100687C28 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | B0C31F7026BFF55100687C28 /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | B0C31F6A26BFF55100687C28 = { 48 | isa = PBXGroup; 49 | children = ( 50 | B0C31F7526BFF55100687C28 /* Queue */, 51 | B0C31F7426BFF55100687C28 /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | B0C31F7426BFF55100687C28 /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | B0C31F7326BFF55100687C28 /* Queue */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | B0C31F7526BFF55100687C28 /* Queue */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | B0C31F7626BFF55100687C28 /* main.swift */, 67 | B0047AFA2765D31C00CFB38A /* TwoPointerLinkedList.swift */, 68 | B0047AFC2765D33000CFB38A /* Node.swift */, 69 | B0047AFE2765D41900CFB38A /* Queue.swift */, 70 | ); 71 | path = Queue; 72 | sourceTree = ""; 73 | }; 74 | /* End PBXGroup section */ 75 | 76 | /* Begin PBXNativeTarget section */ 77 | B0C31F7226BFF55100687C28 /* Queue */ = { 78 | isa = PBXNativeTarget; 79 | buildConfigurationList = B0C31F7A26BFF55100687C28 /* Build configuration list for PBXNativeTarget "Queue" */; 80 | buildPhases = ( 81 | B0C31F6F26BFF55100687C28 /* Sources */, 82 | B0C31F7026BFF55100687C28 /* Frameworks */, 83 | B0C31F7126BFF55100687C28 /* CopyFiles */, 84 | ); 85 | buildRules = ( 86 | ); 87 | dependencies = ( 88 | ); 89 | name = Queue; 90 | productName = Queue; 91 | productReference = B0C31F7326BFF55100687C28 /* Queue */; 92 | productType = "com.apple.product-type.tool"; 93 | }; 94 | /* End PBXNativeTarget section */ 95 | 96 | /* Begin PBXProject section */ 97 | B0C31F6B26BFF55100687C28 /* Project object */ = { 98 | isa = PBXProject; 99 | attributes = { 100 | LastSwiftUpdateCheck = 1250; 101 | LastUpgradeCheck = 1250; 102 | TargetAttributes = { 103 | B0C31F7226BFF55100687C28 = { 104 | CreatedOnToolsVersion = 12.5.1; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = B0C31F6E26BFF55100687C28 /* Build configuration list for PBXProject "Queue" */; 109 | compatibilityVersion = "Xcode 9.3"; 110 | developmentRegion = en; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | Base, 115 | ); 116 | mainGroup = B0C31F6A26BFF55100687C28; 117 | productRefGroup = B0C31F7426BFF55100687C28 /* Products */; 118 | projectDirPath = ""; 119 | projectRoot = ""; 120 | targets = ( 121 | B0C31F7226BFF55100687C28 /* Queue */, 122 | ); 123 | }; 124 | /* End PBXProject section */ 125 | 126 | /* Begin PBXSourcesBuildPhase section */ 127 | B0C31F6F26BFF55100687C28 /* Sources */ = { 128 | isa = PBXSourcesBuildPhase; 129 | buildActionMask = 2147483647; 130 | files = ( 131 | B0047AFD2765D33000CFB38A /* Node.swift in Sources */, 132 | B0C31F7726BFF55100687C28 /* main.swift in Sources */, 133 | B0047AFB2765D31C00CFB38A /* TwoPointerLinkedList.swift in Sources */, 134 | B0047AFF2765D41900CFB38A /* Queue.swift in Sources */, 135 | ); 136 | runOnlyForDeploymentPostprocessing = 0; 137 | }; 138 | /* End PBXSourcesBuildPhase section */ 139 | 140 | /* Begin XCBuildConfiguration section */ 141 | B0C31F7826BFF55100687C28 /* Debug */ = { 142 | isa = XCBuildConfiguration; 143 | buildSettings = { 144 | ALWAYS_SEARCH_USER_PATHS = NO; 145 | CLANG_ANALYZER_NONNULL = YES; 146 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 147 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 148 | CLANG_CXX_LIBRARY = "libc++"; 149 | CLANG_ENABLE_MODULES = YES; 150 | CLANG_ENABLE_OBJC_ARC = YES; 151 | CLANG_ENABLE_OBJC_WEAK = YES; 152 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 153 | CLANG_WARN_BOOL_CONVERSION = YES; 154 | CLANG_WARN_COMMA = YES; 155 | CLANG_WARN_CONSTANT_CONVERSION = YES; 156 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 157 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 158 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 159 | CLANG_WARN_EMPTY_BODY = YES; 160 | CLANG_WARN_ENUM_CONVERSION = YES; 161 | CLANG_WARN_INFINITE_RECURSION = YES; 162 | CLANG_WARN_INT_CONVERSION = YES; 163 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 164 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 165 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 166 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 167 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 168 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 169 | CLANG_WARN_STRICT_PROTOTYPES = YES; 170 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 171 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 172 | CLANG_WARN_UNREACHABLE_CODE = YES; 173 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 174 | COPY_PHASE_STRIP = NO; 175 | DEBUG_INFORMATION_FORMAT = dwarf; 176 | ENABLE_STRICT_OBJC_MSGSEND = YES; 177 | ENABLE_TESTABILITY = YES; 178 | GCC_C_LANGUAGE_STANDARD = gnu11; 179 | GCC_DYNAMIC_NO_PIC = NO; 180 | GCC_NO_COMMON_BLOCKS = YES; 181 | GCC_OPTIMIZATION_LEVEL = 0; 182 | GCC_PREPROCESSOR_DEFINITIONS = ( 183 | "DEBUG=1", 184 | "$(inherited)", 185 | ); 186 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 187 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 188 | GCC_WARN_UNDECLARED_SELECTOR = YES; 189 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 190 | GCC_WARN_UNUSED_FUNCTION = YES; 191 | GCC_WARN_UNUSED_VARIABLE = YES; 192 | MACOSX_DEPLOYMENT_TARGET = 11.3; 193 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 194 | MTL_FAST_MATH = YES; 195 | ONLY_ACTIVE_ARCH = YES; 196 | SDKROOT = macosx; 197 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 198 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 199 | }; 200 | name = Debug; 201 | }; 202 | B0C31F7926BFF55100687C28 /* Release */ = { 203 | isa = XCBuildConfiguration; 204 | buildSettings = { 205 | ALWAYS_SEARCH_USER_PATHS = NO; 206 | CLANG_ANALYZER_NONNULL = YES; 207 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 208 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 209 | CLANG_CXX_LIBRARY = "libc++"; 210 | CLANG_ENABLE_MODULES = YES; 211 | CLANG_ENABLE_OBJC_ARC = YES; 212 | CLANG_ENABLE_OBJC_WEAK = YES; 213 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 214 | CLANG_WARN_BOOL_CONVERSION = YES; 215 | CLANG_WARN_COMMA = YES; 216 | CLANG_WARN_CONSTANT_CONVERSION = YES; 217 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 218 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 219 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 220 | CLANG_WARN_EMPTY_BODY = YES; 221 | CLANG_WARN_ENUM_CONVERSION = YES; 222 | CLANG_WARN_INFINITE_RECURSION = YES; 223 | CLANG_WARN_INT_CONVERSION = YES; 224 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 225 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 226 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 227 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 228 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 229 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 230 | CLANG_WARN_STRICT_PROTOTYPES = YES; 231 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 232 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 233 | CLANG_WARN_UNREACHABLE_CODE = YES; 234 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 235 | COPY_PHASE_STRIP = NO; 236 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 237 | ENABLE_NS_ASSERTIONS = NO; 238 | ENABLE_STRICT_OBJC_MSGSEND = YES; 239 | GCC_C_LANGUAGE_STANDARD = gnu11; 240 | GCC_NO_COMMON_BLOCKS = YES; 241 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 242 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 243 | GCC_WARN_UNDECLARED_SELECTOR = YES; 244 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 245 | GCC_WARN_UNUSED_FUNCTION = YES; 246 | GCC_WARN_UNUSED_VARIABLE = YES; 247 | MACOSX_DEPLOYMENT_TARGET = 11.3; 248 | MTL_ENABLE_DEBUG_INFO = NO; 249 | MTL_FAST_MATH = YES; 250 | SDKROOT = macosx; 251 | SWIFT_COMPILATION_MODE = wholemodule; 252 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 253 | }; 254 | name = Release; 255 | }; 256 | B0C31F7B26BFF55100687C28 /* Debug */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | CODE_SIGN_STYLE = Automatic; 260 | PRODUCT_NAME = "$(TARGET_NAME)"; 261 | SWIFT_VERSION = 5.0; 262 | }; 263 | name = Debug; 264 | }; 265 | B0C31F7C26BFF55100687C28 /* Release */ = { 266 | isa = XCBuildConfiguration; 267 | buildSettings = { 268 | CODE_SIGN_STYLE = Automatic; 269 | PRODUCT_NAME = "$(TARGET_NAME)"; 270 | SWIFT_VERSION = 5.0; 271 | }; 272 | name = Release; 273 | }; 274 | /* End XCBuildConfiguration section */ 275 | 276 | /* Begin XCConfigurationList section */ 277 | B0C31F6E26BFF55100687C28 /* Build configuration list for PBXProject "Queue" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | B0C31F7826BFF55100687C28 /* Debug */, 281 | B0C31F7926BFF55100687C28 /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | defaultConfigurationName = Release; 285 | }; 286 | B0C31F7A26BFF55100687C28 /* Build configuration list for PBXNativeTarget "Queue" */ = { 287 | isa = XCConfigurationList; 288 | buildConfigurations = ( 289 | B0C31F7B26BFF55100687C28 /* Debug */, 290 | B0C31F7C26BFF55100687C28 /* Release */, 291 | ); 292 | defaultConfigurationIsVisible = 0; 293 | defaultConfigurationName = Release; 294 | }; 295 | /* End XCConfigurationList section */ 296 | }; 297 | rootObject = B0C31F6B26BFF55100687C28 /* Project object */; 298 | } 299 | -------------------------------------------------------------------------------- /Queue/Queue/Node.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Node.swift 3 | // Queue 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | class Node { 11 | let data: T 12 | var next: Node? = nil 13 | 14 | init(data: T) { 15 | self.data = data 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Queue/Queue/Queue.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Queue.swift 3 | // Queue 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct Queue { 11 | var list = TwoPointerLinkedList() 12 | 13 | init(_ elements: [T] = []) { 14 | for element in elements { 15 | list.add(node: Node(data: element)) 16 | } 17 | } 18 | 19 | var count : Int { 20 | return list.count 21 | } 22 | var isEmpty : Bool { 23 | return list.head == nil 24 | } 25 | var front: T? { 26 | return list.front?.data 27 | } 28 | var back: T? { 29 | return list.back?.data 30 | } 31 | 32 | mutating func clear () { 33 | list.clear() 34 | } 35 | 36 | mutating func pop() -> T? { 37 | return list.removeFirst()?.data 38 | } 39 | 40 | mutating func push(_ element: T) { 41 | list.add(node: Node(data: element)) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Queue/Queue/TwoPointerLinkedList.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TwoPointerLinkedList.swift 3 | // Queue 4 | // 5 | // Created by 전여훈 on 2021/12/12. 6 | // 7 | 8 | import Foundation 9 | 10 | struct TwoPointerLinkedList { 11 | var head: Node? 12 | var tail: Node? 13 | var count: Int = 0 14 | 15 | var front: Node? { 16 | return head 17 | } 18 | var back: Node? { 19 | return tail 20 | } 21 | 22 | mutating func add(node: Node) { 23 | if self.head == nil { 24 | self.head = node 25 | self.tail = node 26 | } else { 27 | self.tail?.next = node 28 | self.tail = node 29 | } 30 | self.count += 1 31 | } 32 | mutating func removeFirst() -> Node? { 33 | guard head != nil else { 34 | self.clear() 35 | return nil 36 | } 37 | let deleted = self.head 38 | self.head = head?.next 39 | self.count -= 1 40 | 41 | if head == nil { 42 | self.clear() 43 | } 44 | return deleted 45 | } 46 | mutating func clear() { 47 | self.head = nil 48 | self.tail = nil 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Queue/Queue/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Queue 4 | // 5 | // Created by 전여훈 on 2021/08/08. 6 | // 7 | 8 | import Foundation 9 | 10 | var queue = Queue() 11 | 12 | for i in 0..<20 { 13 | queue.push(i) 14 | } 15 | 16 | print(queue.count) 17 | 18 | while !queue.isEmpty { 19 | print(queue.pop()) 20 | } 21 | 22 | print(queue.count) 23 | 24 | for i in 0..<20 { 25 | queue.push(i) 26 | } 27 | 28 | queue.clear() 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-In-Swift 2 | 👷🏻‍♂️ STL 왜 없어.. 스위프트로 구현하는 자료구조 3 | 4 | ## Linked List 5 | ## Doubly Linked List 6 | ## Stack 7 | ## Queue 8 | ## Circular Queue 9 | ## Heap 10 | ## Priority Queue 11 | ## Binary Tree 12 | ## Binary Search Tree 13 | ## AVL Tree 14 | -------------------------------------------------------------------------------- /Stack/Stack.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B0006CC526B6B78700D82DEC /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0006CC426B6B78700D82DEC /* main.swift */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | B0006CBF26B6B78700D82DEC /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = /usr/share/man/man1/; 18 | dstSubfolderSpec = 0; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 1; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | B0006CC126B6B78700D82DEC /* Stack */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Stack; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | B0006CC426B6B78700D82DEC /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | B0006CBE26B6B78700D82DEC /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | B0006CB826B6B78700D82DEC = { 42 | isa = PBXGroup; 43 | children = ( 44 | B0006CC326B6B78700D82DEC /* Stack */, 45 | B0006CC226B6B78700D82DEC /* Products */, 46 | ); 47 | sourceTree = ""; 48 | }; 49 | B0006CC226B6B78700D82DEC /* Products */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | B0006CC126B6B78700D82DEC /* Stack */, 53 | ); 54 | name = Products; 55 | sourceTree = ""; 56 | }; 57 | B0006CC326B6B78700D82DEC /* Stack */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | B0006CC426B6B78700D82DEC /* main.swift */, 61 | ); 62 | path = Stack; 63 | sourceTree = ""; 64 | }; 65 | /* End PBXGroup section */ 66 | 67 | /* Begin PBXNativeTarget section */ 68 | B0006CC026B6B78700D82DEC /* Stack */ = { 69 | isa = PBXNativeTarget; 70 | buildConfigurationList = B0006CC826B6B78700D82DEC /* Build configuration list for PBXNativeTarget "Stack" */; 71 | buildPhases = ( 72 | B0006CBD26B6B78700D82DEC /* Sources */, 73 | B0006CBE26B6B78700D82DEC /* Frameworks */, 74 | B0006CBF26B6B78700D82DEC /* CopyFiles */, 75 | ); 76 | buildRules = ( 77 | ); 78 | dependencies = ( 79 | ); 80 | name = Stack; 81 | productName = Stack; 82 | productReference = B0006CC126B6B78700D82DEC /* Stack */; 83 | productType = "com.apple.product-type.tool"; 84 | }; 85 | /* End PBXNativeTarget section */ 86 | 87 | /* Begin PBXProject section */ 88 | B0006CB926B6B78700D82DEC /* Project object */ = { 89 | isa = PBXProject; 90 | attributes = { 91 | LastSwiftUpdateCheck = 1250; 92 | LastUpgradeCheck = 1250; 93 | TargetAttributes = { 94 | B0006CC026B6B78700D82DEC = { 95 | CreatedOnToolsVersion = 12.5.1; 96 | }; 97 | }; 98 | }; 99 | buildConfigurationList = B0006CBC26B6B78700D82DEC /* Build configuration list for PBXProject "Stack" */; 100 | compatibilityVersion = "Xcode 9.3"; 101 | developmentRegion = en; 102 | hasScannedForEncodings = 0; 103 | knownRegions = ( 104 | en, 105 | Base, 106 | ); 107 | mainGroup = B0006CB826B6B78700D82DEC; 108 | productRefGroup = B0006CC226B6B78700D82DEC /* Products */; 109 | projectDirPath = ""; 110 | projectRoot = ""; 111 | targets = ( 112 | B0006CC026B6B78700D82DEC /* Stack */, 113 | ); 114 | }; 115 | /* End PBXProject section */ 116 | 117 | /* Begin PBXSourcesBuildPhase section */ 118 | B0006CBD26B6B78700D82DEC /* Sources */ = { 119 | isa = PBXSourcesBuildPhase; 120 | buildActionMask = 2147483647; 121 | files = ( 122 | B0006CC526B6B78700D82DEC /* main.swift in Sources */, 123 | ); 124 | runOnlyForDeploymentPostprocessing = 0; 125 | }; 126 | /* End PBXSourcesBuildPhase section */ 127 | 128 | /* Begin XCBuildConfiguration section */ 129 | B0006CC626B6B78700D82DEC /* Debug */ = { 130 | isa = XCBuildConfiguration; 131 | buildSettings = { 132 | ALWAYS_SEARCH_USER_PATHS = NO; 133 | CLANG_ANALYZER_NONNULL = YES; 134 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 135 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 136 | CLANG_CXX_LIBRARY = "libc++"; 137 | CLANG_ENABLE_MODULES = YES; 138 | CLANG_ENABLE_OBJC_ARC = YES; 139 | CLANG_ENABLE_OBJC_WEAK = YES; 140 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 141 | CLANG_WARN_BOOL_CONVERSION = YES; 142 | CLANG_WARN_COMMA = YES; 143 | CLANG_WARN_CONSTANT_CONVERSION = YES; 144 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 145 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 146 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 147 | CLANG_WARN_EMPTY_BODY = YES; 148 | CLANG_WARN_ENUM_CONVERSION = YES; 149 | CLANG_WARN_INFINITE_RECURSION = YES; 150 | CLANG_WARN_INT_CONVERSION = YES; 151 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 152 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 153 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 154 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 155 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 156 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 157 | CLANG_WARN_STRICT_PROTOTYPES = YES; 158 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 159 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 160 | CLANG_WARN_UNREACHABLE_CODE = YES; 161 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 162 | COPY_PHASE_STRIP = NO; 163 | DEBUG_INFORMATION_FORMAT = dwarf; 164 | ENABLE_STRICT_OBJC_MSGSEND = YES; 165 | ENABLE_TESTABILITY = YES; 166 | GCC_C_LANGUAGE_STANDARD = gnu11; 167 | GCC_DYNAMIC_NO_PIC = NO; 168 | GCC_NO_COMMON_BLOCKS = YES; 169 | GCC_OPTIMIZATION_LEVEL = 0; 170 | GCC_PREPROCESSOR_DEFINITIONS = ( 171 | "DEBUG=1", 172 | "$(inherited)", 173 | ); 174 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 175 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 176 | GCC_WARN_UNDECLARED_SELECTOR = YES; 177 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 178 | GCC_WARN_UNUSED_FUNCTION = YES; 179 | GCC_WARN_UNUSED_VARIABLE = YES; 180 | MACOSX_DEPLOYMENT_TARGET = 11.3; 181 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 182 | MTL_FAST_MATH = YES; 183 | ONLY_ACTIVE_ARCH = YES; 184 | SDKROOT = macosx; 185 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 186 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 187 | }; 188 | name = Debug; 189 | }; 190 | B0006CC726B6B78700D82DEC /* Release */ = { 191 | isa = XCBuildConfiguration; 192 | buildSettings = { 193 | ALWAYS_SEARCH_USER_PATHS = NO; 194 | CLANG_ANALYZER_NONNULL = YES; 195 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 196 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 197 | CLANG_CXX_LIBRARY = "libc++"; 198 | CLANG_ENABLE_MODULES = YES; 199 | CLANG_ENABLE_OBJC_ARC = YES; 200 | CLANG_ENABLE_OBJC_WEAK = YES; 201 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 202 | CLANG_WARN_BOOL_CONVERSION = YES; 203 | CLANG_WARN_COMMA = YES; 204 | CLANG_WARN_CONSTANT_CONVERSION = YES; 205 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 206 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 207 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 208 | CLANG_WARN_EMPTY_BODY = YES; 209 | CLANG_WARN_ENUM_CONVERSION = YES; 210 | CLANG_WARN_INFINITE_RECURSION = YES; 211 | CLANG_WARN_INT_CONVERSION = YES; 212 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 213 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 214 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 215 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 216 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 217 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 218 | CLANG_WARN_STRICT_PROTOTYPES = YES; 219 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 220 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 221 | CLANG_WARN_UNREACHABLE_CODE = YES; 222 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 223 | COPY_PHASE_STRIP = NO; 224 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 225 | ENABLE_NS_ASSERTIONS = NO; 226 | ENABLE_STRICT_OBJC_MSGSEND = YES; 227 | GCC_C_LANGUAGE_STANDARD = gnu11; 228 | GCC_NO_COMMON_BLOCKS = YES; 229 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 230 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 231 | GCC_WARN_UNDECLARED_SELECTOR = YES; 232 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 233 | GCC_WARN_UNUSED_FUNCTION = YES; 234 | GCC_WARN_UNUSED_VARIABLE = YES; 235 | MACOSX_DEPLOYMENT_TARGET = 11.3; 236 | MTL_ENABLE_DEBUG_INFO = NO; 237 | MTL_FAST_MATH = YES; 238 | SDKROOT = macosx; 239 | SWIFT_COMPILATION_MODE = wholemodule; 240 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 241 | }; 242 | name = Release; 243 | }; 244 | B0006CC926B6B78700D82DEC /* Debug */ = { 245 | isa = XCBuildConfiguration; 246 | buildSettings = { 247 | CODE_SIGN_STYLE = Automatic; 248 | PRODUCT_NAME = "$(TARGET_NAME)"; 249 | SWIFT_VERSION = 5.0; 250 | }; 251 | name = Debug; 252 | }; 253 | B0006CCA26B6B78700D82DEC /* Release */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | CODE_SIGN_STYLE = Automatic; 257 | PRODUCT_NAME = "$(TARGET_NAME)"; 258 | SWIFT_VERSION = 5.0; 259 | }; 260 | name = Release; 261 | }; 262 | /* End XCBuildConfiguration section */ 263 | 264 | /* Begin XCConfigurationList section */ 265 | B0006CBC26B6B78700D82DEC /* Build configuration list for PBXProject "Stack" */ = { 266 | isa = XCConfigurationList; 267 | buildConfigurations = ( 268 | B0006CC626B6B78700D82DEC /* Debug */, 269 | B0006CC726B6B78700D82DEC /* Release */, 270 | ); 271 | defaultConfigurationIsVisible = 0; 272 | defaultConfigurationName = Release; 273 | }; 274 | B0006CC826B6B78700D82DEC /* Build configuration list for PBXNativeTarget "Stack" */ = { 275 | isa = XCConfigurationList; 276 | buildConfigurations = ( 277 | B0006CC926B6B78700D82DEC /* Debug */, 278 | B0006CCA26B6B78700D82DEC /* Release */, 279 | ); 280 | defaultConfigurationIsVisible = 0; 281 | defaultConfigurationName = Release; 282 | }; 283 | /* End XCConfigurationList section */ 284 | }; 285 | rootObject = B0006CB926B6B78700D82DEC /* Project object */; 286 | } 287 | -------------------------------------------------------------------------------- /Stack/Stack/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Stack 4 | // 5 | // Created by 전여훈 on 2021/08/01. 6 | // 7 | 8 | import Foundation 9 | 10 | struct Stack { 11 | var elements: [T] = [] 12 | 13 | var count : Int { 14 | return elements.count 15 | } 16 | var isEmpty : Bool { 17 | return elements.isEmpty 18 | } 19 | 20 | mutating func pop() -> T? { 21 | return elements.popLast() 22 | } 23 | mutating func push(_ element: T) { 24 | elements.append(element) 25 | } 26 | func top() -> T? { 27 | return elements.last 28 | } 29 | } 30 | --------------------------------------------------------------------------------