Tree-like Datastructure (for use with VirtualTreeview)

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!

Tree-like Datastructure (for use with VirtualTreeview), Tree-like Datastructure (for use with VirtualTreeview). application of tree in data structure in hindi data tree structure application of data structure height of tree in  We always look for help, not only for the development of the Virtual Treeview control itself, but also for maintaining the sample projects, the help or the wiki. License Virtual Treeview is using a double licensing scheme.

Virtual Treeview step by step, For other questions you can contact Mike and use the dedicated newsgroup, The pointer allows (similar to the data property of the tree) a record or even better a The Virtual Treeview does not really know this structure, but it knows how  Hi, I need to update and search data which is organized in a tree like structure, i.e. the data is organized as a parent-child, with a parent able to have 0M children, and those children able to have 0M children themselves (i.e. child can have

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:

uses
  svCollections.GenericTrees;

Declare your data type:

type
  TMainData = record
    Name: string;
    ID: Integer;
  end;

Declare your main data tree object somewhere in your code:

MyTree: TSVTree<TMainData>;

Create it (and do not forget to free later):

MyTree: TSVTree<TMainData>.Create(False);

Assign your VirtualStringTree to our data structure:

MyTree.VirtualTree := VST;

Then you can init your data tree with some values:

procedure TForm1.BuildStructure(Count: Integer);
var
  i, j: Integer;
  svNode, svNode2: TSVTreeNode<TMainData>;
  Data: TMainData;
begin
  MyTree.BeginUpdate;
  try
    for i := 0 to Count - 1 do
    begin
      Data.Name := Format('Root %D', [i]);
      Data.ID := i;
      svNode := MyTree.AddChild(nil, Data);
      for j:= 0 to 10 - 1 do
      begin
        Data.Name := Format('Child %D', [j]);
        Data.ID := j;
        svNode2 := MyTree.AddChild(svNode, Data);
      end;
    end;
  finally
    MyTree.EndUpdate;
  end;
end;

And set VST events to display your data:

procedure TForm1.vt1InitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode;
  var ChildCount: Cardinal);
var
  svNode: TSVTreeNode<TMainData>;
begin
  svNode := MyTree.GetNode(Sender.GenerateIndex(Node));
  if Assigned(svNode) then
  begin
    ChildCount := svNode.FChildCount;
  end;
end;

procedure TForm1.vt1InitNode(Sender: TBaseVirtualTree; ParentNode,
  Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
var
  svNode: TSVTreeNode<TMainData>;
begin
  svNode := MyTree.GetNode(Sender.GenerateIndex(Node));
  if Assigned(svNode) then
  begin
    //if TSVTree<TTestas> is synced with Virtual Treeview and we are building tree by
    // setting RootNodeCount, then we must set svNode.FVirtualNode := Node to
    // have correct node references
    svNode.FVirtualNode := Node;  // Don't Forget!!!!
    if svNode.HasChildren then
    begin
      Include(InitialStates, ivsHasChildren);
    end;
  end;
end;

//display info how you like, I simply get name and ID values
procedure TForm1.vt1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
  svNode: TSVTreeNode<TMainData>;
begin
  svNode := MyTree.GetNode(Sender.GenerateIndex(Node));
  if Assigned(svNode) then
  begin
    CellText := Format('%S ID:%D',[svNode.FValue.Name, svNode.FValue.ID]);
  end;
end;

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.

[PDF] Virtual Treeview, Virtual Treeview exposes its internal states like pending drag or edit events, multi selection or native tree data specialized methods are implemented. See and set up the structure in the most efficient way but does not yet query for data. Three ways of iterating over tree-like ActiveRecord structures. You are building an e-commerce app and at some point you introduce a Category model as a part of Product data. A Category can have various subcategories, which are represented by the same model. At this point, it's obvious we are dealing with a tree-like data structure.

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/

Virtual Tree View: Delphi 3rd Party Open Source Component, Any tree view like component's purpose is to display a hierarchical list of items. on displaying hierarchical data, like folders and files, XML structure, Here are just a few "why to use" features of the Virtual TreeView control:. Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code.

If you are using recent versions of Delphi that supports Generics, check GenericTree

Fast scrolling in Delphi's Virtual Treeview, Whether you do it yourself or you use a method of the tree control, You might also reconsider whether you need a data structure like that. Fixed #645: Can't link 64 bit VCL app with VirtualTreeView in C++ Builder 10.1 Berlin Fixed #656 : Fixed memory leaks in Advanced Demo Enhancement #631 : Added new Demo project CharityEvents to demonstrate the use of Interfaces for node's data and for data presentation

Applications of tree data structure, One reason to use trees might be because you want to store information that naturally Store hierarchical data, like folder structure, organization structure,  GitHub is home to over 36 million developers use GitHub to host and review code, manage projects, and build software together across more than 100 million repositories.

VirtualTreeView/VirtualTrees.dtx at master · TurboPack , Tells the application that the tree (and only the tree) does not want a delayed tool tip. Normal hints structure and the internal data), if -1 then do callback. “In computer science, a binary tree is a tree data structure in which each node has at the most two children, which are referred to as the left child and the right child.” — Wikipedia So let’s look at an example of a binary tree .

Html5 treeview, The TreeView widget is a tree-like representation of textual data. data in a tree structure. html5 treeview Hi to All, i want use treeview control in html5 place it on the form Virtual Treeview does not read the data it manages except its size,  Programming with C# course 2013/2014 Lecturers: Georgi Dimitrov and Slavi Georgiev Lecture #11: Tree-like Data Structures Official course site: http://unwise

Comments
  • @Jeff, IMHO, your question is not understandable to anyone which didn't read your previous questions, for what you don't provide links (as a last resort), but it is preferable if you write each question as if it is the only question you posted here. That way the question will be easily answerable by anyone, but most important, it will be understandable for future readers.
  • @jach - I am glad that you tell me, will try to find the previous post. :)
  • @Jeff: No offense, but if I have got things right, you are a beginner programmer -- but if you keep learning, you will (in a few years) master programming at a much more sophisticated level. Until then, however, I think it is wise not to try too hard things when there are simpler alternatives. Are you really sure that you cannot use a TListBox? I mean, if you need a more advanced control to display your data, like the Virtual TreeView, you should first have the data in some "underlaying datastructure".
  • This question looks useful: stackoverflow.com/questions/1841621
  • @Jeff, Even when you DO get an working answer, it's not in your best interest, nor in the best interest of the community to immediately accept an answer. Unless the problem's very simple, like, you should wait a day or two to accept an answer: maybe an better answer will show up! This is in fact the kind of question that benefits from multiple answers. For example I saw the question this morning, didn't have the time to deal with it, when I came back I saw the tick mark for an answer that suggests using a database. I hate it when people recommend databases as if they'd be a panacea.
  • First of all, thanks for this answer! I will definitely give it a try when I get home. However, I read in the Virtual Treeview documentation, that using records is better than classes? I don't know what I'm supposed to believe, as I am still a newbie at this.
  • @Jeff, a record might be better if you store the data in the treview node itself. If you move the data to a distinct data structure you'll be storing a pointer: Why would VT care if you store a pointer to a record or a pointer to an object instance?
  • @Cosmin - I have been reading up about pointers (about.com), but I still don't see exactly how they work, when I read "If you move the data to a distinct data structure you'll be storing a pointer". Also, doesen't records take less memory?
  • @Jeff: A record might take a few bytes less memory when compared with a similar class, but records don't have destructors and are passed around by value, not by reference. I thought I explained that in my answer. In my opinion classes are a better fit for tree data structure. Alternatively you can implement your own tree using pointers and records.
  • @daemon, TNode is a pointer, so PNode = ^TNode is actually a pointer to a pointer, so they're not both "just pointers". One's a pointer, one's a double-indirected pointer. And it would be the wrong type to use in VTGetText especially if you expect the payload to be TNode OR something else: a better choice would be PObject = ^TObject;
  • I dont see where I can download it? :)
  • I just tested the difference between using the VT to store the data in, and your SVTreeby adding 1000 roots with 1000 children in each. Turns out that the SVTree uses around 100 megs more than the VT one, and the VT one is about twice as fast. Is it because we are using pointers and not the whole record in the VT?
  • There will be some additional memory usage because TSVTreeNode objects are additionally created while building data structure. Don't know how you tested but building my data structure is a little bit slower because it needs to generate unique node's hash to be able to retrieve it later on VT events. I found it quite well performing in decent tests but if you need adding millions of nodes with huge level of hierarchy you may consider some other methods.