Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threaded binary tree fix #377

Merged
merged 3 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 25 additions & 24 deletions Threaded Binary Tree/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,31 @@ outlined above. We use these predecessor/successor attributes to great effect
in this new algorithm for both forward and backward traversals:

```swift
func traverseInOrderForward(visit: T -> Void) {
var n: ThreadedBinaryTree
n = minimum()
while true {
visit(n.value)
if let successor = n.successor() {
n = successor
} else {
break
public func traverseInOrderForward(_ visit: (T) -> Void) {
var n: ThreadedBinaryTree
n = minimum()
while true {
visit(n.value)
if let successor = n.successor() {
n = successor
} else {
break
}
}
}
}
}

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

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


## See also
## See also

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

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

*Images made using www.draw.io*
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
TESTS
I don't have access to an Apple computer, so I can't make a Playground or any
of that fancy stuff. Here's a simple demonstration of the ThreadedBinaryTree
class. It follows the examples in the README.
*/
//: Playground - noun: a place where people can play


// Simple little debug function to make testing output pretty
func check(tree: ThreadedBinaryTree<Int>?) {
func check(_ tree: ThreadedBinaryTree<Int>?) {
if let tree = tree {
print("\(tree.count) Total Nodes:")
print(tree)
Expand All @@ -27,9 +23,9 @@ func check(tree: ThreadedBinaryTree<Int>?) {
} else {
print("This is an INVALID threaded binary tree.")
}
} else {
print("This tree is nil.")
}
} else {
print("This tree is nil.")
}
}


Expand Down
Loading