[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Array Algorithum

HI guys, i need abit of help...

i have the follow structure

type
  PAccount = ^TAccount
  TAccount = record
    AccountID,
    AccountType,
    Username,
    Password,
    Status: string;
    Account: array of PAccount;
  end;

var
  Account = array of PAccount;

procedure FillStructure();
begin

  {
  The table Account structure looks like this:
    AccountID int,
    AccountType varchar
    Username varchar
    Password varchar
    Status varchar
    ParentID int
 
  where the ParentID is equal to the AccountID of the Parent of a child account, not all parents     have children but a parent can have unlimited children and the children can also be parents of unlimited children.  there is no limit to the levels of parent - children.
  }
   
  Query.SQL.Clear;
  Query.SQL.Add('select * from account where customer_id = 5');
  Query.Open;


  while not Query.Eof do
  begin
   
  end;

  Query.Close;

  So i want an algorithum that will fill the Account structure with the parent and any children they have, there is no limit to the number of children and it must be a fast algorithum.
end;

in the end it will be a tree structure looking something like this:

Parent
  Child
  Child
Parent
Parent
  Child
Parent
  Child
  Child (becomes parent)
    Child (becomes parent)
      Child
  Child
Parent
  Child (becomes parent)
    Child
  Child (becomes parent)
    Child (becomes parent)
      Child (becomes parent)
        Child
  Child
Parent
  Child

Etc

so please help anyone, need this done asap

thanks

0
alc4emy5t
Asked:
alc4emy5t
  • 2
  • 2
1 Solution
 
Wim ten BrinkCommented:
Homework?

Just use the query 'select * from account' to retrieve all records. Then walk through the recordset once to fill this tree that you use. However, instead of a dynamic array, you might prefer to use a linked list instead, creating some treelike memory structure. Something like this:

type
  PAccount = ^TAccount
  TAccount = record
    AccountID,
    AccountType,
    Username,
    Password,
    Status: string;
    Next: PAccount;
    Child: PAccount;
  end;

And you might want to build some class around it to manipulate this list. And perhaps a dynamic array that allows you to jump quickly to one of the Account records, allowing you to walk through the list without having to branch through the children. (The dynamic array would flatten it all.)
0
 
alc4emy5tAuthor Commented:
Well a parent can have many children which can also have many children so i thought this is better

type
  PAccount = ^TAccount
  TAccount = record
    AccountID,
    AccountType,
    Username,
    Password,
    Status: string;
    Next: PAccount;
    Child: array of PAccount;
  end;
0
 
alc4emy5tAuthor Commented:
anyway thats ok, ive given up on the idea, seems to complicated, instead i made a structure

TAccount = record
    AccountID,
    AccountType,
    Username,
    Password,
    Status: string;
    Node: TTreeNode
end;

and populated an array of TAccount then displayed the data in a TTreeView and assigned the node to the current array record being added to the treeview.

Anyway cheers for your time, i think that your answer would have worked well.
0
 
Wim ten BrinkCommented:
In general, when you want a 1:N relationship like you want between parent and child, you should let the child point to the parent, not the other way around. Technically, by using an array of children you might make it possible for a child to be owned by multiple parents which is exactly one thing you don't want.

In general, when I need some in-memory tree, I just generate a single array containing all records and add a Parent field to every record, which points to another record in the same array. If it points to nil, it has no parent. The advantage is that it's easier to clean up since you just clear the array. You can also use the array to lookup other records. To find all the children of some record, you would just walk through the whole array and every record that points to the parent is a child.
You have to be aware that you don't create any circulair references, though... A parent whi is a child of it's own child, for example, could raise a lot of errors.

In general, trees can be a real pain sometimes...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now