开发者

Searching an unorderd list of descendants

In my Delphi 2007 database application, I have a list of Generations. Each Generation has a list of pricing templates, each of which is a set of values. In the application, the user can create a line item and assign a pricing template from any generation. A new generation is created by cloning an existing generation. So the user only has to create the first generation, then clone it and only change the required pricing template values in the new generation. These generations(and pricing templates) 开发者_如何学编程are connected by originids.ie, Generation1's originid will be zero and generation2 will have originid pointing to generation1' id etc. Also, the user can create a new generation from any of the existing ones.ie, generation3's origin can be generation1. Now, the user wants a refresh functionality where the user can update the pricing templates from a generation to any other generation if they are "connected". ie, If they connected by origin ids (Not necessarily by a direct connection.ie,If the source generation's origin id is pointing directly to the target's id or any of the direct children of target, or any of the children of the target's children..etc). Please see the description below.

What is the best way to search (algorithm /data structure) for a target from a source in this case?

Thank you all in advance,

Pradeep


GenerationName --Id -- OriginID

Generation 1 -- 100-- 0

Generation 2 --101 --100 (Cloned from 1)

Generation 3 --102 --100 (Cloned from 1)

Generation 4 --103 --102 (Cloned from 3)

Generation 5 --104 --101 (Cloned from 2)

Here,the user can update from Generation 1 to Generation 3(Generation 1Generation 3) or Generation 4(Generation 1Generation 3Generation 4) or Generation 5((Generation 1Generation 2Generation 5) because they are connected. But Generation 3 to generation 5 is not allowed because there is no link between them.


In my view using tree data structure is the best way for these sort of problems.

Coming to Delphi IDE, you can use TTreeView component for this purpose.

Put the data in the tree structure by using TTreeView component. Click here for tutorials regarding TTreeView.

For searching in TTreeView you go through this link.


To determine if an generation can be upgraded to another I would use a function similar to this. Here gen is a structure where you have loaded your generations and can lookup them by Id.

function canUpgrade(fromId, toId: integer) : boolean;
var
  id : integer;
begin
  Result := false;
  id := toId;
  while id<>0 do
  begin
    if gen[id].originId=fromId then
    begin
      Result := true;
      Break;
    end
    else
    begin
      id := gen[id].originId;
    end;
  end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜