简体   繁体   English

在Java中存储树结构

[英]Storing a tree structure in java

I want to pre-compute some moves for my Game AI and store them on the disk, to be loaded at runtime. 我想为我的游戏AI预先计算一些动作并将它们存储在磁盘上,以便在运行时加载。 The moves are stored in a quinary tree and I'd appreciate it if you could recommend me some libraries that could let me to easily save them and restore them. 这些动作存储在一棵五叶树中,如果您可以向我推荐一些可以让我轻松保存并恢复它们的库,我将不胜感激。

使用Java的序列化机制来持久化树是最简单的解决方案,并使用ObjectInputStream读取/ ObjectOutputStream来从磁盘写入序列化数据/将磁盘写入序列化数据。

As mentioned in the previous answers, you can make your tree implement the Serializable and let the JVM do everything for you (make sure all node classes are Serializable as well) 如前面的答案中所述,您可以使树实现Serializable并让JVM为您做所有事情(确保所有节点类也都可以序列化)

public class Tree implements Serializable{
   Node rootNode = ...

}

If your tree contains a large amount of data then you could probably trim down all the data that can be re-calculated when the tree is reloaded from disk, have a look at the Momento design pattern to get an idea on how this can be acheived. 如果您的树包含大量数据,那么您可能会修剪掉从磁盘重新加载树时可以重新计算的所有数据,请查看Momento设计模式以了解如何实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM