Skip to content

Commit 54156c7

Browse files
authored
Merge pull request kodecocodes#360 from boqian2000/master
Simplify the insert method
2 parents b580a4a + ca35d57 commit 54156c7

File tree

4 files changed

+9
-393
lines changed

4 files changed

+9
-393
lines changed

Binary Search Tree/Solution 1/BinarySearchTree.playground/Sources/BinarySearchTree.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class BinarySearchTree<T: Comparable> {
2323
precondition(array.count > 0)
2424
self.init(value: array.first!)
2525
for v in array.dropFirst() {
26-
insert(value: v, parent: self)
26+
insert(value: v)
2727
}
2828
}
2929

@@ -74,23 +74,19 @@ extension BinarySearchTree {
7474
Performance: runs in O(h) time, where h is the height of the tree.
7575
*/
7676
public func insert(value: T) {
77-
insert(value: value, parent: self)
78-
}
79-
80-
fileprivate func insert(value: T, parent: BinarySearchTree) {
8177
if value < self.value {
8278
if let left = left {
83-
left.insert(value: value, parent: left)
79+
left.insert(value: value)
8480
} else {
8581
left = BinarySearchTree(value: value)
86-
left?.parent = parent
82+
left?.parent = self
8783
}
8884
} else {
8985
if let right = right {
90-
right.insert(value: value, parent: right)
86+
right.insert(value: value)
9187
} else {
9288
right = BinarySearchTree(value: value)
93-
right?.parent = parent
89+
right?.parent = self
9490
}
9591
}
9692
}

0 commit comments

Comments
 (0)