如何引用小孩在树上数以百万计的节点 [英] How to refer to children in a tree with millions of nodes

查看:172
本文介绍了如何引用小孩在树上数以百万计的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个树,其中每个节点都可以有子节点的数目不详。树是有过在实践中万个节点。

I'm attempting to build a tree, where each node can have an unspecified amount of children nodes. The tree is to have over a million nodes in practice.

我已经设法去构造树,但我遇到由于一个完整的堆内存错误,当我充满几千个节点的树。这样做的原因是因为我试图存储每个节点的孩子在字典的数据结构(或任何数据结构为此事)。正在创建因此,在运行时我有数千个这样的数据结构,因为每个节点都可以有孩子的一个未指定量,并且每个节点的子节点都将存储在此数据结构中。

I've managed to contruct the tree, however I'm experiencing memory errors due to a full heap when I fill the tree with a few thousand nodes. The reason for this is because I'm attempting to store each node's children in a Dictionary data structure (or any data structure for that matter). Thus, at run-time I've got thousands of such data structures being created since each node can have an unspecified amount of children, and each node's children are to be stored in this data structure.

是否有这样做的另一种方式?我不能简单地用一个变量来存储儿童的参考,因为有可能是孩子的每个节点的数目不详。因此,它不像一个二叉树在那里我可以有2个变量分别跟踪的左孩子和右孩子。

Is there another way of doing this? I cannot simply use a variable to store a reference of the children, as there can be an unspecified amount of children for each node. THus, it is not like a binary tree where I could have 2 variables keeping track of the left child and right child respectively.

请没有建议这样做的另一种方法。我有我的原因,需要创建这棵树,可惜我不能这样做。

Please no suggestions for another method of doing this. I've got my reasons for needing to create this tree, and unfortunately I cannot do otherwise.

谢谢!

推荐答案

您如何节点的很多人将叶子节点?也许只有创建数据结构来存储的孩子,当你第一次的有无的孩子,否则,保持空引用。

How many of your nodes will be "leaf" nodes? Perhaps only create the data structure to store children when you first have a child, otherwise keeping a null reference.

除非你的需要的查找孩子作为一个地图,我会使用一个名单,其中,T> (用适当的容量初始化)而不是词典<,> 为孩子们。这听起来像你可能有更多的需求比你虽然解释,这使得它很难说。

Unless you need to look up the children as a map, I'd use a List<T> (initialized with an appropriate capacity) instead of a Dictionary<,> for the children. It sounds like you may have more requirements than you've explained though, which makes it hard to say.

我很惊讶你只有几千个节点失败后,虽然 - 你应该能够有问题之前创建一个pretty的大量对象

I'm surprised you're failing after only a few thousand nodes though - you should be able to create a pretty large number of objects before having problems.

我也想建议,如果你认为你会最终使用了大量的内存,确保你在64位机器上,并确保您的应用程序本身被设置为64位。 (这可能只是一个简单封装在一个类库,这是很好的,只要类库被设置为64位或值为anycpu。)

I'd also suggest that if you think you'll end up using a lot of memory, make sure you're on a 64-bit machine and make sure your application itself is set to be 64-bit. (That may just be a thin wrapper over a class library, which is fine so long as the class library is set to be 64-bit or AnyCPU.)

这篇关于如何引用小孩在树上数以百万计的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆