|
| 1 | +//: Playground - noun: a place where people can play |
| 2 | + |
| 3 | +public class SegmentTree<T> { |
| 4 | + |
| 5 | + private var value: T |
| 6 | + private var function: (T, T) -> T |
| 7 | + private var leftBound: Int |
| 8 | + private var rightBound: Int |
| 9 | + private var leftChild: SegmentTree<T>? |
| 10 | + private var rightChild: SegmentTree<T>? |
| 11 | + |
| 12 | + |
| 13 | + init(array: [T], leftBound: Int, rightBound: Int, function: (T, T) -> T) { |
| 14 | + self.leftBound = leftBound |
| 15 | + self.rightBound = rightBound |
| 16 | + self.function = function |
| 17 | + if leftBound == rightBound { |
| 18 | + value = array[leftBound] |
| 19 | + } else { |
| 20 | + let middle = (leftBound + rightBound) / 2 |
| 21 | + leftChild = SegmentTree<T>(array: array, leftBound: leftBound, rightBound: middle, function: function) |
| 22 | + rightChild = SegmentTree<T>(array: array, leftBound: middle+1, rightBound: rightBound, function: function) |
| 23 | + value = function(leftChild!.value, rightChild!.value) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + convenience init(array: [T], function: (T, T) -> T) { |
| 28 | + self.init(array: array, leftBound: 0, rightBound: array.count-1, function: function) |
| 29 | + } |
| 30 | + |
| 31 | + public func queryWithLeftBound(leftBound: Int, rightBound: Int) -> T { |
| 32 | + if self.leftBound == self.rightBound { |
| 33 | + return self.value |
| 34 | + } else if leftChild!.rightBound < leftBound { |
| 35 | + return rightChild!.queryWithLeftBound(leftBound, rightBound: rightBound) |
| 36 | + } else if rightChild!.leftBound > rightBound { |
| 37 | + return leftChild!.queryWithLeftBound(leftBound, rightBound: rightBound) |
| 38 | + } else { |
| 39 | + let leftResult = leftChild!.queryWithLeftBound(leftBound, rightBound: leftChild!.rightBound) |
| 40 | + let rightResult = rightChild!.queryWithLeftBound(rightChild!.leftBound, rightBound: rightBound) |
| 41 | + return function(leftResult, rightResult) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public func replaceItemAtIndex(index: Int, withItem item: T) { |
| 46 | + if leftBound == rightBound { |
| 47 | + value = item |
| 48 | + } else { |
| 49 | + if leftChild!.rightBound >= index { |
| 50 | + leftChild!.replaceItemAtIndex(index, withItem: item) |
| 51 | + } else { |
| 52 | + rightChild!.replaceItemAtIndex(index, withItem: item) |
| 53 | + } |
| 54 | + value = function(leftChild!.value, rightChild!.value) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +let array = [1, 2, 3] |
| 63 | + |
| 64 | +let sumSegmentTree = SegmentTree(array: array, function: +) |
| 65 | + |
| 66 | +print(sumSegmentTree.queryWithLeftBound(0, rightBound: 2)) // 1 + 2 + 3 + 4 = 10 |
| 67 | +print(sumSegmentTree.queryWithLeftBound(1, rightBound: 2)) // 2 + 3 = 5 |
| 68 | +print(sumSegmentTree.queryWithLeftBound(0, rightBound: 0)) // 1 = 1 |
| 69 | + |
| 70 | +sumSegmentTree.replaceItemAtIndex(0, withItem: 2) //our array now is [2, 2, 3, 4] |
| 71 | + |
| 72 | +print(sumSegmentTree.queryWithLeftBound(0, rightBound: 0)) // 2 = 2 |
| 73 | +print(sumSegmentTree.queryWithLeftBound(0, rightBound: 1)) // 2 + 2 = 4 |
| 74 | + |
| 75 | + |
| 76 | +//you can use any associative function (i.e (a+b)+c == a+(b+c)) as function for segment tree |
| 77 | +func gcd(m: Int, _ n: Int) -> Int { |
| 78 | + var a = 0 |
| 79 | + var b = max(m, n) |
| 80 | + var r = min(m, n) |
| 81 | + |
| 82 | + while r != 0 { |
| 83 | + a = b |
| 84 | + b = r |
| 85 | + r = a % b |
| 86 | + } |
| 87 | + return b |
| 88 | +} |
| 89 | + |
| 90 | +let gcdArray = [2, 4, 6, 3, 5] |
| 91 | + |
| 92 | +let gcdSegmentTree = SegmentTree(array: gcdArray, function: gcd) |
| 93 | + |
| 94 | +print(gcdSegmentTree.queryWithLeftBound(0, rightBound: 1)) // gcd(2, 4) = 2 |
| 95 | +print(gcdSegmentTree.queryWithLeftBound(2, rightBound: 3)) // gcd(6, 3) = 3 |
| 96 | +print(gcdSegmentTree.queryWithLeftBound(1, rightBound: 3)) // gcd(4, 6, 3) = 1 |
| 97 | +print(gcdSegmentTree.queryWithLeftBound(0, rightBound: 4)) // gcd(2, 4, 6, 3, 5) = 1 |
| 98 | + |
| 99 | +gcdSegmentTree.replaceItemAtIndex(3, withItem: 10) //gcdArray now is [2, 4, 6, 10, 5] |
| 100 | + |
| 101 | +print(gcdSegmentTree.queryWithLeftBound(3, rightBound: 4)) // gcd(10, 5) = 5 |
| 102 | + |
| 103 | + |
| 104 | +//example of segment tree which finds minimum on given range |
| 105 | +let minArray = [2, 4, 1, 5, 3] |
| 106 | + |
| 107 | +let minSegmentTree = SegmentTree(array: minArray, function: min) |
| 108 | + |
| 109 | +print(minSegmentTree.queryWithLeftBound(0, rightBound: 4)) // min(2, 4, 1, 5, 3) = 1 |
| 110 | +print(minSegmentTree.queryWithLeftBound(0, rightBound: 1)) // min(2, 4) = 2 |
| 111 | + |
| 112 | +minSegmentTree.replaceItemAtIndex(2, withItem: 10) // minArray now is [2, 4, 10, 5, 3] |
| 113 | + |
| 114 | +print(minSegmentTree.queryWithLeftBound(0, rightBound: 4)) // min(2, 4, 10, 5, 3) = 2 |
| 115 | + |
| 116 | + |
| 117 | +//type of elements in array can be any type which has some associative function |
| 118 | +let stringArray = ["a", "b", "c", "A", "B", "C"] |
| 119 | + |
| 120 | +let stringSegmentTree = SegmentTree(array: stringArray, function: +) |
| 121 | + |
| 122 | +print(stringSegmentTree.queryWithLeftBound(0, rightBound: 1)) // "a"+"b" = "ab" |
| 123 | +print(stringSegmentTree.queryWithLeftBound(2, rightBound: 3)) // "c"+"A" = "cA" |
| 124 | +print(stringSegmentTree.queryWithLeftBound(1, rightBound: 3)) // "b"+"c"+"A" = "bcA" |
| 125 | +print(stringSegmentTree.queryWithLeftBound(0, rightBound: 5)) // "a"+"b"+"c"+"A"+"B"+"C" = "abcABC" |
| 126 | + |
| 127 | +stringSegmentTree.replaceItemAtIndex(0, withItem: "I") |
| 128 | +stringSegmentTree.replaceItemAtIndex(1, withItem: " like") |
| 129 | +stringSegmentTree.replaceItemAtIndex(2, withItem: " algorithms") |
| 130 | +stringSegmentTree.replaceItemAtIndex(3, withItem: " and") |
| 131 | +stringSegmentTree.replaceItemAtIndex(4, withItem: " swift") |
| 132 | +stringSegmentTree.replaceItemAtIndex(5, withItem: "!") |
| 133 | + |
| 134 | +print(stringSegmentTree.queryWithLeftBound(0, rightBound: 5)) |
| 135 | + |
0 commit comments