我有一棵树,它有很多节点(数百万+),需要加载到内存中。因此,我需要最有效的方式将节点及其关系存储在内存中。最好的数据结构是什么?到目前为止,我有两个选择:
//more obvious but the less efficient
class TreeNode
{
Node parent;
TreeNode[] children;
//additional fields
byte X;
byte Y;
byte marker;
string comment;
}
//more efficient
class TreeNode
{
TreeNode next; //reference to the next child of parent node,
//if isLast=true - reference to parent node
TreeNode firstChild; //reference to the first child of this node
bool isLast; //true, if this node is the last parents child
//additional fields
byte X;
byte Y;
byte marker;
string comment;
}
请注意,我需要在这棵树上执行诸如浏览、删除和插入之类的操作,并且我需要这些操作足够快。
编辑:这种情况下的最佳选择是使用更少的 RAM 来存储整个树。第二个标准是快速删除、浏览和插入操作——它们不应该比我在上面写的数据结构中花费更多的时间。我不能更严格地制定这个标准