打印本文 打印本文 关闭窗口 关闭窗口
自己编写树(Tree)的封装类
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4966  更新时间:2009/4/23 18:34:19  文章录入:mintao  责任编辑:mintao
  if not Assigned(AOwner) then

    raise Exception.Create(''''AOwner is nil In TYuNode.Create'''');

 

  FOwner := AOwner;

  FUpLeft    := nil;

  FUpRight   := nil;

  FDownLeft  := nil;

  FDownRight := nil;

 

  FDeleting := False;

  FIsRootOfDeleted := False;

end;

 

destructor TYuNode.Destroy;

var

  SubNode, WillDeleteNode: TYuNode;

begin

  FDeleting := True;

 

  if FIsRootOfDeleted then //维护指针

    CutFromTree;

 

  SubNode := GetFirstChild;

  while SubNode <> nil do

  begin

    WillDeleteNode := SubNode;

    SubNode := SubNode.GetNextBrother;

    WillDeleteNode.Free;

  end;

 

  inherited;

end;

 

function TYuNode.GetFirstChild: TYuNode;

begin

  Result := FDownLeft;

end;

 

function TYuNode.GetFirstBrother: TYuNode;

begin

  Result := Self;

  while Result.GetPrevBrother <> nil do

    Result := Result.GetPrevBrother;

end;

 

function TYuNode.GetLastBrother: TYuNode;

begin

  Result := Self;

  while Result.GetNextBrother <> nil do

    Result := Result.GetNextBrother;

end;

 

function TYuNode.GetLastChild: TYuNode;

begin

  Result := FDownLeft;

  if Result = nil then Exit;

  while Result.FDownRight <> nil do

    Result := Result.FDownRight;

end;

 

function TYuNode.GetLevel: Integer;

var

  Node: TYuNode;

begin

  Node := Self;

  Result := -1;

  repeat

    Node := Node.Parent;

    Inc(Result);

  until Node = nil;

end;

 

function TYuNode.GetNext: TYuNode;

var

  Node: TYuNode;

begin

  //1.查看第一个子结点

  Result := GetFirstChild;

  //2.如果第1步不成功,查看下一个兄弟

  if Result = nil then

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9]  下一页

打印本文 打印本文 关闭窗口 关闭窗口