Skip to content

Commit

Permalink
Finish threaded bin. tree images
Browse files Browse the repository at this point in the history
  • Loading branch information
jftung committed May 3, 2016
1 parent 7914569 commit 75a94e3
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 19 deletions.
Binary file removed Threaded Binary Tree/Images/DelOne.png
Binary file not shown.
Binary file removed Threaded Binary Tree/Images/DelThree.png
Binary file not shown.
Binary file removed Threaded Binary Tree/Images/DelTwo.png
Binary file not shown.
File renamed without changes
File renamed without changes
Binary file added Threaded Binary Tree/Images/Insert3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Threaded Binary Tree/Images/Remove1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Threaded Binary Tree/Images/Remove2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Threaded Binary Tree/Images/Remove3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Threaded Binary Tree/Images/Remove4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions Threaded Binary Tree/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ need to keep track of both. For example, in a double threaded tree, if a node
has a right child but no left child, it will track its predecessor in place of
its left child.

Here is a valid "full" threaded binary tree:

![Full](/Images/Full.png)

While the following threaded binary tree is not "full," it is still valid. The
structure of the tree does not matter as long as it follows the definition of a
binary search tree:

![Partial](/Images/Partial.png)

The solid lines denote the links between parents and children, while the dotted
lines denote the "threads." The important thing to note here is how the
children and thread edges interact with each other. Every node besides the
root has one entering edge (from its parent), and two leaving edges: one to the
left and one to the right. The left leaving edge goes to the node's left child
if it exists, and to its in-order predecessor if it does not. The right
leaving edge goes to the node's right child if it exists, and to its in-order
successor if it does not. The exceptions are the left-most node and the
right-most node, which do not have a predecessor or successor, respectively.


## Representation

Expand Down Expand Up @@ -175,6 +195,39 @@ to explain this with an example. Please note that this requires knowledge of
binary search trees, so make sure you have
[read this first](../Binary Search Tree/).

Base:

![Base](/Images/Base.png)

Insert1:

![Insert1](/Images/Insert1.png)

Insert2:

![Insert2](/Images/Insert2.png)

Insert3:

![Insert3](/Images/Insert3.png)

Remove1:

![Remove1](/Images/Remove1.png)

Remove2:

![Remove2](/Images/Remove2.png)

Remove3:

![Remove3](/Images/Remove3.png)

Remove4:

![Remove4](/Images/Remove4.png)



### Still under construction.

Expand Down
50 changes: 31 additions & 19 deletions Threaded Binary Tree/ThreadedBinaryTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,24 @@ extension ThreadedBinaryTree: CustomDebugStringConvertible {
*/

// Simple little debug function to make testing output pretty
private func check(tree: ThreadedBinaryTree<Int>) {
print("\(tree.count) Total Nodes:");
print(tree)
print("Debug Info:")
print(tree.debugDescription)
print("In-Order Traversal:")
let myArray = tree.toArray()
for node in myArray {
print(node)
}
if tree.isThreaded() {
print("This threaded binary tree is VALID.")
private func check(tree: ThreadedBinaryTree<Int>?) {
if let tree = tree {
print("\(tree.count) Total Nodes:");
print(tree)
print("Debug Info:")
print(tree.debugDescription)
print("In-Order Traversal:")
let myArray = tree.toArray()
for node in myArray {
print(node)
}
if tree.isThreaded() {
print("This threaded binary tree is VALID.")
} else {
print("This threaded binary tree is INVALID.")
}
} else {
print("This threaded binary tree is INVALID.")
print("This tree is nil.")
}
}

Expand Down Expand Up @@ -530,20 +534,28 @@ print("\nInsert 4")
tree.insert(4)
check(tree)

print("\nRemove 13 (Not in the Tree)")
tree.remove(13)
print("\nInsert 15")
tree.insert(15)
check(tree)

print("\nRemove 5")
tree.remove(5)
print("\nRemove 13 (Not in the Tree)")
tree.remove(13)
check(tree)

print("\nRemove 7")
tree.remove(7)
check(tree)

print("\nRemove 10")
tree.remove(10)
print("\nRemove 5")
tree.remove(5)
check(tree)

print("\nRemove 9 (root)")
let newRoot = tree.remove(9)
check(newRoot)

print("\nRemove 12")
newRoot?.remove(12)
check(newRoot)

print("\n\nDone with Tests!\n")

0 comments on commit 75a94e3

Please sign in to comment.