我有一个如下所示的树结构实现:
class Node {
let value: String
var parentNode: Node?
var childenNode = [Node]()
func appendNode(node: Node) {
childenNode.append(node)
node.parentNode = self.parentNode
}
func isLeaveNode(node: Node) -> Bool{
if node.childenNode.isEmpty {
return true
} else {
return false
}
}
init(value: String) {
self.value = value
}
}
我想要一个函数返回到达当前节点所需的路径。例如:假设我有一个离开节点node
,并且到达该离开节点所用的路径是Main Menu -> Setting -> User Options -> Set User Options
,那么我想要一个返回该路径的函数:例如。 func path(node: Node) -> path
。我该如何实现?
我尝试使用for-in
循环遍历父节点。但是,由于Node
不符合sequence
协议,因此无法完成。
谢谢堆!任何帮助将不胜感激!
答案 0 :(得分:1)
您可以使用clean
定义一个递归遍历节点的函数,直到它到达根目录为止:
parentNode
我使用以下示例对其进行了测试:
func getPath()->[Node]{
return self.parentNode == nil ? [self] : [self] + self.parentNode!.getPath()
}
函数中的三元运算符只是
的简写let grandParent = Node(value: "grand")
let parent = Node(value: "parent")
parent.parentNode = grandParent
let child = Node(value: "child")
child.parentNode = parent
child.getPath() //returns [grandParent, parent, child]
func getPath()->[Node]{
if self.parentNode == nil {
return [self]
} else {
return [self] + self.parentNode!.getPath() //this is the recursive call
}
}
表示我们找到了根节点,所以这是递归调用的基本情况,我们只需要返回当前值。如果self.parentNode == nil
,则表示我们处于子节点,因此返回值将是当前节点的数组+递归调用的结果。
答案 1 :(得分:0)
如果递归是新的,您也可以尝试迭代方法。按照大卫的回答。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.saurinpatel</groupId>
<artifactId>WayneTestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WayneTestProject</name>
<description>Testing Communication with Wayne Box and Front end</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
</configuration>
</plugin>
</plugins>
</build>
</project>