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

alc4emy5tAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 BrinkSelf-employed developerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.