|
| 1 | +// |
| 2 | +// SwiftRedBlackTree.swift |
| 3 | +// SwiftRedBlackTree |
| 4 | +// |
| 5 | +// Copyright (c) 2017 David Kopec |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | + |
| 25 | +// This code was ported from Section 3.3 of Algorithms by Sedgewick & Wayne, 4th Edition |
| 26 | +// You can find their Java implementation here: |
| 27 | +// https://algs4.cs.princeton.edu/33balanced/RedBlackBST.java.html |
| 28 | + |
| 29 | +public struct RBTree<ValueType: Comparable> { |
| 30 | + // I've read in another Swift implementation of Red Black Trees that |
| 31 | + // unwrapping optionals led to serious performance issues, so I am avoiding |
| 32 | + // that by using a mini-class hierarchy |
| 33 | + private class Node { |
| 34 | + var isRed: Bool { |
| 35 | + if let temp = self as? Full { |
| 36 | + return temp.red |
| 37 | + } else { |
| 38 | + return false |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + private class Empty: Node {} |
| 43 | + private class Full: Node { |
| 44 | + var red: Bool // black is false, red is true |
| 45 | + let value: ValueType |
| 46 | + var left: Node |
| 47 | + var right: Node |
| 48 | + init(red: Bool = false, value: ValueType, left: Node = Empty(), right: Node = Empty()) { |
| 49 | + self.red = red |
| 50 | + self.value = value |
| 51 | + self.left = left |
| 52 | + self.right = left |
| 53 | + } |
| 54 | + |
| 55 | + // returns the new link for the parent |
| 56 | + fileprivate func rotateLeft() -> Full { |
| 57 | + guard let right = right as? Full else { return self } |
| 58 | + self.right = right.left |
| 59 | + right.left = self |
| 60 | + right.red = self.red |
| 61 | + self.red = true |
| 62 | + return right |
| 63 | + } |
| 64 | + |
| 65 | + // returns the new link for the parent |
| 66 | + fileprivate func rotateRight() -> Full { |
| 67 | + guard let left = left as? Full else { return self } |
| 68 | + self.left = left.right |
| 69 | + left.right = self |
| 70 | + left.red = self.red |
| 71 | + self.red = true |
| 72 | + return left |
| 73 | + } |
| 74 | + |
| 75 | + fileprivate func flipColors() { |
| 76 | + guard let left = left as? Full, let right = right as? Full else { return } |
| 77 | + self.red = true |
| 78 | + left.red = false |
| 79 | + right.red = false |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private var root: Node = Empty() |
| 84 | + |
| 85 | + private mutating func insertHelper(_ v: ValueType, _ current: Node) -> Full { |
| 86 | + guard let temp = current as? Full else { |
| 87 | + return Full(red: true, value: v) |
| 88 | + } // can't deal with empties |
| 89 | + var current = temp |
| 90 | + if v <= current.value { |
| 91 | + current.left = insertHelper(v, current.left) |
| 92 | + } else { |
| 93 | + current.right = insertHelper(v, current.right) |
| 94 | + } |
| 95 | + |
| 96 | + if !current.left.isRed && current.right.isRed { |
| 97 | + current = current.rotateLeft() |
| 98 | + } |
| 99 | + if let left = current.left as? Full, left.red && left.left.isRed { |
| 100 | + current = current.rotateRight() |
| 101 | + } |
| 102 | + if current.left.isRed && current.right.isRed { |
| 103 | + current.flipColors() |
| 104 | + } |
| 105 | + |
| 106 | + return current |
| 107 | + } |
| 108 | + |
| 109 | + public mutating func insert(_ value: ValueType) { |
| 110 | + root = insertHelper(value, root) |
| 111 | + if let rootEstablished = root as? Full { |
| 112 | + rootEstablished.red = false |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public func contains(_ value: ValueType) -> Bool { |
| 117 | + var current = root |
| 118 | + var height = 0 |
| 119 | + while let trial = current as? Full { |
| 120 | + height += 1 |
| 121 | + if value < trial.value { |
| 122 | + current = trial.left |
| 123 | + } else if value > trial.value { |
| 124 | + current = trial.right |
| 125 | + } else { |
| 126 | + print("height was \(height)") |
| 127 | + return true |
| 128 | + } |
| 129 | + } |
| 130 | + print("height was \(height)") |
| 131 | + return false |
| 132 | + } |
| 133 | +} |
0 commit comments