Scala实现树形结构

  1 package graphx
  2 
  3 import java.util.ArrayList
  4 import java.util.List
  5 
  6 /**
  7   * Created by zhen on 2018/12/28.
  8   */
  9 object TreeParent {
 10 
 11   class Node {
 12     var id: String = ""
 13     var parent: Int = 0
 14 
 15     def this(id: String) {
 16       this()
 17       this.id = id
 18     }
 19 
 20     def this(id: String, parent: Int) {
 21       this()
 22       this.id = id
 23       this.parent = parent
 24     }
 25 
 26     override def toString: String = {
 27       return "TreeParent$Node [id=" + id + ", parent=" + parent + "]"
 28     }
 29   }
 30 }
 31 
 32 class TreeParent {
 33   private val list : List[TreeParent.Node] = new ArrayList[TreeParent.Node]()
 34   private var nodeNums: Int = 0
 35 
 36   /**
 37     * 指定根节点创建树
 38     */
 39   def this(id: String) {
 40     this()
 41     list.add(new TreeParent.Node(id, -1))
 42     nodeNums += 1
 43   }
 44 
 45   /**
 46     * 指定根节点、指定treeSize创建树
 47     */
 48   def this(id: String, treeSize: Int) {
 49     this()
 50     list.add(new TreeParent.Node(id, -1))
 51     nodeNums += 1
 52   }
 53 
 54   /**
 55     * 获取某个节点的索引值
 56     */
 57   def getNodeIndex(node: TreeParent.Node): Int = {
 58     for(i<- 0 until list.size())
 59     {
 60       if (list.get(i) eq node) {
 61         return i
 62       }
 63     }
 64     return -1
 65   }
 66 
 67   /**
 68     * 为指定节点添加子节点
 69     */
 70   def addNode(id: String, parent: TreeParent.Node) {
 71     list.add(new TreeParent.Node(id, getNodeIndex(parent)))
 72   }
 73 
 74   /**
 75     * 判断树是否为空
 76     */
 77   def isEmpty: Boolean = {
 78     return list.get(0) == null
 79   }
 80 
 81   /**
 82     * 返回根节点
 83     */
 84   def getRoot: TreeParent.Node = {
 85     return list.get(0)
 86   }
 87 
 88   /**
 89     * 返回指定节点(非根节点)的父节点
 90     */
 91   def getParent(node: TreeParent.Node): TreeParent.Node = {
 92     return list.get(node.parent)
 93   }
 94 
 95   def getNodeByid(id: String): TreeParent.Node = {
 96     for(i<- 0 until list.size())
 97     {
 98       if (list.get(i).id eq id) {
 99         return list.get(i)
100       }
101     }
102     System.out.println("树中不存在包含该数据的节点")
103     return null
104   }
105 
106   /**
107     * 返回指定节点(非叶子节点)的所有子节点
108     */
109   def getChildren(parent: TreeParent.Node): List[TreeParent.Node] = {
110     val childrenList : List[TreeParent.Node] = new ArrayList[TreeParent.Node]()
111     for(i <- 0 until list.size()){
112       if (list.get(i) != null && list.get(i).parent == getNodeIndex(parent)) {
113         childrenList.add(list.get(i))
114       }
115     }
116     return childrenList
117   }
118 }
119 
120 object TreeAI {
121   def main(args: Array[String]) {
122     val tree: TreeParent = new TreeParent("root")
123     val root: TreeParent.Node = tree.getRoot
124     System.out.println(root)
125     tree.addNode("A", root)
126     tree.addNode("B", root)
127     val A: TreeParent.Node = tree.getNodeByid("A")
128     val B: TreeParent.Node = tree.getNodeByid("B")
129     tree.addNode("C", A)
130     tree.addNode("D", A)
131     tree.addNode("E", B)
132     tree.addNode("F", B)
133     tree.addNode("G", B)
134     tree.addNode("H", root)
135     val H: TreeParent.Node = tree.getNodeByid("H")
136     tree.addNode("I", H)
137     val list: List[TreeParent.Node] = tree.getChildren(root)
138     System.out.println("A节点的子节点为:")
139     import scala.collection.JavaConversions._
140     for (node <- list) {
141       System.out.println(node)
142     }
143   }
144 }

结果:

 

posted @ 2019-01-02 15:26  云山之巅  阅读(1116)  评论(0编辑  收藏  举报