You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Binary Search Tree/README.markdown
+39-36Lines changed: 39 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ Sometimes you don't want to look at just a single node, but at all of them.
58
58
There are three ways to traverse a binary tree:
59
59
60
60
1.*In-order* (or *depth-first*): first look at the left child of a node, then at the node itself, and finally at its right child.
61
-
2.*Pre-order*: first look at a node, then its left and right children.
61
+
2.*Pre-order*: first look at a node, then its left and right children.
62
62
3.*Post-order*: first look at the left and right children and process the node itself last.
63
63
64
64
Once again, this happens recursively.
@@ -133,7 +133,7 @@ public class BinarySearchTree<T: Comparable> {
133
133
}
134
134
```
135
135
136
-
This class describes just a single node, not the entire tree. It's a generic type, so the node can store any kind of data. It also has references to its `left` and `right` child nodes and a `parent` node.
136
+
This class describes just a single node, not the entire tree. It's a generic type, so the node can store any kind of data. It also has references to its `left` and `right` child nodes and a `parent` node.
137
137
138
138
Here's how you'd use it:
139
139
@@ -258,7 +258,7 @@ Here is the implementation of `search()`:
258
258
259
259
I hope the logic is clear: this starts at the current node (usually the root) and compares the values. If the search value is less than the node's value, we continue searching in the left branch; if the search value is greater, we dive into the right branch.
260
260
261
-
Of course, if there are no more nodes to look at -- when `left` or `right` is nil -- then we return `nil` to indicate the search value is not in the tree.
261
+
Of course, if there are no more nodes to look at -- when `left` or `right` is nil -- then we return `nil` to indicate the search value is not in the tree.
262
262
263
263
> **Note:** In Swift that's very conveniently done with optional chaining; when you write `left?.search(value)` it automatically returns nil if `left` is nil. There's no need to explicitly check for this with an `if` statement.
264
264
@@ -300,21 +300,21 @@ The first three lines all return the corresponding `BinaryTreeNode` object. The
300
300
Remember there are 3 different ways to look at all nodes in the tree? Here they are:
// Replacement for current node can be either biggest one on the left or
416
417
// smallest one on the right, whichever is not nil
417
-
iflet left = left {
418
-
replacement = left.maximum()
419
-
} elseiflet right = right {
418
+
iflet right = right {
420
419
replacement = right.minimum()
420
+
} elseiflet left = left {
421
+
replacement = left.maximum()
421
422
} else {
422
-
replacement =nil;
423
+
replacement =nil
423
424
}
424
-
425
-
replacement?.remove();
425
+
426
+
replacement?.remove()
426
427
427
428
// Place the replacement on current node's position
428
-
replacement?.right= right;
429
-
replacement?.left= left;
430
-
reconnectParentTo(node:replacement);
431
-
429
+
replacement?.right= right
430
+
replacement?.left= left
431
+
right?.parent= replacement
432
+
left?.parent= replacement
433
+
reconnectParentTo(node:replacement)
434
+
432
435
// The current node is no longer part of the tree, so clean it up.
433
436
parent =nil
434
437
left =nil
435
438
right =nil
436
-
437
-
return replacement;
439
+
440
+
return replacement
438
441
}
439
442
```
440
443
@@ -574,7 +577,7 @@ if let node1 = tree.search(1) {
574
577
575
578
## The code (solution 2)
576
579
577
-
We've implemented the binary tree node as a class but you can also use an enum.
580
+
We've implemented the binary tree node as a class but you can also use an enum.
578
581
579
582
The difference is reference semantics versus value semantics. Making a change to the class-based tree will update that same instance in memory. But the enum-based tree is immutable -- any insertions or deletions will give you an entirely new copy of the tree. Which one is best totally depends on what you want to use it for.
580
583
@@ -588,7 +591,7 @@ public enum BinarySearchTree<T: Comparable> {
588
591
}
589
592
```
590
593
591
-
The enum has three cases:
594
+
The enum has three cases:
592
595
593
596
-`Empty` to mark the end of a branch (the class-based version used `nil` references for this).
594
597
-`Leaf` for a leaf node that has no children.
@@ -606,7 +609,7 @@ As usual, we'll implement most functionality recursively. We'll treat each case
0 commit comments