Skip to content

Commit 6600a1b

Browse files
committed
initial rough port
0 parents  commit 6600a1b

File tree

12 files changed

+831
-0
lines changed

12 files changed

+831
-0
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.DS_Store
2+
.Trashes
3+
*.swp
4+
# Xcode
5+
#
6+
build/
7+
*.pbxuser
8+
!default.pbxuser
9+
*.mode1v3
10+
!default.mode1v3
11+
*.mode2v3
12+
!default.mode2v3
13+
*.perspectivev3
14+
!default.perspectivev3
15+
xcuserdata
16+
*.xccheckout
17+
*.moved-aside
18+
DerivedData
19+
*.hmap
20+
*.ipa
21+
*.xcuserstate
22+
23+
# CocoaPods
24+
#
25+
# We recommend against adding the Pods directory to your .gitignore. However
26+
# you should judge for yourself, the pros and cons are mentioned at:
27+
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
28+
#
29+
# Pods/

Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SwiftRedBlackTree",
8+
products: [
9+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.library(
11+
name: "SwiftRedBlackTree",
12+
targets: ["SwiftRedBlackTree"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
21+
.target(
22+
name: "SwiftRedBlackTree",
23+
dependencies: []),
24+
.testTarget(
25+
name: "SwiftRedBlackTreeTests",
26+
dependencies: ["SwiftRedBlackTree"]),
27+
]
28+
)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SwiftRedBlackTree
2+
3+
A description of this package.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plist version="1.0">
3+
<dict>
4+
<key>CFBundleDevelopmentRegion</key>
5+
<string>en</string>
6+
<key>CFBundleExecutable</key>
7+
<string>$(EXECUTABLE_NAME)</string>
8+
<key>CFBundleIdentifier</key>
9+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
10+
<key>CFBundleInfoDictionaryVersion</key>
11+
<string>6.0</string>
12+
<key>CFBundleName</key>
13+
<string>$(PRODUCT_NAME)</string>
14+
<key>CFBundlePackageType</key>
15+
<string>BNDL</string>
16+
<key>CFBundleShortVersionString</key>
17+
<string>1.0</string>
18+
<key>CFBundleSignature</key>
19+
<string>????</string>
20+
<key>CFBundleVersion</key>
21+
<string>$(CURRENT_PROJECT_VERSION)</string>
22+
<key>NSPrincipalClass</key>
23+
<string></string>
24+
</dict>
25+
</plist>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plist version="1.0">
3+
<dict>
4+
<key>CFBundleDevelopmentRegion</key>
5+
<string>en</string>
6+
<key>CFBundleExecutable</key>
7+
<string>$(EXECUTABLE_NAME)</string>
8+
<key>CFBundleIdentifier</key>
9+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
10+
<key>CFBundleInfoDictionaryVersion</key>
11+
<string>6.0</string>
12+
<key>CFBundleName</key>
13+
<string>$(PRODUCT_NAME)</string>
14+
<key>CFBundlePackageType</key>
15+
<string>FMWK</string>
16+
<key>CFBundleShortVersionString</key>
17+
<string>1.0</string>
18+
<key>CFBundleSignature</key>
19+
<string>????</string>
20+
<key>CFBundleVersion</key>
21+
<string>$(CURRENT_PROJECT_VERSION)</string>
22+
<key>NSPrincipalClass</key>
23+
<string></string>
24+
</dict>
25+
</plist>

0 commit comments

Comments
 (0)