タイトルが示すように、主にアルゴリズムに少し問題があります。すべての値を解析して出力するアルゴリズムがダウンしていますが、各値で停止してラベルに表示できるようにする必要があります。私はそれを止めることができるかもしれないと思って、グローバル変数 '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.