Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Updated ReadMe for Swift3
  • Loading branch information
farzadshbfn committed Sep 18, 2016
commit f7ecb1ae4a886a8983670ce77e5bed3e6cde0890
28 changes: 14 additions & 14 deletions Segment Tree/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The `leftBound` and `rightBound` of each node are marked in red.
Here's how we create a node of the segment tree:

```swift
public init(array: [T], leftBound: Int, rightBound: Int, function: (T, T) -> T) {
public init(array: [T], leftBound: Int, rightBound: Int, function: @escaping (T, T) -> T) {
self.leftBound = leftBound
self.rightBound = rightBound
self.function = function
Expand Down Expand Up @@ -111,7 +111,7 @@ We go through all this trouble so we can efficiently query the tree.
Here's the code:

```swift
public func queryWithLeftBound(leftBound: Int, rightBound: Int) -> T {
public func query(withLeftBound: leftBound: Int, rightBound: Int) -> T {
// 1
if self.leftBound == leftBound && self.rightBound == rightBound {
return self.value
Expand All @@ -122,16 +122,16 @@ Here's the code:

// 2
if leftChild.rightBound < leftBound {
return rightChild.queryWithLeftBound(leftBound, rightBound: rightBound)
return rightChild.query(withLeftBound: leftBound, rightBound: rightBound)

// 3
} else if rightChild.leftBound > rightBound {
return leftChild.queryWithLeftBound(leftBound, rightBound: rightBound)
return leftChild.query(withLeftBound: leftBound, rightBound: rightBound)

// 4
} else {
let leftResult = leftChild.queryWithLeftBound(leftBound, rightBound: leftChild.rightBound)
let rightResult = rightChild.queryWithLeftBound(rightChild.leftBound, rightBound: rightBound)
let leftResult = leftChild.query(withLeftBound: leftBound, rightBound: leftChild.rightBound)
let rightResult = rightChild.query(withLeftBound: rightChild.leftBound, rightBound: rightBound)
return function(leftResult, rightResult)
}
}
Expand Down Expand Up @@ -162,10 +162,10 @@ let array = [1, 2, 3, 4]

let sumSegmentTree = SegmentTree(array: array, function: +)

sumSegmentTree.queryWithLeftBound(0, rightBound: 3) // 1 + 2 + 3 + 4 = 10
sumSegmentTree.queryWithLeftBound(1, rightBound: 2) // 2 + 3 = 5
sumSegmentTree.queryWithLeftBound(0, rightBound: 0) // just 1
sumSegmentTree.queryWithLeftBound(3, rightBound: 3) // just 4
sumSegmentTree.query(withLeftBound: 0, rightBound: 3) // 1 + 2 + 3 + 4 = 10
sumSegmentTree.query(withLeftBound: 1, rightBound: 2) // 2 + 3 = 5
sumSegmentTree.query(withLeftBound: 0, rightBound: 0) // just 1
sumSegmentTree.query(withLeftBound: 3, rightBound: 3) // just 4
```

Querying the tree takes **O(log n)** time.
Expand All @@ -177,21 +177,21 @@ The value of a node in the segment tree depends on the nodes below it. So if we
Here is the code:

```swift
public func replaceItemAtIndex(index: Int, withItem item: T) {
public func replaceItem(at index: Int, withItem item: T) {
if leftBound == rightBound {
value = item
} else if let leftChild = leftChild, rightChild = rightChild {
if leftChild.rightBound >= index {
leftChild.replaceItemAtIndex(index, withItem: item)
leftChild.replaceItem(at: index, withItem: item)
} else {
rightChild.replaceItemAtIndex(index, withItem: item)
rightChild.replaceItem(at: index, withItem: item)
}
value = function(leftChild.value, rightChild.value)
}
}
```

As usual, this works with recursion. If the node is a leaf, we just change its value. If the node is not a leaf, then we recursively call `replaceItemAtIndex()` to update its children. After that, we recalculate the node's own value so that it is up-to-date again.
As usual, this works with recursion. If the node is a leaf, we just change its value. If the node is not a leaf, then we recursively call `replaceItem(at: )` to update its children. After that, we recalculate the node's own value so that it is up-to-date again.

Replacing an item takes **O(log n)** time.

Expand Down