实现代码如下:

结点数据结构:package com.test.forest; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Random; public class SerPath implements Serializable{ private int fakePath = 0; public int layerIndex = -1; public List childList = new ArrayList<>(); public SerPath(int index){ fakePath = index; } public int getPath(){ return fakePath; } }

遍历流程:package com.test.forest; import java.util.ArrayList; import java.util.List; import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; public class Forest { /**构造测试样本**/ public List createForest() { /**森林表**/ List forestTreeHeadList = new ArrayList<>(); /**第一棵树**/ SerPath treeHead1 = new SerPath(1); treeHead1.childList.add(new SerPath(2)); treeHead1.childList.add(new SerPath(3)); treeHead1.childList.add(new SerPath(4)); treeHead1.childList.add(new SerPath(5)); treeHead1.childList.get(2).childList.add(new SerPath(6)); treeHead1.childList.get(2).childList.add(new SerPath(7)); treeHead1.childList.get(2).childList.add(new SerPath(8)); treeHead1.childList.get(2).childList.add(new SerPath(9)); forestTreeHeadList.add(treeHead1); /**第2棵树**/ SerPath treeHead2 = new SerPath(10); treeHead2.childList.add(new SerPath(11)); treeHead2.childList.add(new SerPath(12)); treeHead2.childList.add(new SerPath(13)); treeHead2.childList.get(0).childList.add(new SerPath(14)); treeHead2.childList.get(0).childList.add(new SerPath(15)); treeHead2.childList.get(0).childList.add(new SerPath(16)); treeHead2.childList.get(2).childList.add(new SerPath(17)); treeHead2.childList.get(2).childList.add(new SerPath(18)); forestTreeHeadList.add(treeHead2); return forestTreeHeadList; } /**遍历森林中的所有树和树节点**/ public void readForest(List forestTreeHeadList) { for(SerPath treeHead : forestTreeHeadList){ Queue> layerQueue = new LinkedBlockingQueue<>(); //输出头结点的值: System.out.println(treeHead.getPath()); //发现头结点有孩子表,表入队: if(!treeHead.childList.isEmpty()){ layerQueue.add(treeHead.childList); } //只要孩子表队列不为空就循环 while(!layerQueue.isEmpty()){ List nodeList = layerQueue.poll(); //出队孩子表表头 for(SerPath childNode : nodeList){ //遍历当前孩子表,如果孩子节点也有孩子表,则将这个孩子节点的孩子表放入队尾等候遍历 System.out.println(childNode.getPath()); //读孩子数据 if(!childNode.childList.isEmpty()) { //如果孩子节点也有孩子表,则将这个孩子节点的孩子表放入队尾等候遍历 layerQueue.add(childNode.childList); } } } } } public static void main(String[] args) { Forest forest = new Forest(); forest.readForest(forest.createForest()); } }

实际输出:

利用序列化保存再读取的测试:package com.test.forest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; public class Forest { /**构造测试样本**/ public List createForest() { /**森林表**/ List forestTreeHeadList = new ArrayList<>(); /**第一棵树**/ SerPath treeHead1 = new SerPath(1); treeHead1.childList.add(new SerPath(2)); treeHead1.childList.add(new SerPath(3)); treeHead1.childList.add(new SerPath(4)); treeHead1.childList.add(new SerPath(5)); treeHead1.childList.get(2).childList.add(new SerPath(6)); treeHead1.childList.get(2).childList.add(new SerPath(7)); treeHead1.childList.get(2).childList.add(new SerPath(8)); treeHead1.childList.get(2).childList.add(new SerPath(9)); forestTreeHeadList.add(treeHead1); /**第2棵树**/ SerPath treeHead2 = new SerPath(10); treeHead2.childList.add(new SerPath(11)); treeHead2.childList.add(new SerPath(12)); treeHead2.childList.add(new SerPath(13)); treeHead2.childList.get(0).childList.add(new SerPath(14)); treeHead2.childList.get(0).childList.add(new SerPath(15)); treeHead2.childList.get(0).childList.add(new SerPath(16)); treeHead2.childList.get(2).childList.add(new SerPath(17)); treeHead2.childList.get(2).childList.add(new SerPath(18)); forestTreeHeadList.add(treeHead2); return forestTreeHeadList; } /**遍历森林中的所有树和树节点**/ public void readForest(List forestTreeHeadList) { for(SerPath treeHead : forestTreeHeadList){ Queue> layerQueue = new LinkedBlockingQueue<>(); //输出头结点的值: System.out.println(treeHead.getPath()); //发现头结点有孩子表,表入队: if(!treeHead.childList.isEmpty()){ layerQueue.add(treeHead.childList); } //只要孩子表队列不为空就循环 while(!layerQueue.isEmpty()){ List nodeList = layerQueue.poll(); //出队孩子表表头 for(SerPath childNode : nodeList){ //遍历当前孩子表,如果孩子节点也有孩子表,则将这个孩子节点的孩子表放入队尾等候遍历 System.out.println(childNode.getPath()); //读孩子数据 if(!childNode.childList.isEmpty()) { //如果孩子节点也有孩子表,则将这个孩子节点的孩子表放入队尾等候遍历 layerQueue.add(childNode.childList); } } } } } /**前序遍历、深度优先遍历**/ public void readForestDepthFirst(List forestTreeHeadList){ if(forestTreeHeadList == null || forestTreeHeadList.isEmpty()) return; for(SerPath path : forestTreeHeadList) { System.out.println(path.getPath()); readForest(path.childList); } } public static void main(String[] args) { Forest forest = new Forest(); List forestTreeHeadList = forest.createForest(); //广度遍历: forest.readForest(forestTreeHeadList); System.out.println(); try { File file = new File("E:\\1.temp"); if(!file.exists()){ file.createNewFile(); } FileOutputStream outputStream = new FileOutputStream(file); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(forestTreeHeadList); FileInputStream inputStream = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); List reLoad = (List) objectInputStream.readObject(); //读取之后广度遍历第二次: forest.readForest(reLoad); //读取之后深度遍历: System.out.println(); forest.readForestDepthFirst(reLoad); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

测试结果:

java 存储多叉树_JAVA多叉树森林的构造、内存存储与遍历相关推荐

  1. java字符串表表容量_java – 我可以使用什么符号表来存储~50 mil的字符串,快速查找而不会耗尽堆空间?...

    我有一个约5000万字符串的文件,我需要在启动时添加到某种符号表中,然后以合理的速度搜索几次. 我尝试使用DLB trie,因为查找会相对较快,因为所有字符串都是< 10个字符,但在填充DLB时 ...

  2. java 二维矩阵_Java如何输入二维矩阵并存储到二维数组中

    展开全部 最不严谨的写法(但是可62616964757a686964616fe59b9ee7ad9431333335326163用)下列方法可以不需知道二维数组的宽度和长度,但是不能直接保存,而是输入 ...

  3. java 对象 线程安全_JAVA并发编程学习:构造线程安全的对象

    设计线程安全的类 实例限制 当一个对象被另一个对象封装时,所有访问被被封装对象的代码路径就是全部可知的,这相比于让对象可被整个系统访问来说,更容易对代码路径进行分析.将数据封装在对象内部,把对数据的访 ...

  4. java中二叉树_java后端学习路线

    "学习真的是一个循序渐进的过程,也是一件需要坚持的事情.对于这篇文章,不同阶段的人可能有不同的理解,所以我把我整理的比较全面的java学习路线分享给大家,以供参考.下面的这个学习路线主要针对 ...

  5. java 静态变量 存储_Java学习笔记9---类静态成员变量的存储位置及JVM的内存划分...

    笔记8提到了类静态成员变量的访问方式,但静态成员变量存储在哪里呢?在网上查阅不少资料,发现好多内容都是过时的了,其中主流观点是静态成员变量存放在方法区.JDK8之前,静态成员变量确实存放在方法区:但J ...

  6. java浮点数存储方式_Java浮点数内存存储

    转自: 1.小数的二进制表示问题 首先我们要搞清楚下面两个问题: (1)  十进制整数如何转化为二进制数 算法很简单.举个例子,11表示成二进制数: 11/2=5   余   1 5/2=2   余  ...

  7. Java运行作业控制语言_Java安全——语言本身的设计

    Java安全--语言本身的设计 Java 安全 内在安全机制 Java语言本身的安全机制是要保护内存资源--保证内存完整性,核心的安全特性要确保程序不能非法解析或修改驻留在内存中的机密信息.从语言本身 ...

  8. java异常处理试题答案_Java 面试题和答案 - (下)

    第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的常见问题,Java的集合类,垃圾收集器,本章主要讨论异常处理,Java小应用程序,Swing,JDBC,远程方法调用(RMI),Servle ...

  9. java模拟银行存取_JAVA基础案例 模拟银行存取款业务

    模拟银行存取款业务 编写一个Java应用程序,模拟网上银行登录及存取款业务.登录时需判断银行卡号和银行卡密码,当输入的卡号和密码都正确时,登录成功,提示当前登录的账户名,并进入下一步选择操作类型.操作 ...

  10. java double储存原理_Java内存分配原理

    Java内存分配与管理是Java的核心技术之一,一般Java在内存分配时会涉及到以下区域: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 ...

最新文章

  1. API 2.0Switching Basemaps
  2. MATLAB 使用CNN拟合回归模型预测手写数字的旋转角度(卷积神经网络)
  3. SAP CRM WebClient UI根据扩展字段搜索出结果的实现原理
  4. 网页设计html最简单水印方法,前端canvas水印快速制作(附完整代码)
  5. Linux Shell 常用命令与目录分区的学习总结
  6. hashcode java_java 的Object类的hashcode()方法具体是怎么实现的?
  7. 建立一颗二叉排序树,并删除该二叉排序树上的某个节点
  8. java nio 缓冲区(一)
  9. Hbase的安装与测试
  10. python3中浮点数float的四舍五入,round跟decimal区别
  11. Html meta 标签定义页面元信息 详解
  12. GSM/CDMA区别
  13. 熊猫烧香被恶搞,网友爆笑诗词句大集合
  14. 优秀工程师应该具备哪些素质_作为现代工程师应具备的素养
  15. HDU6194 后缀数组
  16. Windows微信文本压缩算法分析
  17. 【解决报错】java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
  18. GamingAnywhere 一:GA初览
  19. 案例,使用封装实现电子宠物系统的企鹅类正确输入健康值和亲密度
  20. ansa打开catia文件_ANSA软件介绍.doc

热门文章

  1. IDEA 使用和问题总结
  2. Flutter高级第3篇:底部 Tab 切换保持页面状态的几种方法
  3. Android 主人VS宠物 游戏
  4. LeetCode 973. K Closest Points to Origin
  5. 吴恩达深度学习笔记 第二章作业1
  6. Windows下将jar包封装成服务程序
  7. 仿XP系统的任务栏菜单
  8. inittab 分析
  9. sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml
  10. 2014年辛星starphp第一节设置入口文件以及App类