如何使用二叉树(100分)

  • 主题发起人 主题发起人 ygd
  • 开始时间 开始时间
Y

ygd

Unregistered / Unconfirmed
GUEST, unregistred user!
我想建立一个二叉树,向里面储存字符,在一个按钮的onclick下生成。
如何用类pascal在delphi下实现?
急!!!!!!!!!!!![?]
请帮忙!
谢谢!
 
我刚写了个用二叉树存文件树的联系,把二叉树的类贴出来给你看看:

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;
 
后退
顶部