Solved

Array Algorithum

Posted on 2004-11-11
146 Views
HI guys, i need abit of help...

type
PAccount = ^TAccount
TAccount = record
AccountID,
AccountType,
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
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

thanks

0
Question by:alc4emy5t

LVL 17

Accepted Solution

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,
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

Author Comment

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,
Status: string;
Next: PAccount;
Child: array of PAccount;
end;
0

Author Comment

anyway thats ok, ive given up on the idea, seems to complicated, instead i made a structure

TAccount = record
AccountID,
AccountType,
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.

0

LVL 17

Expert Comment

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…