简体   繁体   中英

Swift: How does this create a binary tree?

So I am following an online tutorial and I understand everything except this part. The person explains to me this creates a binary tree so to speak where one item is linked to two items. In this case a page is linked to two pages. I don't understand how this works in this example where struct Adventure creates the binary tree. Any help will be appreciated. Currently, I feel really bad for not understanding this at all.

import Foundation

class Page {
    let story: Story

    typealias Choice = (title: String, page:Page)

    var firstChoice: Choice?
    var secondChoice: Choice? 

    init(story: Story) {
        self.story = story
    }
}

    extension Page {
// adds the page
        func addChoiceWith(title: String, story:Story) -> Page {
            let page = Page(story:story)
            return addChoiceWith(title: title, page: page)
        }

// creates branches
        func addChoiceWith(title: String, page: Page) -> Page {
            switch (firstChoice, secondChoice) {
            case (.some, .some) : return self
            case (.none, .none), (.none, .some): firstChoice = (title, page)
            case (.some, .none): secondChoice = (title,page)

            }

            return page
        }
    }


    struct Adventure {
        static var story: Page {
            let returnTrip = Page(story: .returnTrip)
            let touchdown = returnTrip.addChoiceWith(title: "Stop and Investigate", story: .touchDown)
            let homeward = returnTrip.addChoiceWith(title: "Continue home to Earth", story: .homeward)
            let rover = touchdown.addChoiceWith(title: "Explore the Rover", story: .rover)
            let crate = touchdown.addChoiceWith(title: "Open the Crate", story: .crate)

            homeward.addChoiceWith(title: "Head back to Mars", page: touchdown)
            let home = homeward.addChoiceWith(title: "Continue Home to Earth", story: .home)

            let cave = rover.addChoiceWith(title: "Explore the Coordinates", story: .cave)
            rover.addChoiceWith(title: "Return to Earth", page: home)

            cave.addChoiceWith(title: "Continue towards faint light", story: .droid)
            cave.addChoiceWith(title: "Refill the ship and explore the rover", page: rover)

            crate.addChoiceWith(title: "Explore the Rover", page: rover)
            crate.addChoiceWith(title: "Use the key", story: .monster)

            return returnTrip
        }
    }

Each Page represents a node in the tree and each Choice represents branch. A Story is like the content of the node. firstChoice.page and secondChoice.page represents the two children of a node.

The Adventure.story property creates a tree and returns the root node. You can access all other nodes using the root:

Adventure.story.firstChoice.page.secondChoice.page

Now, let's look at how the tree is constructed. Note that this isn't a standard binary tree that you see all the time. Some of the nodes have their parents as children. But each node does have two children.

I will turn the code into pseudocode. You can follow this code, and on a piece of paper, draw the tree yourself.

Create a node called "returnTrip"
Add a child to returnTrip called "touchDown"
Add a child to returnTrip called "homeward"
Add a child to touchDown called "rover"
Add a child to touchDown called "crate"
Connect homeward back to touchDown
Add a child to homeward called "home"
Add a child to rover called "cave"
Connect rover to home
Add a child to cave called "droid"
Connect cave to rover
Connect crate to rover
Add a child to crate called "monster"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM