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!
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
TTreeViewI’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 theTRootrecord (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 ofTRootwill contain the sameKids:TListas 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 theTListwith anarray of Tkidbut 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
TNodetype, but in the real world you might have nodes of typesTContact,TContactCategory,TRecentCall, all in one VT. You’ll use theisoperator 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:stringinto a virtual methodGetText:string;virtualand then create specialized descendants ofTNodethat overrideGetTextto 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.PVirtualNodeintoTNodeitself so you can check rather the Node is “checked” in the Virtual Tree. This would be a bridge of the data-GUI separation.