Skip to content

Commit f4be92e

Browse files
committed
Improve debug output for binary search tree
1 parent ba15a8d commit f4be92e

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

Binary Search Tree/README.markdown

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,13 @@ When working with somewhat complicated data structures such as this, it's useful
226226
```swift
227227
extension BinarySearchTree: CustomStringConvertible {
228228
public var description: String {
229-
var s = "value: \(value)"
230-
if let parent = parent {
231-
s += ", parent: \(parent.value)"
232-
}
229+
var s = ""
233230
if let left = left {
234-
s += ", left = [" + left.description + "]"
231+
s += "(\(left.description)) <- "
235232
}
233+
s += "\(value)"
236234
if let right = right {
237-
s += ", right = [" + right.description + "]"
235+
s += " -> (\(right.description))"
238236
}
239237
return s
240238
}
@@ -243,16 +241,7 @@ extension BinarySearchTree: CustomStringConvertible {
243241

244242
When you do a `print(tree)`, you should get something like this:
245243

246-
value: 7, left = [value: 2, parent: 7, left = [value: 1, parent: 2], right = [value: 5, parent: 2]], right = [value: 10, parent: 7, left = [value: 9, parent: 10]]
247-
248-
To make this a bit clearer to understand, reformat it slightly:
249-
250-
value: 7,
251-
left = [value: 2, parent: 7,
252-
left = [value: 1, parent: 2],
253-
right = [value: 5, parent: 2]],
254-
right = [value: 10, parent: 7,
255-
left = [value: 9, parent: 10]]
244+
((1) <- 2 -> (5)) <- 7 -> ((9) <- 10)
256245

257246
With some imagination, you should see now that this indeed corresponds to the following tree:
258247

@@ -477,17 +466,19 @@ Try it out:
477466

478467
```swift
479468
if let node2 = tree.search(2) {
469+
print(tree) // before
480470
node2.remove()
471+
print(tree) // after
481472
}
482473
```
483474

484-
First you find the node that you want to remove with `search()` and then you call `remove()` on that object. If you now print out the tree you get:
475+
First you find the node that you want to remove with `search()` and then you call `remove()` on that object. Before the removal, the tree printed like this:
476+
477+
((1) <- 2 -> (5)) <- 7 -> ((9) <- 10)
478+
479+
But after `remove()` you get:
485480

486-
value: 7,
487-
left = [value: 5, parent: 7,
488-
left = [value: 1, parent: 5]],
489-
right = [value: 10, parent: 7,
490-
left = [value: 9, parent: 10]]
481+
((1) <- 5) <- 7 -> ((9) <- 10)
491482

492483
As you can see, node `5` has taken the place of `2`. In fact, if you do `print(node2)` you'll see that it now has the value `5`. We didn't actually remove the `node2` object, we just gave it a new value.
493484

Binary Search Tree/Solution 1/BinarySearchTree.playground/Contents.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ tree.insert(5)
66
tree.insert(10)
77
tree.insert(9)
88
tree.insert(1)
9+
910
tree
11+
tree.debugDescription
1012

1113
let tree2 = BinarySearchTree<Int>(array: [7, 2, 5, 10, 9, 1])
1214

@@ -24,7 +26,7 @@ tree.maximum()
2426

2527
if let node2 = tree.search(2) {
2628
node2.remove()
27-
node2 // this is now node "5"
29+
node2.value // this is now node "5"
2830
print(tree)
2931
}
3032

Binary Search Tree/Solution 1/BinarySearchTree.playground/Sources/BinarySearchTree.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,29 @@ extension BinarySearchTree {
311311

312312
extension BinarySearchTree: CustomStringConvertible {
313313
public var description: String {
314+
var s = ""
315+
if let left = left {
316+
s += "(\(left.description)) <- "
317+
}
318+
s += "\(value)"
319+
if let right = right {
320+
s += " -> (\(right.description))"
321+
}
322+
return s
323+
}
324+
}
325+
326+
extension BinarySearchTree: CustomDebugStringConvertible {
327+
public var debugDescription: String {
314328
var s = "value: \(value)"
315329
if let parent = parent {
316330
s += ", parent: \(parent.value)"
317331
}
318332
if let left = left {
319-
s += ", left = [" + left.description + "]"
333+
s += ", left = [" + left.debugDescription + "]"
320334
}
321335
if let right = right {
322-
s += ", right = [" + right.description + "]"
336+
s += ", right = [" + right.debugDescription + "]"
323337
}
324338
return s
325339
}

Binary Search Tree/Solution 1/BinarySearchTree.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,29 @@ extension BinarySearchTree {
311311

312312
extension BinarySearchTree: CustomStringConvertible {
313313
public var description: String {
314+
var s = ""
315+
if let left = left {
316+
s += "(\(left.description)) <- "
317+
}
318+
s += "\(value)"
319+
if let right = right {
320+
s += " -> (\(right.description))"
321+
}
322+
return s
323+
}
324+
}
325+
326+
extension BinarySearchTree: CustomDebugStringConvertible {
327+
public var debugDescription: String {
314328
var s = "value: \(value)"
315329
if let parent = parent {
316330
s += ", parent: \(parent.value)"
317331
}
318332
if let left = left {
319-
s += ", left = [" + left.description + "]"
333+
s += ", left = [" + left.debugDescription + "]"
320334
}
321335
if let right = right {
322-
s += ", right = [" + right.description + "]"
336+
s += ", right = [" + right.debugDescription + "]"
323337
}
324338
return s
325339
}

0 commit comments

Comments
 (0)