嵌套数字列表的c++数据结构
C++ Data Structure for Nested Number Lists
我需要一个有效的c++数据结构实现,可以存储嵌套的数字列表,例如:
0:
1:
3
4
7
5:
10
13
15
7:
2
1:
1:
2
3
6:
7
9
我希望能够以一种非常有效的方式循环遍历最深处的元素,这样我就可以按照它们在嵌套列表中出现的顺序访问由三个数字组成的集合:
(0,1,3)
(0,1,4)
(0,5,10)
...
我还希望通过传递一组三个数字并在树的每一层添加适当的数字来向树添加元素。我认为我应该使用某种类型的树数据结构,但不知道哪种是最有效的。
最后,我想将一个值与每个"叶子"关联起来,这样每个三元组将映射到某个整数值。
对于这种情况,Trie可能是最有效的结构。你可以在这里找到关于Trie的概述。
本质上,Trie的存储值可能在值的早期有很多重叠(如字符串或数字序列),每个值只在每个位置存储一次。
很难说你到底想要什么。
一个快速的解决方案就是std::map<std::tuple<int, int, int>, ValueType>
,而在你的例子中,ValueType
就是int
。
或者,您可以这样做:
class Node
{
//optional - depends on whether you need it:
std::weak_ptr<Node> parent;
//for the children, you might want this (if they are numbered 0, 1, ...)
std::vector<std::unique_ptr<Node>> children;
//or instead you might want this for the children
//(if they are numbered with potential gaps, like in your example):
std::map<int, std::unique_ptr<Node>> children;
};
class LeafNode : Node
{
ValueType data; //ValueType is whatever you want
}
这很像给树加上后缀。但是哪一个对你更方便…解决你。
相关文章:
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 带参数的数据结构的全局声明