Tree-like Datastructure (for use with VirtualTreeview)

前端 未结 6 470
遥遥无期
遥遥无期 2020-12-15 14:20

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 al

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 15:09

    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:

    1. Generics
    2. A generic type T
    3. This type T needs to be constrained to class and constructor as follows:

    (In your case replace class with record, untested, but may also work)

    1. two fields: node array of self (hint hint), data : T;
    2. A property

    3. Not just any propery, a default property ;)

    4. A getter.

    5. A recursive constructor with depth and child.

    6. Some if statement to stop the construction.
    7. 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

    var
      mGroupTree : TTree;
    
    procedure Main;
    var
      Depth : integer;
      Childs : integer;
    begin
    
      Depth := 2;
      Childs := 3;
    
      mGroupTree := TTree.Create( Depth, Childs );
    
      mGroupTree.Data.mString := 'Basket'; // notice how nice this root is ! ;)
    
      mGroupTree[0].Data.mString := 'Apples';
      mGroupTree[1].Data.mString := 'Oranges';
      mGroupTree[2].Data.mString := 'Bananas';
    
      mGroupTree[0][0].Data.mString := 'Bad apple';
      mGroupTree[0][1].Data.mString := 'Average apple';
      mGroupTree[0][2].Data.mString := 'Good apple';
    
      mGroupTree[1][0].Data.mString := 'Tiny orange';
      mGroupTree[1][1].Data.mString := 'Medium orange';
      mGroupTree[1][2].Data.mString := 'Big orange';
    
      mGroupTree[2][0].Data.mString := 'Straight banana';
      mGroupTree[2][1].Data.mString := 'Curved banana';
      mGroupTree[2][2].Data.mString := 'Crooked banana';
    

    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:

    procedure DisplayString( Depth : string; ParaTree : TTree);
    var
      vIndex : integer;
    begin
      if ParaTree <> nil then
      begin
    //    if ParaTree.Data.mString <> '' then
        begin
          writeln( ParaTree.Data.mString );
    
          Depth := Depth + ' ';
          for vIndex := 0 to ParaTree.Childs-1 do
          begin
            DisplayString( Depth, ParaTree[vIndex] );
          end;
        end;
      end;
    end;
    

    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.

提交回复
热议问题