Visual Binary Trees with Swift 3: Introduction
26 Aug 2016
- Introduction & Summary
- Part I: QuickLook-able Binary Tree with Pluggable Traversals
- Part II: The Drawing Architecture, Customization, and Tree Layouts
Swift Playgrounds have always been a great showcase for the language, and now it’s elevated even further with the new Playgrounds app for iPad.
Experimenting with the app in one of the early iOS10 betas made me seriously wonder if it finally will give Apple the key to the edu market. While not their first dedicated product (remember eMac?), this is Apple’s first serious software directly focused on the needs of the educational sector. By all early indications, it should get them lots of iPads sales there and also create some interesting new opportunities for technical writers.
As Swift 3 got over its ‘no-source-breaking changes’ milestone, I thought it’d be interesting to play with the playgrounds some more and do something edu-related that would also be useful for my regular projects.
Since learning / teaching how to write code eventually gets you to base data structures and algorithms, the idea was to visualize binary trees along with their common operations. While there are some tools such as VisuAlgo.net, having an instant visual tree representation right in a Swift playground would surely be handy both for learning and for real-life work.
So after a few weekends, here is a short video right from the iPad app showing some features such as base tree drawing and visualization of pluggable traversals:
And a similar screenshot in Xcode:
If at that point you are ready to go and play with the playground, that is completely fine.
If on the other hand you want more details on how to use it with your own trees, customize many of the visualization aspects, or interested in Swift 3 implementation, or perhaps just curious about the general problem of optimal layouts for arbitrary trees – keep reading for a deep dive into things like protocols with associated types (PATs), brief history of tree layouts, overall drawing architecture (largely attributed to Crusty), applicable 2D/3D visualization technology, etc.
An embeddable framework version under MIT license is TBD after Swift 3 is finalized ↩