@@ -28,7 +28,7 @@ public class SegmentTree<T> {
28
28
self . init ( array: array, leftBound: 0 , rightBound: array. count- 1 , function: function)
29
29
}
30
30
31
- public func query( withLeftBound : Int , rightBound: Int ) -> T {
31
+ public func query( leftBound : Int , rightBound: Int ) -> T {
32
32
if self . leftBound == leftBound && self . rightBound == rightBound {
33
33
return self . value
34
34
}
@@ -37,12 +37,12 @@ public class SegmentTree<T> {
37
37
guard let rightChild = rightChild else { fatalError ( " rightChild should not be nil " ) }
38
38
39
39
if leftChild. rightBound < leftBound {
40
- return rightChild. query ( withLeftBound : leftBound, rightBound: rightBound)
40
+ return rightChild. query ( leftBound : leftBound, rightBound: rightBound)
41
41
} else if rightChild. leftBound > rightBound {
42
- return leftChild. query ( withLeftBound : leftBound, rightBound: rightBound)
42
+ return leftChild. query ( leftBound : leftBound, rightBound: rightBound)
43
43
} else {
44
- let leftResult = leftChild. query ( withLeftBound : leftBound, rightBound: leftChild. rightBound)
45
- let rightResult = rightChild. query ( withLeftBound : rightChild. leftBound, rightBound: rightBound)
44
+ let leftResult = leftChild. query ( leftBound : leftBound, rightBound: leftChild. rightBound)
45
+ let rightResult = rightChild. query ( leftBound : rightChild. leftBound, rightBound: rightBound)
46
46
return function ( leftResult, rightResult)
47
47
}
48
48
}
@@ -68,14 +68,14 @@ let array = [1, 2, 3, 4]
68
68
69
69
let sumSegmentTree = SegmentTree ( array: array, function: + )
70
70
71
- print ( sumSegmentTree. query ( withLeftBound : 0 , rightBound: 3 ) ) // 1 + 2 + 3 + 4 = 10
72
- print ( sumSegmentTree. query ( withLeftBound : 1 , rightBound: 2 ) ) // 2 + 3 = 5
73
- print ( sumSegmentTree. query ( withLeftBound : 0 , rightBound: 0 ) ) // 1 = 1
71
+ print ( sumSegmentTree. query ( leftBound : 0 , rightBound: 3 ) ) // 1 + 2 + 3 + 4 = 10
72
+ print ( sumSegmentTree. query ( leftBound : 1 , rightBound: 2 ) ) // 2 + 3 = 5
73
+ print ( sumSegmentTree. query ( leftBound : 0 , rightBound: 0 ) ) // 1 = 1
74
74
75
75
sumSegmentTree. replaceItem ( at: 0 , withItem: 2 ) //our array now is [2, 2, 3, 4]
76
76
77
- print ( sumSegmentTree. query ( withLeftBound : 0 , rightBound: 0 ) ) // 2 = 2
78
- print ( sumSegmentTree. query ( withLeftBound : 0 , rightBound: 1 ) ) // 2 + 2 = 4
77
+ print ( sumSegmentTree. query ( leftBound : 0 , rightBound: 0 ) ) // 2 = 2
78
+ print ( sumSegmentTree. query ( leftBound : 0 , rightBound: 1 ) ) // 2 + 2 = 4
79
79
80
80
81
81
//you can use any associative function (i.e (a+b)+c == a+(b+c)) as function for segment tree
@@ -96,38 +96,38 @@ let gcdArray = [2, 4, 6, 3, 5]
96
96
97
97
let gcdSegmentTree = SegmentTree ( array: gcdArray, function: gcd)
98
98
99
- print ( gcdSegmentTree. query ( withLeftBound : 0 , rightBound: 1 ) ) // gcd(2, 4) = 2
100
- print ( gcdSegmentTree. query ( withLeftBound : 2 , rightBound: 3 ) ) // gcd(6, 3) = 3
101
- print ( gcdSegmentTree. query ( withLeftBound : 1 , rightBound: 3 ) ) // gcd(4, 6, 3) = 1
102
- print ( gcdSegmentTree. query ( withLeftBound : 0 , rightBound: 4 ) ) // gcd(2, 4, 6, 3, 5) = 1
99
+ print ( gcdSegmentTree. query ( leftBound : 0 , rightBound: 1 ) ) // gcd(2, 4) = 2
100
+ print ( gcdSegmentTree. query ( leftBound : 2 , rightBound: 3 ) ) // gcd(6, 3) = 3
101
+ print ( gcdSegmentTree. query ( leftBound : 1 , rightBound: 3 ) ) // gcd(4, 6, 3) = 1
102
+ print ( gcdSegmentTree. query ( leftBound : 0 , rightBound: 4 ) ) // gcd(2, 4, 6, 3, 5) = 1
103
103
104
104
gcdSegmentTree. replaceItem ( at: 3 , withItem: 10 ) //gcdArray now is [2, 4, 6, 10, 5]
105
105
106
- print ( gcdSegmentTree. query ( withLeftBound : 3 , rightBound: 4 ) ) // gcd(10, 5) = 5
106
+ print ( gcdSegmentTree. query ( leftBound : 3 , rightBound: 4 ) ) // gcd(10, 5) = 5
107
107
108
108
109
109
//example of segment tree which finds minimum on given range
110
110
let minArray = [ 2 , 4 , 1 , 5 , 3 ]
111
111
112
112
let minSegmentTree = SegmentTree ( array: minArray, function: min)
113
113
114
- print ( minSegmentTree. query ( withLeftBound : 0 , rightBound: 4 ) ) // min(2, 4, 1, 5, 3) = 1
115
- print ( minSegmentTree. query ( withLeftBound : 0 , rightBound: 1 ) ) // min(2, 4) = 2
114
+ print ( minSegmentTree. query ( leftBound : 0 , rightBound: 4 ) ) // min(2, 4, 1, 5, 3) = 1
115
+ print ( minSegmentTree. query ( leftBound : 0 , rightBound: 1 ) ) // min(2, 4) = 2
116
116
117
117
minSegmentTree. replaceItem ( at: 2 , withItem: 10 ) // minArray now is [2, 4, 10, 5, 3]
118
118
119
- print ( minSegmentTree. query ( withLeftBound : 0 , rightBound: 4 ) ) // min(2, 4, 10, 5, 3) = 2
119
+ print ( minSegmentTree. query ( leftBound : 0 , rightBound: 4 ) ) // min(2, 4, 10, 5, 3) = 2
120
120
121
121
122
122
//type of elements in array can be any type which has some associative function
123
123
let stringArray = [ " a " , " b " , " c " , " A " , " B " , " C " ]
124
124
125
125
let stringSegmentTree = SegmentTree ( array: stringArray, function: + )
126
126
127
- print ( stringSegmentTree. query ( withLeftBound : 0 , rightBound: 1 ) ) // "a"+"b" = "ab"
128
- print ( stringSegmentTree. query ( withLeftBound : 2 , rightBound: 3 ) ) // "c"+"A" = "cA"
129
- print ( stringSegmentTree. query ( withLeftBound : 1 , rightBound: 3 ) ) // "b"+"c"+"A" = "bcA"
130
- print ( stringSegmentTree. query ( withLeftBound : 0 , rightBound: 5 ) ) // "a"+"b"+"c"+"A"+"B"+"C" = "abcABC"
127
+ print ( stringSegmentTree. query ( leftBound : 0 , rightBound: 1 ) ) // "a"+"b" = "ab"
128
+ print ( stringSegmentTree. query ( leftBound : 2 , rightBound: 3 ) ) // "c"+"A" = "cA"
129
+ print ( stringSegmentTree. query ( leftBound : 1 , rightBound: 3 ) ) // "b"+"c"+"A" = "bcA"
130
+ print ( stringSegmentTree. query ( leftBound : 0 , rightBound: 5 ) ) // "a"+"b"+"c"+"A"+"B"+"C" = "abcABC"
131
131
132
132
stringSegmentTree. replaceItem ( at: 0 , withItem: " I " )
133
133
stringSegmentTree. replaceItem ( at: 1 , withItem: " like " )
@@ -136,4 +136,4 @@ stringSegmentTree.replaceItem(at: 3, withItem: " and")
136
136
stringSegmentTree. replaceItem ( at: 4 , withItem: " swift " )
137
137
stringSegmentTree. replaceItem ( at: 5 , withItem: " ! " )
138
138
139
- print ( stringSegmentTree. query ( withLeftBound : 0 , rightBound: 5 ) )
139
+ print ( stringSegmentTree. query ( leftBound : 0 , rightBound: 5 ) )
0 commit comments