Skip to content

Commit f86421c

Browse files
authored
Merge pull request kodecocodes#377 from JaapWijnen/threaded-binary-tree-fix
Threaded binary tree fix
2 parents ca8e7ff + 92363b8 commit f86421c

File tree

5 files changed

+127
-119
lines changed

5 files changed

+127
-119
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: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
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>?) {
5+
func check(_ tree: ThreadedBinaryTree<Int>?) {
106
if let tree = tree {
117
print("\(tree.count) Total Nodes:")
128
print(tree)
@@ -27,9 +23,9 @@ func check(tree: ThreadedBinaryTree<Int>?) {
2723
} else {
2824
print("This is an INVALID threaded binary tree.")
2925
}
30-
} else {
31-
print("This tree is nil.")
32-
}
26+
} else {
27+
print("This tree is nil.")
28+
}
3329
}
3430

3531

0 commit comments

Comments
 (0)