Swift: 这如何创建一个二叉树?
Swift: How does this create a binary tree?
所以我正在按照在线教程学习,除了这部分我什么都懂。该人向我解释说,这会创建一个二叉树,可以说一个项目链接到两个项目。在这种情况下,一个页面链接到两个页面。我不明白在这个例子中这是如何工作的,其中 struct Adventure
创建了二叉树。任何帮助将不胜感激。目前,我为完全不了解这一点感到非常难过。
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
}
}
每个Page
代表树中的一个节点,每个Choice
代表分支。一个Story
就像节点的内容。 firstChoice.page
和secondChoice.page
表示一个节点的两个children。
Adventure.story
属性 创建树和 returns 根节点。您可以使用根访问所有其他节点:
Adventure.story.firstChoice.page.secondChoice.page
现在,让我们看看树是如何构建的。请注意,这不是您一直看到的标准二叉树。一些节点的 parents 为 children。但是每个节点确实有两个children.
我会把代码变成伪代码。你可以按照这个代码,在一张纸上,自己画出这棵树。
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"
所以我正在按照在线教程学习,除了这部分我什么都懂。该人向我解释说,这会创建一个二叉树,可以说一个项目链接到两个项目。在这种情况下,一个页面链接到两个页面。我不明白在这个例子中这是如何工作的,其中 struct Adventure
创建了二叉树。任何帮助将不胜感激。目前,我为完全不了解这一点感到非常难过。
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
}
}
每个Page
代表树中的一个节点,每个Choice
代表分支。一个Story
就像节点的内容。 firstChoice.page
和secondChoice.page
表示一个节点的两个children。
Adventure.story
属性 创建树和 returns 根节点。您可以使用根访问所有其他节点:
Adventure.story.firstChoice.page.secondChoice.page
现在,让我们看看树是如何构建的。请注意,这不是您一直看到的标准二叉树。一些节点的 parents 为 children。但是每个节点确实有两个children.
我会把代码变成伪代码。你可以按照这个代码,在一张纸上,自己画出这棵树。
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"