Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two methods Root and Depth #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions TeeGenericTree.pas
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ TNode<T>=class
procedure Delete(const Index:TInteger; const ACount:TInteger=1);
procedure ForEach(const AProc:TNodeProc; const Recursive:Boolean=True);
procedure Sort(const ACompare:TCompareProc; const Recursive:Boolean=True);
function Root : TNode<T>;
function Depth : integer;

property Index:TInteger read GetIndex;
property Item[const Index:TInteger]:TNode<T> read Get; default;
Expand All @@ -172,6 +174,7 @@ implementation
Constructor TNode<T>.Create(const AData: T);
begin
inherited Create;
fParent := nil;
Data:=AData;
end;

Expand Down Expand Up @@ -241,6 +244,19 @@ procedure TNode<T>.Delete(const Index, ACount: TInteger);
Extract(Index,ACount);
end;

function TNode<T>.Depth: integer;
var
tmp : TNode<T>;
begin
Result := 0;
Tmp := self;
while assigned(tmp.Parent) do
begin
inc(Result);
tmp := tmp.Parent;
end;
end;

// Returns True when this node has no children nodes
function TNode<T>.Empty:Boolean;
begin
Expand Down Expand Up @@ -385,6 +401,13 @@ procedure TNode<T>.PrivateSort(const ACompare: TCompareProc; const l,r:TInteger)
PrivateSort(ACompare,i,r);
end;

function TNode<T>.Root: TNode<T>;
begin
Result := Self; // default we are the root
While Assigned(Result.Parent) do
Result := Result.Parent;
end;

// Re-order children items according to a custom ACompare function
procedure TNode<T>.Sort(const ACompare: TCompareProc; const Recursive: Boolean);
var t : TInteger;
Expand Down