在 Smalltalk 中构建和查询示例通用树
使用 SqueakSource 上的 BTree 包(或您可能知道的任何其他包)并给出以下树,
-Root
|
--Node1
|
--Node2
---Node21
---Node22
---Node23
----Node231
----Node232
-----Node2321
----Node233
|
--Node3
我尝试编写以下内容但没有成功:
- 构建树
- 给定 Node23 回答其直接子节点
- 给定 Node2 回答其所有子节点
- 给定任何节点,回答它的父级
- 收集所有子级
这不是家庭作业,我之所以要求一个基本示例是因为在 BTree 的情况下,文档几乎不存在,并且测试不足以弄清楚基本用法,实际上是示例混合有断言并使用便捷方法。
Using the BTree package at SqueakSource (or any other you may know) and given the following tree
-Root
|
--Node1
|
--Node2
---Node21
---Node22
---Node23
----Node231
----Node232
-----Node2321
----Node233
|
--Node3
I've tried to write the following but without success:
- Build the tree
- Given Node23 answer its direct children
- Given Node2 answer all its children
- Given any Node, answer its parent
- Collect all the children
This is NOT homework, the reason why I'm asking for a basic example is because in the case of BTree, documentation is almost inexistent, and tests are not good enough for figuring out basic usage, actually examples are mixed with asserts and using convenience methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不太关心执行速度,SqueakSource 中有一个用于此目的的广泛记录的包 http:// /www.squeaksource.com/TreeLW.html 最初是为 VisualWorks 发布的。
您可以用来构建树的一个类是 SKPVTreeLW,它支持值、子树、超树和键。您可能会实现这样的示例:
If you're not so concerned with execution speed, there is an extensively documented package in SqueakSource for that purpose http://www.squeaksource.com/TreeLW.html which was originally released for VisualWorks.
One class you may use for building your tree is SKPVTreeLW, which supports value, subtrees, supertree, and a key. You may achieve your example implementing something like this:
B-Tree 是一种将键与值关联起来的排序数据结构。 B 树允许您处理大量数据并保证在对数时间内搜索、插入和删除。 SqueakSource 上的 BTree 包 遵循 Smalltalk
Dictionary
协议:at:
和at:ifAbsent:
用于搜索给定键的值,at:put:
用于插入键和值对,以及removeKey:
用于删除一个key。此外,还支持您从
Dictionary
中了解到的所有迭代器函数(do:
、keysDo:
、valuesDo:
、<代码>keysAndValuesDo:, ...);以及其他一些迭代键范围的方法(from:do:
、from:to:do:
、upTo:do:
,...)。通常,您的应用程序代码中不需要 B-Tree 集合,除非您的内置Dictionary
类存在性能问题。在我看来,你试图修改 B 树的内部工作原理。您不应该这样做,
BTree
类会自动重新组织自身以始终提供最有效的表示(这主要是测试验证的)。如果您想管理自己的树,为什么不创建自己的Node
类,其中包含子节点的OrderedCollection
和父链接?A B-Tree is a sorted data structure associating keys with values. B-Trees allow you to handle huge amount of data and guarantee searches, insertions, and deletions in logarithmic time. The BTree Package on SqueakSource follows the protocol of a Smalltalk
Dictionary
:at:
andat:ifAbsent:
are used to search for values given a key,at:put:
is used to insert a key and value pair, andremoveKey:
is used to delete a key.Furthermore, all iterator functions you know from
Dictionary
are supported (do:
,keysDo:
,valuesDo:
,keysAndValuesDo:
, ...); as well as a few more to iterate over ranges of keys (from:do:
,from:to:do:
,upTo:do:
, ...). Normally you shouldn't need B-Tree collections in your application code, unless you have a performance problem with the built-inDictionary
class.It seems to me that you try to modify the inner workings of a B-Tree. You shouldn't do that, the
BTree
class reorganizes itself automatically to always provide the most efficient representation (this is mostly what the tests verify). If you want to manage your own tree, why not create your ownNode
class containing anOrderedCollection
of the child nodes and a parent link?