├── README.md ├── Screenshots ├── screenshot_1.PNG ├── screenshot_2.PNG └── screenshot_3.PNG └── Tree Trouble.playgroundbook └── Contents ├── Chapters ├── Chapter1.playgroundchapter │ ├── Manifest.plist │ └── Pages │ │ ├── Page1.playgroundpage │ │ ├── Contents.swift │ │ └── Manifest.plist │ │ └── Page2.playgroundpage │ │ ├── Contents.swift │ │ └── Manifest.plist └── Chapter2.playgroundchapter │ ├── Manifest.plist │ └── Pages │ ├── Page3.playgroundpage │ ├── Contents.swift │ ├── Manifest.plist │ └── PrivateResources │ │ └── Hints.plist │ ├── Page4.playgroundpage │ ├── Contents.swift │ └── Manifest.plist │ └── TreeTrouble.cutscenepage │ ├── Manifest.plist │ └── Resources │ ├── treeTroubleCutscene.html │ └── treeTroubleCutscene.hyperesources │ ├── 484D19-restorable.plist │ ├── HYPE-576.full.min.js │ ├── HYPE-576.thin.min.js │ ├── PIE.htc │ ├── blank.gif │ ├── treetroublecutscene_hype_generated_script.js │ └── underlay.jpg ├── Manifest.plist ├── Resources ├── background.png ├── correct.wav ├── icon.png ├── loop.wav ├── underlay.jpg └── wrong.wav └── Sources ├── BSTScene.swift ├── BSTView.swift ├── BinarySearchTree.swift ├── GameScene.swift ├── GameView.swift └── Node.swift /README.md: -------------------------------------------------------------------------------- 1 | # Tree Trouble 2 | >An interactive Swift Playground Book about Binary Search Trees, designed for the Swift Playgrounds application on the iPad. 3 | This is my personal entry for the WWDC 2017 scholarship application. The book is a simple, responsive tutorial with a 4 | game on the last page. You will help create, insert nodes, and traverse your own Binary Search Tree using the Swift coding 5 | language. 6 | 7 |
8 | 9 |
10 | 11 | # Install Instructions 12 | To run the playground book, you will need an iPad with the Swift Playgrounds application installed. 13 | 14 | 1. In a command line, type `git clone https://github.com/joelrorseth/Tree-Trouble` 15 | 2. Open the new folder in Finder, and use Airdrop or email to `transfer the Tree Trouble.playgroundbook to the iPad device`. 16 | You can also upload the file to iCloud Drive or Google Drive, then choose to import from one of these services in the 17 | Swift Playgrounds application. 18 | 3. On the iPad, open the file in Swift Playgrounds. 19 | 20 |
21 | 22 | # License 23 | MIT License 24 | 25 | Copyright (c) [2017] [Joel Rorseth] 26 | 27 | Permission is hereby granted, free of charge, to any person obtaining a copy 28 | of this software and associated documentation files (the "Software"), to deal 29 | in the Software without restriction, including without limitation the rights 30 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 31 | copies of the Software, and to permit persons to whom the Software is 32 | furnished to do so, subject to the following conditions: 33 | 34 | The above copyright notice and this permission notice shall be included in all 35 | copies or substantial portions of the Software. 36 | 37 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 38 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 39 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 40 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 41 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 42 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 43 | SOFTWARE. 44 | -------------------------------------------------------------------------------- /Screenshots/screenshot_1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelrorseth/Tree-Trouble/86c6b8c4e388ff168535e5469816b94b9c4afc81/Screenshots/screenshot_1.PNG -------------------------------------------------------------------------------- /Screenshots/screenshot_2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelrorseth/Tree-Trouble/86c6b8c4e388ff168535e5469816b94b9c4afc81/Screenshots/screenshot_2.PNG -------------------------------------------------------------------------------- /Screenshots/screenshot_3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelrorseth/Tree-Trouble/86c6b8c4e388ff168535e5469816b94b9c4afc81/Screenshots/screenshot_3.PNG -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Version 6 | 1.0 7 | Name 8 | Introducing Binary Search Trees 9 | Pages 10 | 11 | Page1.playgroundpage 12 | Page2.playgroundpage 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page1.playgroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: # Binary Search Trees 2 | //: In the world of computer science, a high level of efficiency is expected and demanded when designing an algorithm. One of the most famous data structures employed for this use is the [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree). In this playground, you’ll learn how binary search trees work by making one yourself! 3 | 4 | //: ## How Do They Work? 5 | //: Each tree is made up of several *nodes* (represented as circles) that contain a value, each being connected to other nodes by one or more *edges* (represented as lines). Each node can have zero or one parent nodes (nodes connected above a node), and zero to two child nodes (connected below). The value of each left node is less than its parent's value, and each right child's value is greater. 6 | 7 | //: [Next: The Ordering Property](@next) 8 | 9 | // Run the code to see how the Binary Search Tree will insert the values! 10 | var bst = BinarySearchTree(array: [4, 2, 6, 1, 3, 5, 7]) 11 | 12 | //#-hidden-code 13 | 14 | import PlaygroundSupport 15 | var bstView = BSTView(tree: bst) 16 | PlaygroundPage.current.liveView = bstView 17 | 18 | //#-end-hidden-code 19 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page1.playgroundpage/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Version 6 | 1.0 7 | Name 8 | What is a Binary Search Tree? 9 | LiveViewMode 10 | VisibleByDefault 11 | LiveViewEdgeToEdge 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page2.playgroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ## But What Makes It A BST? 2 | //: The most important part of the Binary Search Tree is the ordering property. For every node, the value of each left child node is *less than* the value of itself, and the value of each right child node is *greater than* itself! Easy! 3 | 4 | //: ## Insertion 5 | //: To insert a node, start at the root (the topmost node). Determine if the value of the new node is less than or greater than this node, and follow the left edge or right edge respectively. If there is no edge in this direction, create a *new edge* here and connect it to the new node! 6 | 7 | //: Finish the code to create the tree 8 | var bst = BinarySearchTree(value:/*#-editable-code root node*/<#T##node##Int#>/*#-end-editable-code*/) 9 | 10 | //: Add nodes into the tree! 11 | bst.insert(value: /*#-editable-code number*/<#T##node##Int#>/*#-end-editable-code*/) 12 | bst.insert(value: /*#-editable-code number*/<#T##node##Int#>/*#-end-editable-code*/) 13 | bst.insert(value: /*#-editable-code number*/<#T##node##Int#>/*#-end-editable-code*/) 14 | bst.insert(value: /*#-editable-code number*/<#T##node##Int#>/*#-end-editable-code*/) 15 | bst.insert(value: /*#-editable-code number*/<#T##node##Int#>/*#-end-editable-code*/) 16 | bst.insert(value: /*#-editable-code number*/<#T##node##Int#>/*#-end-editable-code*/) 17 | 18 | //: [Next: Traversal and Ordering](@next) 19 | 20 | //#-hidden-code 21 | 22 | import PlaygroundSupport 23 | var bstView = BSTView(tree: bst) 24 | PlaygroundPage.current.liveView = bstView 25 | 26 | //#-end-hidden-code 27 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page2.playgroundpage/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Version 6 | 1.0 7 | Name 8 | The Ordering Property 9 | LiveViewMode 10 | VisibleByDefault 11 | LiveViewEdgeToEdge 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Version 6 | 1.0 7 | Name 8 | Traversal and Ordering 9 | Pages 10 | 11 | Page3.playgroundpage 12 | TreeTrouble.cutscenepage 13 | Page4.playgroundpage 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/Page3.playgroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ## Traversal 2 | //: One of the most notable features to the Binary Search Tree is the natural sorted nature of its elements. Using an [in-order traversal](https://en.wikipedia.org/wiki/Tree_traversal#In-order), a Binary Search Tree of any size can output its elements in ascending, sorted order! 3 | 4 | //: ## In-Order Traversal 5 | //: In a [tree traversal](https://en.wikipedia.org/wiki/Tree_traversal), we simply retrieve or process all tree nodes in a given order. To perform an *in-order traversal*, we will start at the root node, checking if it exists. The algorithm will then traverse the left subtree by recursively calling the traversal function again. It will then output the value of the current root node, followed by traversing the right subtree recursively. 6 | 7 | //: Fill in the in order traversal below! Note that each BinarySearchTree has a *left*, *right*, and *parent* member variable that is itself a BinarySearchTree. 8 | 9 | var sorted = [Int]() 10 | 11 | extension BinarySearchTree { 12 | 13 | // Remember the order! 14 | public func traverseInOrder() { 15 | <#branch#>?.traverseInOrder() 16 | sorted.append(value) 17 | <#branch#>?.traverseInOrder() 18 | } 19 | } 20 | 21 | var bst = BinarySearchTree(array: [21, 14, 27, 7, 16, 24, 32]) 22 | bst.traverseInOrder() 23 | 24 | // Click the square to the right to check your answer! 25 | print(sorted) 26 | 27 | //: [Next: Tree Trouble Challenge](@next) 28 | 29 | //#-hidden-code 30 | 31 | import PlaygroundSupport 32 | var bstView = BSTView(tree: bst) 33 | PlaygroundPage.current.liveView = bstView 34 | 35 | //#-end-hidden-code 36 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/Page3.playgroundpage/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Version 6 | 1.0 7 | Name 8 | Traversing the Tree 9 | LiveViewMode 10 | VisibleByDefault 11 | LiveViewEdgeToEdge 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/Page3.playgroundpage/PrivateResources/Hints.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Hints 6 | 7 | 8 | Content 9 | For inorder traversal, traverse left, process the value, then traverse right! 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/Page4.playgroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ## Tree Trouble Challenge 2 | //: Now that you have an understanding of how the Binary Search Tree works, put your skills to the test! 3 | 4 | /*: 5 | Rules 6 | * Time: 60 seconds 7 | * Objective: Identify the incorrectly placed node in each Binary Search Tree 8 | * Description: The game will continuously load random Binary Search Trees that need correcting. It is your job to identify the mistake in as many as possible! 9 | */ 10 | 11 | import PlaygroundSupport 12 | 13 | PlaygroundPage.current.liveView = GameView() 14 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/Page4.playgroundpage/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Version 6 | 1.0 7 | Name 8 | Tree Trouble 9 | LiveViewMode 10 | VisibleByDefault 11 | LiveViewEdgeToEdge 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/TreeTrouble.cutscenepage/Manifest.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Name 6 | Tree Trouble Cutscene 7 | CutsceneReference 8 | treeTroubleCutscene.html 9 | 10 | 11 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/TreeTrouble.cutscenepage/Resources/treeTroubleCutscene.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | treeTroubleCutscene 7 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/TreeTrouble.cutscenepage/Resources/treeTroubleCutscene.hyperesources/484D19-restorable.plist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelrorseth/Tree-Trouble/86c6b8c4e388ff168535e5469816b94b9c4afc81/Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/TreeTrouble.cutscenepage/Resources/treeTroubleCutscene.hyperesources/484D19-restorable.plist -------------------------------------------------------------------------------- /Tree Trouble.playgroundbook/Contents/Chapters/Chapter2.playgroundchapter/Pages/TreeTrouble.cutscenepage/Resources/treeTroubleCutscene.hyperesources/HYPE-576.thin.min.js: -------------------------------------------------------------------------------- 1 | (function(){var aa=void 0,g=!0,j=null,k=!1,fa=encodeURIComponent,n=window,q=document,z=Math,ga=navigator,ha=parseInt,ja=parseFloat;function ka(T,y){return T.width=y}function la(T,y){return T.innerHTML=y}function ma(T,y){return T.loop=y}function na(T,y){return T.zIndex=y}function pa(T,y){return T.length=y}function qa(T,y){return T.className=y}function ra(T,y){return T.MozPerspective=y}function ta(T,y){return T.display=y}function ua(T,y){return T.height=y} 2 | var A="appendChild",E="push",va="direction",Aa="lastPosition",Ba="clearTimeout",Ca="width",Da="round",Ea="slice",Fa="replace",Ga="ceil",Ha="getElementById",Ja="offsetWidth",Ka="charAt",La="preventDefault",Pa="button",Qa="indexOf",Ua="opera",Va="createElement",Wa="addEventListener",K="setAttribute",Xa="play",Ya="type",cb="attachEvent",db="source",eb="changedTouches",fb="zIndex",gb="getElementsByTagName",hb="clientX",lb="clientY",M="length",mb="className",nb="setTimeout",ob="removeEventListener",pb= 3 | "ctrlKey",qb="duration",rb="userAgent",sb="location",O="hasOwnProperty",tb="touches",Z="style",Cb="pause",Db="body",Eb="removeChild",Fb="search",Gb="options",Hb="start",Ib="getAttribute",Jb="detachEvent",Kb="element",Lb="startTime",Mb="removeAttribute",Sb="parentNode",Tb="display",Ub="height",Vb="splice",Wb="event",$="",Xb="\n",Yb=" ",Zb=" -webkit-gradient(linear,left top,left bottom,from(transparent),color-stop(",$b=" : ",ac=" rotate(",bc=" rotateX(",cc=" rotateY(",dc=" scaleX(",ec=" scaleY(",fc= 4 | " translateX(",gc=" translateY(",hc=" translateZ(",ic="#",jc="%",kc="%27",Bc="&",Cc=" ",Dc="')",Ec="(",Fc=")",Gc=") ",Hc=",",Ic=",transparent),to(rgba(255,255,255,.5)))",Jc="-bottom",Kc="-moz-",Lc="-moz-perspective",fd="-ms-transform",gd="-ms-transform-origin",hd="-o-",id="-webkit-",jd="-webkit-perspective",kd="-webkit-tap-highlight-color",ld="-webkit-transform",md=".HYPE_element{-webkit-transform:rotateY(0);}video.HYPE_element{-webkit-transform:none;}^{color:#000;`size:16px;`weight:normal;`family:Helvetica,Arial,Sans-Serif;`weight:normal;`style:normal;`variant:normal;text-decoration:none;text-align:left;text-transform:none;text-indent:0;text-shadow:none;line-height:normal;letter-spacing:normal;white-space:normal;word-spacing:normal;@:baseline;border:none;background-color:transparent;background-image:none;-webkit-`smoothing:antialiased;-moz-backface-visibility:hidden;}", 5 | nd=".HYPE_scene ",od="/",pd="0",qd="0.0",rd="0123456789ABCDEF",sd="1",td="1.0",ud="100%",vd="600px",wd=": ",xd="",yd="",zd="
",Ad="",Bd='