Skip to content

Commit ee38a5a

Browse files
author
Vladimir Zamyatin
committed
Fix a bug in SegmentTree
1 parent 6cc7c2f commit ee38a5a

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

Segment Tree/SegmentTree.playground/Contents.swift

+23-23
Original file line numberDiff line numberDiff line change
@@ -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

6969
let 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

7575
sumSegmentTree.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

9797
let 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

104104
gcdSegmentTree.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
110110
let minArray = [2, 4, 1, 5, 3]
111111

112112
let 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

117117
minSegmentTree.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
123123
let stringArray = ["a", "b", "c", "A", "B", "C"]
124124

125125
let 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

132132
stringSegmentTree.replaceItem(at: 0, withItem: "I")
133133
stringSegmentTree.replaceItem(at: 1, withItem: " like")
@@ -136,4 +136,4 @@ stringSegmentTree.replaceItem(at: 3, withItem: " and")
136136
stringSegmentTree.replaceItem(at: 4, withItem: " swift")
137137
stringSegmentTree.replaceItem(at: 5, withItem: "!")
138138

139-
print(stringSegmentTree.query(withLeftBound: 0, rightBound: 5))
139+
print(stringSegmentTree.query(leftBound: 0, rightBound: 5))

Segment Tree/SegmentTree.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SegmentTree<T> {
3535
self.init(array: array, leftBound: 0, rightBound: array.count-1, function: function)
3636
}
3737

38-
public func query(withLeftBound: Int, rightBound: Int) -> T {
38+
public func query(leftBound: Int, rightBound: Int) -> T {
3939
if self.leftBound == leftBound && self.rightBound == rightBound {
4040
return self.value
4141
}
@@ -44,12 +44,12 @@ public class SegmentTree<T> {
4444
guard let rightChild = rightChild else { fatalError("rightChild should not be nil") }
4545

4646
if leftChild.rightBound < leftBound {
47-
return rightChild.query(withLeftBound: leftBound, rightBound: rightBound)
47+
return rightChild.query(leftBound: leftBound, rightBound: rightBound)
4848
} else if rightChild.leftBound > rightBound {
49-
return leftChild.query(withLeftBound: leftBound, rightBound: rightBound)
49+
return leftChild.query(leftBound: leftBound, rightBound: rightBound)
5050
} else {
51-
let leftResult = leftChild.query(withLeftBound: leftBound, rightBound: leftChild.rightBound)
52-
let rightResult = rightChild.query(withLeftBound:rightChild.leftBound, rightBound: rightBound)
51+
let leftResult = leftChild.query(leftBound: leftBound, rightBound: leftChild.rightBound)
52+
let rightResult = rightChild.query(leftBound:rightChild.leftBound, rightBound: rightBound)
5353
return function(leftResult, rightResult)
5454
}
5555
}

0 commit comments

Comments
 (0)