I'm trying to build a tree. The only information I have for each element is the id of the parent he refers to.
I have three differents records :
Type TRecord1 = Record
ID : Integer;
Name : String255;
Res : Array[1..500] Of Byte;
End;
//------------------------------------------------------------------------------
Type TRecord2 = Record
ID : Integer;
Parents : Array [0..4] of Integer;
Customer : String100;
Res : Array[1..500] Of Byte;
End;
//------------------------------------------------------------------------------
Type TRecord3 = Record
ID : Integer;
Parents : Array [0..4] of Integer;
Datas : String20;
Res : Array[1..500] Of Byte;
End;
For datas store : I have one File for one kind of Record which store all datas of this record
Record1 has always no parent. Record2 and Record3 have always at least 1 parent (maximum 5 => Parents array)
A Record2 can be a parent of Record2, ect... There is no hierarchy between them except Record1 which is always a root node.
How is it possible to build efficiently the treeview associated ?
I can at first build Record1 Nodes which will be the first nodes of the tree. Then for each of this node, I have to find Record2 of Record3 associated to this node, ect, ect... It seems that will take too much time.
Any idea to create the tree from another way ?
FOr the moment, I save the TreeView on a Text File and I Made a link between a Node and the record associated. But I want create the TreeView from the datas instead of have a TreeView in a side and datats in another side
I'm guessing that IDs of your record items are unique and therefore are probably assigned to each record incrementally. This means that older records have lower ID number than newer records.
So, you can first sort all your records by their ID and then start from the one with lowest ID and proceed toward the one with highest ID.
Now, if all of your records would only have a single parent possible, this would surely work with assumption that you always create a parent record before any of its child records.
But, since you have records that could have multiple parents, it is possible that you encounter a scenario where one of the parents from your current record refers to another record that you haven't added to your tree yet.
I'm guessing that you could potentially avoid that by doing this in two passes, where in the first pass you try and position the node of your current record in relation to the first parent. And then in the second pass, you add connections for the rest of the parents.