@@ -17,10 +17,10 @@ The main motivation behind using a threaded binary tree over a simpler and
17
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/). 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
23
- children last.
20
+ [ binary search tree] (../Binary Search Tree/). This means most threaded binary
21
+ trees are also binary search trees. The idea is to visit all the left children
22
+ of a node first, then visit the node itself, and then visit the right children
23
+ last.
24
24
25
25
An in-order traversal of any binary tree generally goes as follows (using Swift
26
26
syntax):
@@ -89,25 +89,24 @@ binary search tree:
89
89
![ Partial] ( Images/Partial.png )
90
90
91
91
The solid lines denote the links between parents and children, while the dotted
92
- lines denote the "threads." The important thing to note here is how the
93
- children and thread edges interact with each other. Every node besides the
94
- root has one entering edge (from its parent), and two leaving edges: one to the
95
- left and one to the right. The left leaving edge goes to the node's left child
96
- if it exists, and to its in-order predecessor if it does not. The right
97
- leaving edge goes to the node's right child if it exists, and to its in-order
98
- successor if it does not. The exceptions are the left-most node and the
99
- right-most node, which do not have a predecessor or successor, respectively.
92
+ lines denote the "threads." It is important to note how the children and
93
+ thread edges interact with each other. Every node besides the root has one
94
+ entering edge (from its parent), and two leaving edges: one to the left and one
95
+ to the right. The left leaving edge goes to the node's left child if it
96
+ exists, and to its in-order predecessor if it does not. The right leaving edge
97
+ goes to the node's right child if it exists, and to its in-order successor if
98
+ it does not. The exceptions are the left-most node and the right-most node,
99
+ which do not have a predecessor or successor, respectively.
100
100
101
101
102
102
## Representation
103
103
104
- Before we go into detail about the methods that we can apply to threaded binary
105
- trees, it might be a good idea to explain how we will be representing the tree.
106
- The core of this data structure is the ` ThreadedBinaryTree<T: Comparable> `
107
- class. Each instance of this class represents a node with six member
108
- variables: ` value ` , ` parent ` , ` left ` , ` right ` , ` leftThread ` , and
109
- ` rightThread ` . Of all of these, only ` value ` is required. The other five are
110
- Swift * optionals* (they may be ` nil ` ).
104
+ Before we go into detail about the methods of a threaded binary tree, we should
105
+ first explain how the tree itself is represented. The core of this data
106
+ structure is the ` ThreadedBinaryTree<T: Comparable> ` class. Each instance of
107
+ this class represents a node with six member variables: ` value ` , ` parent ` ,
108
+ ` left ` , ` right ` , ` leftThread ` , and ` rightThread ` . Of all of these, only
109
+ ` value ` is required. The other five are Swift * optionals* (they may be ` nil ` ).
111
110
- ` value: T ` is the value of this node (e.g. 1, 2, A, B, etc.)
112
111
- ` parent: ThreadedBinaryTree? ` is the parent of this node
113
112
- ` left: ThreadedBinaryTree? ` is the left child of this node
@@ -185,7 +184,7 @@ func traverseInOrderBackward(visit: T -> Void) {
185
184
}
186
185
}
187
186
```
188
- Again, this a method of ` ThreadedBinaryTree ` , so we'd call it like
187
+ Again, this a method of ` ThreadedBinaryTree ` , so we'd call it via
189
188
` node.traverseInorderForward(visitFunction) ` . Note that we are able to specify
190
189
a function that executes on each node as they are visited. This function can
191
190
be anything you want, as long as it accepts ` T ` (the type of the values of the
0 commit comments