@@ -14,14 +14,15 @@ If you don't know what a tree is or what it is for, then
14
14
## In-order traversal
15
15
16
16
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
18
18
of the tree. An in-order traversal of a binary tree visits the nodes in the
19
19
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
22
23
children last.
23
24
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
25
26
syntax):
26
27
27
28
``` swift
@@ -68,16 +69,16 @@ threaded**:
68
69
69
70
Using a single or double threaded tree depends on what we want to accomplish.
70
71
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.
73
74
74
75
It is important to note that each node stores either its predecessor or its
75
76
left child, and either its successor or its right child. The nodes do not
76
77
need to keep track of both. For example, in a double threaded tree, if a node
77
78
has a right child but no left child, it will track its predecessor in place of
78
79
its left child.
79
80
80
- Here is a valid "full" threaded binary tree:
81
+ Here is an example valid "full" threaded binary tree:
81
82
82
83
![ Full] ( Images/Full.png )
83
84
@@ -106,11 +107,11 @@ The core of this data structure is the `ThreadedBinaryTree<T: Comparable>`
106
107
class. Each instance of this class represents a node with six member
107
108
variables: ` value ` , ` parent ` , ` left ` , ` right ` , ` leftThread ` , and
108
109
` rightThread ` . Of all of these, only ` value ` is required. The other five are
109
- Swift * optionals* .
110
+ Swift * optionals* (they may be ` nil ` ) .
110
111
- ` 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
114
115
- ` leftThread: ThreadedBinaryTree? ` is the in-order predecessor of this node
115
116
- ` rightThread: ThreadedBinaryTree? ` is the in-order successor of this node
116
117
@@ -213,7 +214,6 @@ A backward traversal would be very similar, but you would replace `right`,
213
214
` maximum() ` , and ` predecessor() ` .
214
215
215
216
216
-
217
217
## Insertion and deletion
218
218
219
219
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:
288
288
2 . For the leftmost node ** m** in the ` right ` subtree of any node ** n** ,
289
289
** m** 's ` leftThread ` is ** n** .
290
290
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 ` .
297
297
298
298
How about we do something crazy? What would happen if we tried to remove
299
299
** 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
314
314
** 10** , as we just have to update the edges leaving ** 10** . Now all we have to
315
315
do is fiddle with the threads in order to maintain the two properties outlined
316
316
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
318
318
variables to ` nil ` .
319
319
320
320
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
326
326
** 5** , but mirrored. ** 5** had a ` left ` child, while ** 12** has a ` right `
327
327
child, but the core algorithm is the same.
328
328
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
331
333
[ the implementation] ( ThreadedBinaryTree.swift ) .
332
334
333
335
0 commit comments