机构树遍历

tech2022-07-08  199

scala代码如下

import scala.collection.mutable.ListBuffer object 机构树 { def main(args: Array[String]): Unit = { val root = new Node(1, "aa", 0) val node2 = new Node(2, "cc", 1) val node3 = new Node(3, "ds", 1) val node4 = new Node(4, "tts", 1) val node5 = new Node(5, "kk", 2) val node8 = new Node(8, "cc", 2) val node9 = new Node(9, "jj", 5) val node10 = new Node(10, "wf", 3) root.sub.append(node2) root.sub.append(node3) root.sub.append(node4) node2.sub.append(node5) node2.sub.append(node8) node3.sub.append(node10) node5.sub.append(node9) val tree = new Tree tree.root = root tree.postOrder() } } class Node(inputid: Int, inputname: String, inputparent_id: Int) { var sub = new ListBuffer[Node] val name: String = inputname var parent_id = inputparent_id var id: Int = inputid def postOrder: Unit = { if (this.sub!=null) this.sub.foreach(_.postOrder) } println(s"当前节点id=$id,name=$name") } class Tree { var root: Node = null def postOrder(): Unit = { if (root != null) { root.postOrder } else { printf("当前Tree为空,不能遍历") } } }

最新回复(0)