Einzelnen Beitrag anzeigen

Benutzerbild von sniper_w
sniper_w

Registriert seit: 12. Dez 2004
Ort: Wien, Österriech
893 Beiträge
 
Delphi 6 Enterprise
 
#6

Re: Problem beim Löschen aus einer TempList

  Alt 4. Feb 2005, 22:21
Schon mal was von Binary Trees gehört. Das wäre für dich eine Idealle lösung.
Hier ein Beispiel :
Delphi-Quellcode:
type
  PbinNode = ^TbinNode;
  TbinNode = packed record
    Word:string; // Frei auszuwählende Variablen und deren Typen...
    Number:integer; // Frei auszuwählende Variablen und deren Typen...
    left_child : PbinNode;
    right_child : PbinNode;
    parent : PbinNode;
  end;

function FindNode( p : Pbinnode; _text_ : string; var Node:pbinnode ):boolean;
function FindNode_n( p : Pbinnode; _text_ : string; var Node:pbinnode ):boolean;

function AddNode( p : Pbinnode; _text_ : string; parent_ : Pbinnode = nil):PbinNode;
procedure TreePrint( p : PbinNode);

var
  WordList : TStringList; // must be cleared before use of TreePrint procedure --->WordList.Clear;
                          // it is automatic created and destroyed..

implementation

(*
  Finds a node with the condition '_text_' = p^.Word.
  Returns true if the word _text_ exists in the tree
*)


// normal, no recursion, witch is much better

function FindNode_n( p : Pbinnode; _text_ : string; var Node:pbinnode ):boolean;
 var tempnode : Pbinnode;
begin
 tempnode := p; // not really necessary or yes ? I think yes because p is a pointer...

 find_counter := 0;

 while (true) do
 begin

  inc (find_counter);

  if tempnode = nil then // if we are in a dead end
          begin // if so, nothing found
           Result := False;
           Node := nil;
           Exit;
          end;

  if _text_ = tempnode^.Word then // we fond it
        begin
         Node := tempnode;
         Result := True;
         Exit;
        end;

  // look on left
  if _text_ < tempnode^.Word
        then tempnode := tempnode^.left_child

  else
  // look on right
  tempnode := tempnode^.right_child;

 end;
end;

function FindNode( p : Pbinnode; _text_ : string; var Node:pbinnode ):boolean; // recursive
begin
 inc (find_counter);
 if p = nil then
 begin // nothing found
  Result := False;
  Node := nil;
 end

        else
         // we found it
         if _text_ = p^.Word then
                begin
                 Node := p;
                 Result := true;
               end
        else
        // look on left
        if (_text_ < p^.Word) then Result := FindNode(p^.left_child, _text_, Node )

        else
        // look on right
        Result := FindNode(p^.right_child, _text_, Node);
end;

function AddNode( p : Pbinnode; _text_ : string; parent_ : Pbinnode = nil):PbinNode;
begin
  if p = nil then
  begin
    New(p);
    p^.Word := _text_;
    p^.Number := 1;
    p^.left_child := nil;
    p^.right_child := nil;
    p^.parent := parent_;
    inc(count);
  end

  else
        if (_text_ = p^.Word) then
                 Inc(p^.Number)// nothing happens
  else
        if (_text_ < p^.Word) then
                 p^.left_child := AddNode(p^.left_child, _text_, p)
  else
         p^.right_child := AddNode(p^.right_child,_text_, p);
  result := p;
end;

(*
Puts the content of the tree in a string list as a sorted list of entries
No dupicates are found in created list
*)


procedure TreePrint( p : PbinNode); // 0,1,2,3,4,5,6....
begin
  if (p <> nil) then
  begin
    TreePrint(p^.left_child);
    WordList.Add(p^.Word);
    TreePrint(p^.right_child);
  end;
end;

initialization

 WordList := TStringList.Create();
 WordList.Clear(); // there is no real need for this but never the less

finalization

 WordList.Free();
Katura Haris
Es (ein gutes Wort) ist wie ein guter Baum, dessen Wurzel fest ist und dessen Zweige in den Himmel reichen.
  Mit Zitat antworten Zitat