Data Structures & Algorithms in Swift · Challenge: Binary Tree | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/977854-data-structures-algorithms-in-swift/lessons/12

I have a question, when you are creating the first method inOrderTraversal, “visit”, is a completion handler?, I would like to understand more about

visit is a closure. It lets you define what visit does each time the method is called. In our serialize() function, visit adds the value of each node to an array.

I don’t think I’d call visit a completion handler, though, because we aren’t really using visit to mark the completion of anything. A completion handler for a binary tree might be a closure that’s called once the entire tree has been traversed.

@catie, @jessycatterwaul, why do we use first and afterwards? Why can’t we use T or Element always?

I’m sorry, we don’t understand. Can you clarify?

Hm… it looks very strange as the main words of my message just vanished totally…
Once again =) Why do we use Element first and T afterwards? Why can’t we use T or Element always?

Aha! That makes much more sense :joy_cat:

You can use T or Element every time. Really, you can name them whatever is most meaningful to you. I should have been more consistent, though. My apologies!

1 Like

Thank you very much =)