我刚写了个用二叉树存文件树的联系,把二叉树的类贴出来给你看看:
TBiTree = class
private
FData: string;
FExtra: DWord;
FLChild: TBiTree;
FRChild: TBiTree;
public
constructor Create; overload;
constructor Create(Data: string; Extra: DWord); overload;
constructor Create(Data: string; Extra: DWord; LChild, RChild: TBiTree); overload;
destructor Destroy; override;
function AddLChild(Data: string; Extra: DWord): TBiTree;
function AddRChild(Data: string; Extra: DWord): TBiTree;
procedure Clear;
procedure SetValue(Data: string; Extra: DWord; LChild, RChild: TBiTree);
property Data: string read FData write FData;
property Extra: DWord read FExtra write FExtra;
property LChild: TBiTree read FLChild write FLChild;
property RChild: TBiTree read FRChild write FRChild;
end;
constructor TBiTree.Create;
begin
SetValue('', DWord(-1), nil, nil);
end;
constructor TBiTree.Create(Data: string; Extra: DWord);
begin
SetValue(Data, Extra, nil, nil);
end;
constructor TBiTree.Create(Data: string; Extra: DWord; LChild, RChild: TBiTree);
begin
SetValue(Data, Extra, LChild, RChild);
end;
destructor TBiTree.Destroy;
begin
Clear;
end;
function TBiTree.AddLChild(Data: string; Extra: DWord): TBiTree;
begin
Self.LChild := TBiTree.Create(Data, Extra);
Result := Self.LChild;
end;
function TBiTree.AddRChild(Data: string; Extra: DWord): TBiTree;
begin
Self.RChild := TBiTree.Create(Data, Extra);
Result := Self.RChild;
end;
procedure TBiTree.Clear;
begin
if Assigned(FLChild) then
FreeAndNil(FLChild);
if Assigned(FRChild) then
FreeAndNil(FRChild);
end;
procedure TBiTree.SetValue(Data: string; Extra: DWord; LChild, RChild: TBiTree);
begin
Self.Data := Data;
Self.Extra := Extra;
Self.LChild := LChild;
Self.RChild := RChild;
end;