Skip to content

Commit ab3f24a

Browse files
committed
Finalize Threaded Binary Tree README
1 parent 8fdc1af commit ab3f24a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

Threaded Binary Tree/README.markdown

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ If you don't know what a tree is or what it is for, then
1414
## In-order traversal
1515

1616
The main motivation behind using a threaded binary tree over a simpler and
17-
smaller standard binary tree is to increase the speed of in-order traversal
17+
smaller standard binary tree is to increase the speed of an in-order traversal
1818
of the tree. An in-order traversal of a binary tree visits the nodes in the
1919
order in which they are stored, which matches the underlying ordering of a
20-
[binary search tree](../Binary Search Tree/). The idea is to visit all the
21-
left children of a node first, then visit the node itself, and visit the left
20+
[binary search tree](../Binary Search Tree/). Thus threaded binary trees are
21+
almost always binary search trees too. The idea is to visit all the left
22+
children of a node first, then visit the node itself, and visit the left
2223
children last.
2324

24-
In-order traversal of any binary tree generally goes as follows (using Swift
25+
An in-order traversal of any binary tree generally goes as follows (using Swift
2526
syntax):
2627

2728
```swift
@@ -68,16 +69,16 @@ threaded**:
6869

6970
Using a single or double threaded tree depends on what we want to accomplish.
7071
If we only need to traverse the tree in one direction (either forward or
71-
backward), use a single threaded tree. If we want to traverse in both
72-
directions, use a double threaded tree.
72+
backward), then we use a single threaded tree. If we want to traverse in both
73+
directions, then we use a double threaded tree.
7374

7475
It is important to note that each node stores either its predecessor or its
7576
left child, and either its successor or its right child. The nodes do not
7677
need to keep track of both. For example, in a double threaded tree, if a node
7778
has a right child but no left child, it will track its predecessor in place of
7879
its left child.
7980

80-
Here is a valid "full" threaded binary tree:
81+
Here is an example valid "full" threaded binary tree:
8182

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

@@ -106,11 +107,11 @@ The core of this data structure is the `ThreadedBinaryTree<T: Comparable>`
106107
class. Each instance of this class represents a node with six member
107108
variables: `value`, `parent`, `left`, `right`, `leftThread`, and
108109
`rightThread`. Of all of these, only `value` is required. The other five are
109-
Swift *optionals*.
110+
Swift *optionals* (they may be `nil`).
110111
- `value: T` is the value of this node (e.g. 1, 2, A, B, etc.)
111-
- `parent: ThreadedBinaryTree?` is the parent of this node (if it exists)
112-
- `left: ThreadedBinaryTree?` is the left child of this node (if it exists)
113-
- `right: ThreadedBinaryTree?` is the right child of this node (if it exists)
112+
- `parent: ThreadedBinaryTree?` is the parent of this node
113+
- `left: ThreadedBinaryTree?` is the left child of this node
114+
- `right: ThreadedBinaryTree?` is the right child of this node
114115
- `leftThread: ThreadedBinaryTree?` is the in-order predecessor of this node
115116
- `rightThread: ThreadedBinaryTree?` is the in-order successor of this node
116117

@@ -213,7 +214,6 @@ A backward traversal would be very similar, but you would replace `right`,
213214
`maximum()`, and `predecessor()`.
214215

215216

216-
217217
## Insertion and deletion
218218

219219
The quick in-order traversal that a threaded binary trees gives us comes at a
@@ -288,12 +288,12 @@ understand two important properties of threaded binary trees:
288288
2. For the leftmost node **m** in the `right` subtree of any node **n**,
289289
**m**'s `leftThread` is **n**.
290290

291-
Note how this property held true before the removal of **5**, as **4** was the
292-
rightmost node in **5**'s `left` subtree. In order to maintain this property,
293-
we must set **4**'s `rightThread` to **9**, as **4** is now the rightmost node
294-
in **9**'s `left` subtree. To completely remove **5**, all we now have to do
295-
is set **5**'s `parent`, `left`, `right`, `leftThread`, and `rightThread` to
296-
`nil`.
291+
Note how these properties held true before the removal of **5**, as **4** was
292+
the rightmost node in **5**'s `left` subtree. In order to maintain this
293+
property, we must set **4**'s `rightThread` to **9**, as **4** is now the
294+
rightmost node in **9**'s `left` subtree. To completely remove **5**, all we
295+
now have to do is set **5**'s `parent`, `left`, `right`, `leftThread`, and
296+
`rightThread` to `nil`.
297297

298298
How about we do something crazy? What would happen if we tried to remove
299299
**9**, the root node? This is the resulting tree:
@@ -314,7 +314,7 @@ tree using the algorithms outlined above. This ensures that the edges in the
314314
**10**, as we just have to update the edges leaving **10**. Now all we have to
315315
do is fiddle with the threads in order to maintain the two properties outlined
316316
above. In this case, **12**'s `leftThread` is now **10**. Node **9** is no
317-
longer needed, so we can finish teh removal process by setting all of its
317+
longer needed, so we can finish the removal process by setting all of its
318318
variables to `nil`.
319319

320320
In order to illustrate how to remove a node that has only a `right` child,
@@ -326,8 +326,10 @@ The process to remove **12** is identical to the process we used to remove
326326
**5**, but mirrored. **5** had a `left` child, while **12** has a `right`
327327
child, but the core algorithm is the same.
328328

329-
That is a quick overview of how insertion and deletion work in threaded binary
330-
trees. More detail can of course be found in
329+
And that's it! This was just a quick overview of how insertion and deletion
330+
work in threaded binary trees, but if you understood these examples, you should
331+
be able to insert or remove any node from any tree you want. More detail can
332+
of course be found in
331333
[the implementation](ThreadedBinaryTree.swift).
332334

333335

0 commit comments

Comments
 (0)