Skip to content

Commit 7ecbb71

Browse files
authored
prettify some syntax around optionals
1 parent 7573a0c commit 7ecbb71

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

AVL Tree/AVLTree.playground/Sources/AVLTree.swift

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,11 @@ open class AVLTree<Key: Comparable, Payload> {
9999

100100
extension TreeNode {
101101
public func minimum() -> TreeNode? {
102-
if let leftChild = self.leftChild {
103-
return leftChild.minimum()
104-
}
105-
return self
102+
return leftChild?.minimum() ?? self
106103
}
107104

108105
public func maximum() -> TreeNode? {
109-
if let rightChild = self.rightChild {
110-
return rightChild.maximum()
111-
}
112-
return self
106+
return rightChild?.maximum() ?? self
113107
}
114108
}
115109

@@ -120,11 +114,7 @@ extension AVLTree {
120114
}
121115

122116
public func search(input: Key) -> Payload? {
123-
if let result = search(key: input, node: root) {
124-
return result.payload
125-
} else {
126-
return nil
127-
}
117+
return search(key: input, node: root)?.payload
128118
}
129119

130120
fileprivate func search(key: Key, node: Node?) -> Node? {
@@ -385,11 +375,7 @@ extension TreeNode: CustomDebugStringConvertible {
385375

386376
extension AVLTree: CustomDebugStringConvertible {
387377
public var debugDescription: String {
388-
if let root = root {
389-
return root.debugDescription
390-
} else {
391-
return "[]"
392-
}
378+
return root?.debugDescription ?? "[]"
393379
}
394380
}
395381

@@ -409,10 +395,6 @@ extension TreeNode: CustomStringConvertible {
409395

410396
extension AVLTree: CustomStringConvertible {
411397
public var description: String {
412-
if let root = root {
413-
return root.description
414-
} else {
415-
return "[]"
416-
}
398+
return root?.description ?? "[]"
417399
}
418400
}

0 commit comments

Comments
 (0)