0

正如标题所述,主要是在算法上遇到了一些麻烦。我有算法来解析并打印所有值,但我需要能够停止每个值并将其显示在标签中。我尝试使用全局变量“count”,以为我可以阻止它。有任何想法吗?这是我到目前为止所拥有的:

unit Unit4;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,           Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm4 = class(TForm)
  Createtree: TButton;
  Current: TLabel;
  RecursiveNext: TButton;
  Close: TButton;
  IterativeNext: TButton;
  Iterative: TLabel;
  procedure CloseClick(Sender: TObject);
  procedure CreatetreeClick(Sender: TObject);
  procedure RecursiveNextClick(Sender: TObject);
  procedure IterativeNextClick(Sender: TObject);
private
  { Private declarations }
public
  { Public declarations }
end;

type
 nodeptr = ^nodetype;
 nodetype = record
 id : integer ;
 left,right : nodeptr ;
 threaded : Boolean;
  end;

var
 Form4: TForm4;
 inf : textfile;
 root,c : nodeptr;
 count : integer;

implementation

{$R *.dfm}
function Emptytree(var root : nodeptr) : Boolean;
begin
  Result := False;
  if root^.id <> NULL then Result := True;
end;

procedure TForm4.CloseClick(Sender: TObject);
begin
 application.Terminate;
end;

function recursiveinorder(c : nodeptr) : integer;
 begin
 if c^.left <> nil then recursiveinorder(c^.left);
  Result := c^.id;
 if (c^.right <> nil) and (count > 0) then recursiveinorder(c^.right);
  count := count - 1;
 end;

function iterativeinorder(c : nodeptr) : integer;
var done : boolean;
begin
 c := root;
while c <> nil do c := c^.left;
begin
  done := false;
  while done = False do
   begin
   Result := c^.id;
    if (c^.threaded = false) and (count > 0) then
    begin
    c := c^.right;
    done := true;
    end
    else
    begin
    c := c^.right;
    if c = nil then done := true;
    end;
   end;
  end;
count := count - 1;
end;

procedure TForm4.CreatetreeClick(Sender: TObject);
var
parent, knew, c : nodeptr;

begin
 count := 0;
 new(parent);
 new(c);
 new(root);
 assignfile(inf, 'treedata.txt');
 reset(inf);
 readln(inf,root^.id);
while not eof(inf) do
  begin
  new(knew);
  readln(inf,knew^.id);
  if not Emptytree(root) then
   begin
    c:= root;
      while c <> nil do
        begin
        parent := c;
        if knew^.id < c^.id then
        c:=c^.left
      else c:= c^.right;
        end;
      if knew^.id < parent^.id then
       begin
       parent^.left := knew;
       knew^.threaded := True;
       knew^.right := parent;
       end
       else begin
         knew^.right := parent^.right;
         knew^.threaded := parent^.threaded;
         parent^.right := knew;
         parent^.threaded := false;
       end;
 end;
 if Emptytree(root) then root := knew;
end;
end;

procedure TForm4.IterativeNextClick(Sender: TObject);
 begin
  count := count + 1;
 Iterative.Caption := inttostr(iterativeinorder(root));
end;

procedure TForm4.RecursiveNextClick(Sender: TObject);
 begin
 count := count + 1;
 Current.Caption := inttostr(recursiveinorder(root));
end;

end.
4

0 回答 0