c# - tree data structure with data contained in leaf nodes and all other nodes having just a node name
问题描述
Need to implement a 3-level tree with labels for levels 1 and 2 and data in level 3. Need to be able to add nodes, remove nodes, traverse, and search. How would you implement it?
Could a regular tree with two data slots for node name and node data work? i.e. for all the leaf nodes the node name would be blank and node data would be filled, and vice versa for the non-leaf nodes. I feel like this would be a bit wasteful of memory though.
解决方案
I feel like this would be a bit wasteful of memory though.
Agreed. Coming from an assembly language programming background decisions like these troubled me a lot :)
However, here definition of wasteful depends upon two factors -
- How much data you are talking about
- Where are you planning to run the program (Laptop/Mobile/RPi/low memory embedded system/...)
For simplicity lets consider node data is of type int
(32 bits). Now to fill even 100 KB of memory you'll need have 25,600 entries in your tree.
Now if we talk about not wasting any memory, then we will end up writing two set of nodes, one with only node name and one with only node data. And more code to add nodes, remove nodes, traverse, and search.
I hope this answers your query.
推荐阅读
- azure-functions - Azure Sql 专用链接阻止访问存储帐户
- kotlin - 将一个接口委托给多个实现
- oracle - 与多行选择合并
- git - Git 无法从索引中检出 PDF 文件
- json - JOLT 转换 - 提取字段并将其置于顶层
- c++ - OpenGL:如何实现相机后视图
- amazon-web-services - AWS Cli:如何将错误捕获到变量而不是在控制台中打印
- python - 如何纠正 replace() 中出现的这个问题?Python
- android - 我的 android xml 代码没有正确显示两个文本
- c# - 如何在 Gmail API 中获取整个电子邮件文本/正文/内容?C#