@@ -28,7 +28,7 @@ public class SegmentTree<T> {
2828 self . init ( array: array, leftBound: 0 , rightBound: array. count- 1 , function: function)
2929 }
3030
31- public func query( withLeftBound : Int , rightBound: Int ) -> T {
31+ public func query( leftBound : Int , rightBound: Int ) -> T {
3232 if self . leftBound == leftBound && self . rightBound == rightBound {
3333 return self . value
3434 }
@@ -37,12 +37,12 @@ public class SegmentTree<T> {
3737 guard let rightChild = rightChild else { fatalError ( " rightChild should not be nil " ) }
3838
3939 if leftChild. rightBound < leftBound {
40- return rightChild. query ( withLeftBound : leftBound, rightBound: rightBound)
40+ return rightChild. query ( leftBound : leftBound, rightBound: rightBound)
4141 } else if rightChild. leftBound > rightBound {
42- return leftChild. query ( withLeftBound : leftBound, rightBound: rightBound)
42+ return leftChild. query ( leftBound : leftBound, rightBound: rightBound)
4343 } 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)
4646 return function ( leftResult, rightResult)
4747 }
4848 }
@@ -68,14 +68,14 @@ let array = [1, 2, 3, 4]
6868
6969let sumSegmentTree = SegmentTree ( array: array, function: + )
7070
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
7474
7575sumSegmentTree. replaceItem ( at: 0 , withItem: 2 ) //our array now is [2, 2, 3, 4]
7676
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
7979
8080
8181//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]
9696
9797let gcdSegmentTree = SegmentTree ( array: gcdArray, function: gcd)
9898
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
103103
104104gcdSegmentTree. replaceItem ( at: 3 , withItem: 10 ) //gcdArray now is [2, 4, 6, 10, 5]
105105
106- print ( gcdSegmentTree. query ( withLeftBound : 3 , rightBound: 4 ) ) // gcd(10, 5) = 5
106+ print ( gcdSegmentTree. query ( leftBound : 3 , rightBound: 4 ) ) // gcd(10, 5) = 5
107107
108108
109109//example of segment tree which finds minimum on given range
110110let minArray = [ 2 , 4 , 1 , 5 , 3 ]
111111
112112let minSegmentTree = SegmentTree ( array: minArray, function: min)
113113
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
116116
117117minSegmentTree. replaceItem ( at: 2 , withItem: 10 ) // minArray now is [2, 4, 10, 5, 3]
118118
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
120120
121121
122122//type of elements in array can be any type which has some associative function
123123let stringArray = [ " a " , " b " , " c " , " A " , " B " , " C " ]
124124
125125let stringSegmentTree = SegmentTree ( array: stringArray, function: + )
126126
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"
131131
132132stringSegmentTree. replaceItem ( at: 0 , withItem: " I " )
133133stringSegmentTree. replaceItem ( at: 1 , withItem: " like " )
@@ -136,4 +136,4 @@ stringSegmentTree.replaceItem(at: 3, withItem: " and")
136136stringSegmentTree. replaceItem ( at: 4 , withItem: " swift " )
137137stringSegmentTree. replaceItem ( at: 5 , withItem: " ! " )
138138
139- print ( stringSegmentTree. query ( withLeftBound : 0 , rightBound: 5 ) )
139+ print ( stringSegmentTree. query ( leftBound : 0 , rightBound: 5 ) )
0 commit comments