You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.markdown
+1-1
Original file line number
Diff line number
Diff line change
@@ -132,7 +132,7 @@ Most of the time using just the built-in `Array`, `Dictionary`, and `Set` types
132
132
- Red-Black Tree
133
133
- Splay Tree
134
134
- Threaded Binary Tree
135
-
-[Segment Tree](Segment Tree/). Stores intervals and can answer which intervals (or segments) contain a given point.
135
+
-[Segment Tree](Segment Tree/). Can compute f(a[l], a[l+1], ..., a[r]) for l ≤ r (f is some associative function) and update a[x] = v both on **O(log n)** time
136
136
- kd-Tree
137
137
-[Heap](Heap/). A binary tree stored in an array, so it doesn't use pointers. Makes a great priority queue.
If out current leftBound and rightBound are equal it means that we are in leaf so we don't have any child nodes and we just fill in `value` property with `array[leftBound]` else we have two child nodes. In that case we divide our current segment into two equal (if length is even) segments: `middle = (leftBound + rightBound) / 2`**[leftBound, middle]** and **[middle+1, rightBound]** and then we build our child nodes for that segments. After we build our child nodes we can easily calculate our value as `value = function(leftChild!.value, rightChild!.value)` because **f(leftBound, rightBound) = f(f(leftBound, middle), f(middle+1, rightBound))**
57
+
If out current leftBound and rightBound are equal it means that we are in leaf so we don't have any child nodes and we just fill in `value` property with `array[leftBound]` else we have two child nodes. In that case we divide our current segment into two equal (if length is even) segments: `middle = (leftBound + rightBound) / 2`**[leftBound, middle]** and **[middle+1, rightBound]** and then we build our child nodes for that segments. After we build our child nodes so we can easily calculate our value as `value = function(leftChild!.value, rightChild!.value)` because **f(leftBound, rightBound) = f(f(leftBound, middle), f(middle+1, rightBound))**
58
58
59
59
## Getting answer to query
60
60
61
61
```swift
62
62
publicfuncqueryWithLeftBound(leftBound: Int, rightBound: Int) -> T {
@@ -73,8 +73,36 @@ public func queryWithLeftBound(leftBound: Int, rightBound: Int) -> T {
73
73
}
74
74
}
75
75
```
76
-
Firstly, we check if our current node is leaf, if it is we just return its value else
77
-
we check that our query segment fully lies in rightChild, if so we return result of query on rightChild else if segment lies in leftChild we return result of query on leftChild. If our query lies in both child then we combine results of query on both child.
76
+
Firstly, we check if current query segment is equal to segment for which our current node responsible, if it is we just return its value.
77
+
```swift
78
+
returnself.value
79
+
```
80
+
81
+

82
+
83
+
Else we check that our query segment fully lies in rightChild, if so we return result of query on rightChild
As always we check if current node is leaf and if so we just change its value otherwise we need to find out to which child index belongs. After that we call same function on that child and after that we recalculate our value because it needs to be in actual state.
123
+
Foremost, we check if current node is leaf and if so we just change its value otherwise we need to find out to which child index belongs and call same function on that child. After that we recalculate our value because it needs to be in actual state.
96
124
97
125
## Examples
98
126
99
127
Examples of using segment trees can be found in playground
0 commit comments