Skip to content

Commit 6eb7d9a

Browse files
committed
old version with error
1 parent ca8e7ff commit 6eb7d9a

File tree

6 files changed

+519
-511
lines changed

6 files changed

+519
-511
lines changed

Threaded Binary Tree/README.markdown

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,31 @@ outlined above. We use these predecessor/successor attributes to great effect
158158
in this new algorithm for both forward and backward traversals:
159159

160160
```swift
161-
func traverseInOrderForward(visit: T -> Void) {
162-
var n: ThreadedBinaryTree
163-
n = minimum()
164-
while true {
165-
visit(n.value)
166-
if let successor = n.successor() {
167-
n = successor
168-
} else {
169-
break
161+
public func traverseInOrderForward(_ visit: (T) -> Void) {
162+
var n: ThreadedBinaryTree
163+
n = minimum()
164+
while true {
165+
visit(n.value)
166+
if let successor = n.successor() {
167+
n = successor
168+
} else {
169+
break
170+
}
171+
}
170172
}
171-
}
172-
}
173173

174-
func traverseInOrderBackward(visit: T -> Void) {
175-
var n: ThreadedBinaryTree
176-
n = maximum()
177-
while true {
178-
visit(n.value)
179-
if let predecessor = n.predecessor() {
180-
n = predecessor
181-
} else {
182-
break
174+
public func traverseInOrderBackward(_ visit: (T) -> Void) {
175+
var n: ThreadedBinaryTree
176+
n = maximum()
177+
while true {
178+
visit(n.value)
179+
if let predecessor = n.predecessor() {
180+
n = predecessor
181+
} else {
182+
break
183+
}
184+
}
183185
}
184-
}
185-
}
186186
```
187187
Again, this a method of `ThreadedBinaryTree`, so we'd call it via
188188
`node.traverseInorderForward(visitFunction)`. Note that we are able to specify
@@ -221,7 +221,7 @@ continuously manage the `leftThread` and `rightThread` variables. Rather than
221221
walking through some boring code, it is best to explain this with an example
222222
(although you can read through [the implementation](ThreadedBinaryTree.swift)
223223
if you want to know the finer details). Please note that this requires
224-
knowledge of binary search trees, so make sure you have
224+
knowledge of binary search trees, so make sure you have
225225
[read this first](../Binary Search Tree/).
226226

227227
> Note: we do allow duplicate nodes in this implementation of a threaded binary
@@ -342,11 +342,12 @@ Many of these methods are inherent to binary search trees as well, so you can
342342
find [further documentation here](../Binary Search Tree/).
343343

344344

345-
## See also
345+
## See also
346346

347347
[Threaded Binary Tree on Wikipedia](https://en.wikipedia.org/wiki/Threaded_binary_tree)
348348

349349
*Written for the Swift Algorithm Club by
350350
[Jayson Tung](https://github.com/JFTung)*
351+
*Migrated to Swift 3 by Jaap Wijnen*
351352

352353
*Images made using www.draw.io*

Threaded Binary Tree/ThreadedBinaryTreeTests.swift renamed to Threaded Binary Tree/ThreadedBinaryTree.playground/Contents.swift

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1-
/*
2-
TESTS
3-
I don't have access to an Apple computer, so I can't make a Playground or any
4-
of that fancy stuff. Here's a simple demonstration of the ThreadedBinaryTree
5-
class. It follows the examples in the README.
6-
*/
1+
//: Playground - noun: a place where people can play
2+
73

84
// Simple little debug function to make testing output pretty
9-
func check(tree: ThreadedBinaryTree<Int>?) {
10-
if let tree = tree {
11-
print("\(tree.count) Total Nodes:")
12-
print(tree)
13-
print("Debug Info:")
14-
print(tree.debugDescription)
15-
print("In-Order Traversal:")
16-
let myArray = tree.toArray()
17-
for node in myArray {
18-
print(node)
19-
}
20-
if tree.isBST(minValue: Int.min, maxValue: Int.max) {
21-
print("This is a VALID binary search tree.")
22-
} else {
23-
print("This is an INVALID binary search tree.")
24-
}
25-
if tree.isThreaded() {
26-
print("This is a VALID threaded binary tree.")
5+
func check(_ tree: ThreadedBinaryTree<Int>?) {
6+
if let tree = tree {
7+
print("\(tree.count) Total Nodes:")
8+
print(tree)
9+
print("Debug Info:")
10+
print(tree.debugDescription)
11+
print("In-Order Traversal:")
12+
let myArray = tree.toArray()
13+
for node in myArray {
14+
print(node)
15+
}
16+
if tree.isBST(minValue: Int.min, maxValue: Int.max) {
17+
print("This is a VALID binary search tree.")
18+
} else {
19+
print("This is an INVALID binary search tree.")
20+
}
21+
if tree.isThreaded() {
22+
print("This is a VALID threaded binary tree.")
23+
} else {
24+
print("This is an INVALID threaded binary tree.")
25+
}
2726
} else {
28-
print("This is an INVALID threaded binary tree.")
27+
print("This tree is nil.")
2928
}
30-
} else {
31-
print("This tree is nil.")
32-
}
3329
}
3430

3531

0 commit comments

Comments
 (0)