Skip to content

Commit f7ecb1a

Browse files
committed
Updated ReadMe for Swift3
1 parent ab02cf5 commit f7ecb1a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

Segment Tree/README.markdown

+14-14
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ The `leftBound` and `rightBound` of each node are marked in red.
7373
Here's how we create a node of the segment tree:
7474

7575
```swift
76-
public init(array: [T], leftBound: Int, rightBound: Int, function: (T, T) -> T) {
76+
public init(array: [T], leftBound: Int, rightBound: Int, function: @escaping (T, T) -> T) {
7777
self.leftBound = leftBound
7878
self.rightBound = rightBound
7979
self.function = function
@@ -111,7 +111,7 @@ We go through all this trouble so we can efficiently query the tree.
111111
Here's the code:
112112

113113
```swift
114-
public func queryWithLeftBound(leftBound: Int, rightBound: Int) -> T {
114+
public func query(withLeftBound: leftBound: Int, rightBound: Int) -> T {
115115
// 1
116116
if self.leftBound == leftBound && self.rightBound == rightBound {
117117
return self.value
@@ -122,16 +122,16 @@ Here's the code:
122122

123123
// 2
124124
if leftChild.rightBound < leftBound {
125-
return rightChild.queryWithLeftBound(leftBound, rightBound: rightBound)
125+
return rightChild.query(withLeftBound: leftBound, rightBound: rightBound)
126126

127127
// 3
128128
} else if rightChild.leftBound > rightBound {
129-
return leftChild.queryWithLeftBound(leftBound, rightBound: rightBound)
129+
return leftChild.query(withLeftBound: leftBound, rightBound: rightBound)
130130

131131
// 4
132132
} else {
133-
let leftResult = leftChild.queryWithLeftBound(leftBound, rightBound: leftChild.rightBound)
134-
let rightResult = rightChild.queryWithLeftBound(rightChild.leftBound, rightBound: rightBound)
133+
let leftResult = leftChild.query(withLeftBound: leftBound, rightBound: leftChild.rightBound)
134+
let rightResult = rightChild.query(withLeftBound: rightChild.leftBound, rightBound: rightBound)
135135
return function(leftResult, rightResult)
136136
}
137137
}
@@ -162,10 +162,10 @@ let array = [1, 2, 3, 4]
162162

163163
let sumSegmentTree = SegmentTree(array: array, function: +)
164164

165-
sumSegmentTree.queryWithLeftBound(0, rightBound: 3) // 1 + 2 + 3 + 4 = 10
166-
sumSegmentTree.queryWithLeftBound(1, rightBound: 2) // 2 + 3 = 5
167-
sumSegmentTree.queryWithLeftBound(0, rightBound: 0) // just 1
168-
sumSegmentTree.queryWithLeftBound(3, rightBound: 3) // just 4
165+
sumSegmentTree.query(withLeftBound: 0, rightBound: 3) // 1 + 2 + 3 + 4 = 10
166+
sumSegmentTree.query(withLeftBound: 1, rightBound: 2) // 2 + 3 = 5
167+
sumSegmentTree.query(withLeftBound: 0, rightBound: 0) // just 1
168+
sumSegmentTree.query(withLeftBound: 3, rightBound: 3) // just 4
169169
```
170170

171171
Querying the tree takes **O(log n)** time.
@@ -177,21 +177,21 @@ The value of a node in the segment tree depends on the nodes below it. So if we
177177
Here is the code:
178178

179179
```swift
180-
public func replaceItemAtIndex(index: Int, withItem item: T) {
180+
public func replaceItem(at index: Int, withItem item: T) {
181181
if leftBound == rightBound {
182182
value = item
183183
} else if let leftChild = leftChild, rightChild = rightChild {
184184
if leftChild.rightBound >= index {
185-
leftChild.replaceItemAtIndex(index, withItem: item)
185+
leftChild.replaceItem(at: index, withItem: item)
186186
} else {
187-
rightChild.replaceItemAtIndex(index, withItem: item)
187+
rightChild.replaceItem(at: index, withItem: item)
188188
}
189189
value = function(leftChild.value, rightChild.value)
190190
}
191191
}
192192
```
193193

194-
As usual, this works with recursion. If the node is a leaf, we just change its value. If the node is not a leaf, then we recursively call `replaceItemAtIndex()` to update its children. After that, we recalculate the node's own value so that it is up-to-date again.
194+
As usual, this works with recursion. If the node is a leaf, we just change its value. If the node is not a leaf, then we recursively call `replaceItem(at: )` to update its children. After that, we recalculate the node's own value so that it is up-to-date again.
195195

196196
Replacing an item takes **O(log n)** time.
197197

0 commit comments

Comments
 (0)