3
interface //#################################################################### ■
5
uses FMX.Types, FMX.TreeView,
8
type //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【型】
10
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【レコード】
12
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【クラス】
14
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTreeNode
16
TTreeNode = class( LUX.Graph.Tree.TTreeNode<TTreeNode> )
22
constructor Create; override;
25
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TMyMaterialSource
27
//const //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【定数】
29
//var //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【変数】
31
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【ルーチン】
33
function RandString( const N_:Integer ) :String;
35
procedure ShowTree( const TreeView_:TTreeView; const Root_:TTreeNode );
37
function RandNode( const Node_:TTreeNode ) :TTreeNode;
38
function RandKnot( const Node_:TTreeNode ) :TTreeNode;
40
function FindNode( const Node_:TTreeNode ) :TTreeNode;
41
function FindKnot( const Knot_:TTreeNode ) :TTreeNode;
42
function FindLeaf( const Node_:TTreeNode ) :TTreeNode;
44
procedure AddNode( const Root_:TTreeNode );
45
procedure TransNode( const Root0_,Root1_:TTreeNode );
46
procedure SwapSibliNodes( const Root_:TTreeNode );
47
procedure SwapOtherNodes( const Root1_,Root2_:TTreeNode );
48
procedure DelNode( const Root_:TTreeNode );
50
implementation //############################################################### ■
54
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【レコード】
56
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【クラス】
58
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TTreeNode
60
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& private
62
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& protected
64
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public
66
constructor TTreeNode.Create;
70
Name := RandString( 8 );
73
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$【ルーチン】
75
function RandString( const N_:Integer ) :String;
77
Cs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
83
for I := 1 to N_ do Result := Result + Cs.Chars[ Random( 26 ) ];
86
//------------------------------------------------------------------------------
88
procedure ShowTree( const TreeView_:TTreeView; const Root_:TTreeNode );
89
//・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
90
procedure AddNode( const Parent_:TFmxObject; const TreeNode_:TTreeNode );
96
P := TTreeViewItem.Create( TreeView_ );
100
if Assigned( TreeNode_.Paren ) then S := TreeNode_.Order.ToString
103
P.Text := S + ' [' + TreeNode_.Name + '] ' + TreeNode_.ChildsN.ToString;
107
for I := 0 to ChildsN-1 do AddNode( P, Childs[ I ] );
110
//・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
114
AddNode( TreeView_, Root_ );
119
//------------------------------------------------------------------------------
121
function RandNode( const Node_:TTreeNode ) :TTreeNode;
123
Result := Node_.Childs[ Random( Node_.ChildsN ) ];
126
function RandKnot( const Node_:TTreeNode ) :TTreeNode;
130
Ps :TArray<TTreeNode>;
134
SetLength( Ps, ChildsN );
137
for I := 0 to ChildsN-1 do
141
if P.ChildsN > 0 then
143
Ps[ N ] := P; Inc( N );
148
if N = 0 then Result := nil
149
else Result := Ps[ Random( N ) ];
152
//------------------------------------------------------------------------------
154
function FindNode( const Node_:TTreeNode ) :TTreeNode;
158
while ( Result.ChildsN > 0 )
159
and ( Random( 4 ) > 0 ) do Result := RandNode( Result );
162
function FindKnot( const Knot_:TTreeNode ) :TTreeNode;
168
while Random( 4 ) > 0 do
170
P := RandKnot( Result );
172
if not Assigned( P ) then Exit;
178
function FindLeaf( const Node_:TTreeNode ) :TTreeNode;
182
while Result.ChildsN > 0 do Result := RandNode( Result );
185
//------------------------------------------------------------------------------
187
procedure AddNode( const Root_:TTreeNode );
191
P := FindNode( Root_ );
194
0: TTreeNode.Create.Paren := P;
195
1: TTreeNode.Create( P );
199
procedure TransNode( const Root0_,Root1_:TTreeNode );
203
if Root0_.ChildsN > 0 then
205
C := FindLeaf( RandNode( Root0_ ) );
206
P := FindNode( Root1_ );
208
if P.ChildsN = 0 then
212
1: P.InsertHead( C );
213
2: P.InsertTail( C );
220
1: P.InsertHead( C );
221
2: P.InsertTail( C );
222
3: TTreeNode( P.Head ).InsertPrev( C ); {本来キャスト不要}
223
4: TTreeNode( P.Head ).InsertNext( C ); {本来キャスト不要}
224
5: TTreeNode( P.Tail ).InsertPrev( C ); {本来キャスト不要}
225
6: TTreeNode( P.Tail ).InsertNext( C ); {本来キャスト不要}
226
7: TTreeNode( P.Childs[ Random( P.ChildsN ) ] ).InsertPrev( C ); {本来キャスト不要}
227
8: TTreeNode( P.Childs[ Random( P.ChildsN ) ] ).InsertNext( C ); {本来キャスト不要}
233
procedure SwapSibliNodes( const Root_:TTreeNode );
235
P, C1, C2 :TTreeNode;
238
if Root_.ChildsN > 0 then
240
P := FindKnot( Root_ );
242
if Assigned( P ) then
244
C1 := RandNode( P ); I1 := C1.Order;
245
C2 := RandNode( P ); I2 := C2.Order;
251
3: TTreeNode.Swap( C1, C2 );
257
procedure SwapOtherNodes( const Root1_,Root2_:TTreeNode );
261
if ( Root1_.ChildsN > 0 )
262
or ( Root2_.ChildsN > 0 ) then
264
C1 := FindLeaf( RandNode( Root1_ ) );
265
C2 := FindLeaf( RandNode( Root2_ ) );
267
TTreeNode.Swap( C1, C2 );
271
procedure DelNode( const Root_:TTreeNode );
273
if Root_.ChildsN > 0 then FindLeaf( RandNode( Root_ ) ).Free;
276
//############################################################################## □
278
initialization //######################################################## 初期化
280
finalization //########################################################## 最終化
282
end. //######################################################################### ■