树状数据结构(与 VirtualTreeview 一起使用)
我已经到了需要停止将数据存储在 VCL 组件中并拥有“底层数据结构”的地步,如 先生。 Rob Kennedy建议。
首先,这个问题是关于“我如何制作一个底层数据结构”。 :)
我的层次结构由 2 级节点组成。
现在,我通过循环根节点来遍历我的东西,其中我循环遍历根节点的子节点,以获得我需要的东西(数据)。我希望能够将所有数据存储在所谓的底层数据结构中,以便我可以使用线程轻松修改条目(我想我能够做到这一点?)
但是,当循环访问我的条目时(现在 ) ),结果取决于节点的 Checkstate - 如果我使用底层数据结构,我如何知道我的节点是否已检查,何时循环遍历我的数据结构而不是我的节点?
假设我想使用 2 个级别。
这将是父母:
TRoot = Record
RootName : String;
RootId : Integer;
Kids : TList; //(of TKid)
End;
和孩子:
TKid = Record
KidName : String;
KidId : Integer;
End;
这基本上就是我现在所做的。评论指出这不是最好的解决方案,所以我愿意接受建议。 :)
我希望你能理解我的问题。 :)
谢谢!
I have come to the point where I need to stop storing my data in a VCL component, and have an "underlying datastructure", as Mr. Rob Kennedy suggested.
First of all, this question is about "how do I make an underlying datastructure". :)
My hierachy consists of 2 levels of nodes.
Right now, I go thru my stuff by looping rootnodes, wherein I loop thru the rootnode's childnodes, to get what I need (Data). I would love to be able to store all my data in a so-called Underlying Datastructure, so that I can easily modify the entries using threads (I suppose I am able to do that?)
However, when looping through my entries (right now), the results are depending on the node's Checkstate - if I am using an underlying data structure, how do I know if my node is checked or not, when its my datastructure I loop thru, and not my nodes?
Let's say I wanted to use 2 levels.
This would be the Parent:
TRoot = Record
RootName : String;
RootId : Integer;
Kids : TList; //(of TKid)
End;
And the kid:
TKid = Record
KidName : String;
KidId : Integer;
End;
Thats basically what I do now. Comments state that this is not the best solution, so I am open to suggestions. :)
I hope you understand my question(s). :)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您请求的数据结构非常简单,非常简单,我建议使用 Windows 提供的
TTreeView
:它允许将文本和 ID 直接存储到树的节点中,无需额外的工作。尽管我建议使用更简单的
TTreeView
,但我仍将提供我对数据结构问题的看法。首先,我将使用类,而不是记录。在非常短的代码示例中,您以一种非常不幸的方式混合记录和类:当您复制TRoot
记录时(分配记录会生成完整的副本,因为记录始终被视为“值”) "),您没有对树进行“深层复制”:TRoot
的完整副本将包含与原始版本相同的Kids:TList
,因为类、与记录不同的是,引用:您正在处理引用的值。当您拥有带有对象字段的记录时,另一个问题是生命周期管理:记录没有析构函数,因此您需要其他机制来释放拥有的对象(
Kids :TList
)。您可以将TList
替换为Tkid 数组
,但是在传递怪物记录时您需要非常小心,因为您可能会在您最意想不到的时候结束对大量记录的深度复制。在我看来,最谨慎的做法是将数据结构基于类,而不是记录:类实例(对象)作为引用传递,因此您可以随意移动它们,而无需问题。您还可以获得内置的生命周期管理(析构函数)
基类将如下所示。您会注意到它可以用作 Root 或 Kid,因为 Root 和 Kid 共享数据:两者都有名称和 ID:
如果此类用作 Root,则需要一种方法来存储 Kids 。我假设您使用的是 Delphi 2010+,因此您有泛型。这个类,带有一个列表,看起来像这样:
您可能不会立即意识到这一点,但仅这个类就足以实现多级树!下面是一些用一些数据填充树的代码:
您需要一个递归过程来使用此类型填充虚拟树视图:
使用对象时,虚拟树中的不同节点可能具有与其关联的不同类型的对象。在我们的示例中,我们仅添加
TNode
类型的节点,但在现实世界中,您可能拥有TContact
、TContactCategory
、< code>TRecentCall,全部在一个 VT 中。您将使用is
运算符来检查 VT 节点中对象的实际类型,如下所示:以下是为什么将 VirtualNode 指针存储到节点实例的示例:
您知道有一个工作示例一个简单的树形数据结构。您需要“增长”此数据结构以满足您的需求:可能性是无限的!为了给您一些想法和探索方向:
Name:string
转换为虚拟方法GetText:string;virtual
,然后创建TNode 的专门后代
覆盖GetText
以提供专门的行为。TNode.AddPath(Path:string; ID:Integer)
,允许您执行Root.AddPath('Contacts\Abraham', 1);
- 也就是说,一种自动创建所有中间节点到最终节点的方法,以便轻松创建树。PVirtualNode
包含到TNode
本身中,以便您可以检查节点是否在虚拟树中“检查”。这将成为数据与 GUI 分离的桥梁。The data structure you're requesting is very simple, it's so simple I'd recommend using the windows-provided
TTreeView
: it allows storing the text and an ID straight into the tree's node with no additional work.Despite my recommendation to use the simpler
TTreeView
I'm going to provide my take on the data structure problem. First of all I'm going to use classes, not records. In your very short code sample you're mixing records and classes in a very unfrotunate way: When you make a copy of theTRoot
record (assigning records makes complete copies, because records are allways treated as "values"), you're not making a "deep copy" of the tree: The complete copy ofTRoot
will contain the sameKids:TList
as the original, because classes, unlike records, are references: you're coping the value of the reference.An other problem when you have a record with an object field is life cycle management: A record doesn't have an destructor so you'll need an other mechanism to free the owned object (
Kids:TList
). You could replace theTList
with anarray of Tkid
but then you'll need to be very careful when passing the monster record around, because you might end making deep copies of huge records when you least expect it.In my opinion the most prudent thing to do is to base the data structure on classes, not records: class instances (objects) are passed around as references, so you can move them around all you want with no problems. You also get built-in life cycle management (the destructor)
The base class would look like this. You'll notice it can be used as either the Root or the Kid, because both Root and Kid share data: The both have a name and an ID:
If this class is used as an Root, it needs a way to store the Kids. I assume you're on Delphi 2010+, so you have generics. This class, complete with a list, looks like this:
You might not immediately realize this, but this class alone is enough to implement a multi-level tree! Here's some code to fill up the tree with some data:
You need a recursive procedure to fill the virtual tree view using this type:
When using objects, different nodes in your Virtual Tree may have different types of objects associated with them. In our example we're only adding nodes of
TNode
type, but in the real world you might have nodes of typesTContact
,TContactCategory
,TRecentCall
, all in one VT. You'll use theis
operator to check the actual type of the object in the VT node like this:And here's an example why to store VirtualNode pointer to your node instances:
You know have an working example for a simple tree data structure. You'll need to "grow" this data structure to suite your needs: the possibilities are endless! To give you some ideas, directions to explore:
Name:string
into a virtual methodGetText:string;virtual
and then create specialized descendants ofTNode
that overrideGetText
to provide specialized behavior.TNode.AddPath(Path:string; ID:Integer)
that allows you to doRoot.AddPath('Contacts\Abraham', 1);
- that is, a method that automatically creates all intermediary nodes to the final node, to allow easy creation of the tree.PVirtualNode
intoTNode
itself so you can check rather the Node is "checked" in the Virtual Tree. This would be a bridge of the data-GUI separation.我相信您最好找到一个包含通用树实现的现有库,然后您可以重新使用它来满足您的需求。
为了让您了解原因,这里是我编写的一些代码,用于说明对可以想象的最简单树结构的最简单操作。
注意:我尚未测试此代码,因此无法保证其正确性。我预计它有缺陷。
这一切所做的只是向树添加一个新节点。它使您几乎无法控制节点添加到树中的位置。如果只是添加一个新节点作为指定父节点的最后一个兄弟节点。
要采用这种方法,您可能需要处理:
这样做当然是可行的,但最好建议您找到已经实现该功能的第 3 方库。
I believe you will be best served by finding an existing library containing a general tree implementation which you can then re-use to serve your needs.
To give you an idea why, here is some code I wrote to illustrate the most simple operation on the most simple tree structure imaginable.
Note: I have not tested this code and so cannot vouch for its correctness. I expect it has defects.
All this does is add an new node to the tree. It gives you little control over where in the tree the node is added. If simply adds a new node as the last sibling of a specified parent node.
To take this sort of approach you would likely need to deal with:
It's certainly feasible to do this, but you may be better advised to find a 3rd party library that already implements the functionality.
我在这里问了类似的问题。我没有得到任何有用的答案,所以我决定自己实现,您可以找到在这里。
编辑:
我将尝试发布如何使用我的数据结构的示例:
声明您的数据类型:
在代码中的某处声明您的主数据树对象:
创建它(并且不要忘记稍后释放):
将您的 VirtualStringTree 分配给我们的数据结构:
然后您可以使用一些值初始化您的数据树:
并设置 VST 事件来显示您的数据:
此时,您仅使用 MyTree 数据结构,对其所做的所有更改都将反映在您分配的 VST 中。然后,您始终可以将底层结构保存(和加载)到流或文件中。希望这有帮助。
I asked similar question here. I didn't got any useful answers so I decide to make my own implementation, which you can find here.
EDIT:
I'll try to post example how you could use my data structure:
Declare your data type:
Declare your main data tree object somewhere in your code:
Create it (and do not forget to free later):
Assign your VirtualStringTree to our data structure:
Then you can init your data tree with some values:
And set VST events to display your data:
At this point you work only with your MyTree data structure and all the changes made to it will be reflected in your assigned VST. You then can always save (and load) underlying structure to stream or file. Hope this helps.
如果我理解正确的话,你的树需要一个数据结构。每个单独的节点都需要一条记录来保存其数据。但底层的等级制度可以通过几种不同的方式进行管理。我猜这一切都需要在某种数据库中进行管理 - 这已经在本网站上讨论过了,所以我将向您指出:
在数据库中实现分层数据结构
,此处:
将平面表解析为树的最有效/优雅的方法是什么?< /a>
和此处:
SQL - 如何存储和导航层次结构?
嵌套集模型:
http://mikehillyer.com/articles/managing- mysql 中的分层数据/
If I understand correctly, you need a datastructure for your tree. Each individual node requires a record to hold its data. But the underlying heirarchy can be managed in a few different ways. Im guessing this is all to be managed in some sort of database - This has already been talked about on this site, so i will point you to:
Implementing a hierarchical data structure in a database
and here:
What is the most efficient/elegant way to parse a flat table into a tree?
and here:
SQL - How to store and navigate hierarchies?
Nested Set Model:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
如果您使用的是支持泛型的最新版本的 Delphi,请检查 GenericTree
If you are using recent versions of Delphi that supports Generics, check GenericTree
现在 Delphi 有泛型。我刚刚发明了一个非常好的树数据结构。暂时不会放弃代码,不是真正的开源人士,也许在不久的将来,还有其他原因请参见下文。
但我会给出一些关于如何重新创建它的提示:
假设所有节点都可以包含相同的数据结构(从上面看来就是这种情况,一个字符串,一个 id,然后是链接。
您需要重新创建的成分) -create 这是以下内容:
两个字段:self的节点数组(提示hint),数据:T;
属性
不仅仅是任何属性,而是默认属性;)
吸气剂。
具有深度和子级的递归构造函数。
一些 if 语句来停止构造。
当然还有 SetLength 来创建链接/节点,并在 for 循环中调用一些创建,然后进行一些减法;)
给定你们所有人足够的提示,看看是否有人可以重新创建它会很有趣,否则我可能会还不如申请专利,只是开玩笑,不会砸钱反对它,尽管有其他设施,但可能会扩大课程范围。
该类在构造过程中像真正的数据结构一样分配所有节点......注意添加和删除等,至少现在不是。
现在是这个(秘密)设计最有趣和有趣的方面,我有点想要的东西现在已经成为现实。我现在可以编写如下代码:
TGroup 只是一个示例,可以是任何东西,只要它在我的例子中是一个类即可。
在本例中,它只是一个带有 mString 的类。
现在您可能会从这个实际测试代码中注意到,它允许“数组扩展”,就像我很少看到的那样,这要归功于这个属性,它的自引用有点像...
所以[][]是深度2。
[][][] 的深度为 3。
我仍在评估它的用途。
一个潜在的问题是 Delphi 没有真正的技术来自动扩展这些数组,尽管我还没有找到并且对此感到满意。
我想要一种技术,我可以编写一些可以进入任何深度级别的代码:
[0][0][0][0][0]
还不确定如何做到这一点...简单的选项是“递归” 。
真实示例:
有点有趣不是吗。
仍在探索它对于“实际应用程序”的有用性以及我是否想使用递归;)
也许有一天我会开源我的所有代码。我已经快40岁了,当我过了40岁,从39岁到40岁,我有点打算开源。距离 40 =D 还差 4 个月
(我必须说这是我第一次对泛型印象深刻,很久以前就测试过它,当时它是超级错误的,也许设计方面无法使用,但现在错误已修复并限制了泛型,它在 2018 年 8 月最新的 Delphi Toyko 10.2.3 版本中非常令人印象深刻!;) :))
我只是触及了最新 Delphi 技术不可能实现的表面,也许使用匿名方法编写递归例程来处理此数据结构可能会成为一个问题。更容易一些,也许也可以考虑并行处理,Delphi 帮助提到了匿名方法的这一点。
再见,
斯凯巴克。
Delphi has generics nowadays. I just invented a very nice tree data structure. Not gonna give code away just yet, not really an open source person, maybe in the near future though, also other reasons see below.
But I will give some hints on how to re-create it:
Assuming all your nodes can contain the same data structure (which seems to be the case from above, a string, an id, and then links.
The ingredients you need to re-create this is the following:
<T : class, constructor> (In your case replace class with record, untested, but may also work)
two fields: node array of self (hint hint), data : T;
A property
Not just any propery, a default property ;)
A getter.
A recursive constructor with depth and child.
Some if statement to stop the construction.
And ofcourse SetLength to create the links/nodes and calling some creates in a for loop and then some subtraction of something ;)
Given all of you enough hints, would be fun and interesting to see if anybody can re-create it, otherwise I might just as well patent it, just kidding, not gonna throw money against it, might expand the class though with other facilities.
The class allocates all nodes during construction like a true data structure... noting with add and remove and such, at least not for now.
Now comes the most interesting and funny aspect of this (secret) design, something I kinda wanted and is now a reality. I can now write code as follows:
TGroup is just an example can be anything as long as it's a class in my case.
In this case it's just a class with mString
Now what you may notice from this actual test code is that it allows "array expansion" like I have rarely seen thanks to this property, which self-references sort of...
So [] [] is depth 2.
[][][] would be depth 3.
I am still evaluating the use of this.
One potential problem is Delphi has no real technique to auto-expand these arrays, though none I have yet found and are statisfied with.
I would like a technique where I can write some code which can go to any depth level:
[0][0][0][0][0]
Not yet sure how to do that... simpelst option is "recursion".
real example:
Kinda interesting isn't it.
Still exploring it's usefullness for "real applications" and if I want to go with recursion or not ;)
Maybe some day I will open source all of my code. I am close to 40 years old, when I go beyond 40, from 39 to 40, I was kinda planning on going open source. Still 4 months away from 40 =D
(I must say this is the first time I am impressed by Generics, tested it long ago, it was super buggy back then and maybe design-wise unusable, but now with the bugs fixed and constrained generics, it's very impressive in latest Delphi Toyko 10.2.3 version august 2018 ! ;) :))
I am just scratching the surface of what is impossible with latest Delphi tech, maybe with anonymous methods writing recursive routines to process this data structure might become a bit easier, also maybe parallel processing might come into consideration, Delphi help mentions this for anonymous methods.
Bye,
Skybuck.