@@ -73,7 +73,7 @@ The `leftBound` and `rightBound` of each node are marked in red.
73
73
Here's how we create a node of the segment tree:
74
74
75
75
``` 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) {
77
77
self .leftBound = leftBound
78
78
self .rightBound = rightBound
79
79
self .function = function
@@ -111,7 +111,7 @@ We go through all this trouble so we can efficiently query the tree.
111
111
Here's the code:
112
112
113
113
``` swift
114
- public func queryWithLeftBound ( leftBound : Int , rightBound : Int ) -> T {
114
+ public func query ( withLeftBound : leftBound: Int , rightBound : Int ) -> T {
115
115
// 1
116
116
if self .leftBound == leftBound && self .rightBound == rightBound {
117
117
return self .value
@@ -122,16 +122,16 @@ Here's the code:
122
122
123
123
// 2
124
124
if leftChild.rightBound < leftBound {
125
- return rightChild.queryWithLeftBound ( leftBound, rightBound : rightBound)
125
+ return rightChild.query ( withLeftBound : leftBound, rightBound : rightBound)
126
126
127
127
// 3
128
128
} else if rightChild.leftBound > rightBound {
129
- return leftChild.queryWithLeftBound ( leftBound, rightBound : rightBound)
129
+ return leftChild.query ( withLeftBound : leftBound, rightBound : rightBound)
130
130
131
131
// 4
132
132
} 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)
135
135
return function (leftResult, rightResult)
136
136
}
137
137
}
@@ -162,10 +162,10 @@ let array = [1, 2, 3, 4]
162
162
163
163
let sumSegmentTree = SegmentTree (array : array, function : + )
164
164
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
169
169
```
170
170
171
171
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
177
177
Here is the code:
178
178
179
179
``` swift
180
- public func replaceItemAtIndex ( index : Int , withItem item : T) {
180
+ public func replaceItem ( at index : Int , withItem item : T) {
181
181
if leftBound == rightBound {
182
182
value = item
183
183
} else if let leftChild = leftChild, rightChild = rightChild {
184
184
if leftChild.rightBound >= index {
185
- leftChild.replaceItemAtIndex ( index, withItem : item)
185
+ leftChild.replaceItem ( at : index, withItem : item)
186
186
} else {
187
- rightChild.replaceItemAtIndex ( index, withItem : item)
187
+ rightChild.replaceItem ( at : index, withItem : item)
188
188
}
189
189
value = function (leftChild.value , rightChild.value )
190
190
}
191
191
}
192
192
```
193
193
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.
195
195
196
196
Replacing an item takes ** O(log n)** time.
197
197
0 commit comments