ID |
类别 |
父类别 |
1 |
A |
0 |
2 |
B |
0 |
3 |
C |
0 |
4 |
AA |
1 |
5 |
BB |
2 |
6 |
CC |
3 |
添加到TreeView中:
1 type
2 PpgInfo = ^TpgInfo;
3
4 TpgInfo = record
5 aName: string;
6 aID: string;
7 aPID: string;
8 end;
9
10 //add to tv
11 procedure TForm2.add2tv(al: TList<TpgInfo>);
12 var
13 paRec: PpgInfo;
14 i: Integer;
15 find: Boolean;
16 begin
17 while al.Count > 0 do
18 begin
19
20 if al[0].aPID = '0' then
21 begin
22 // add tv
23 New(paRec);
24 paRec^ := al[0];
25 TreeView1.Items.AddObject(TreeView1.Items.GetFirstNode,
26 al[0].aName, paRec);
27 al.Delete(0);
28 end
29 else
30 begin
31 // search pid
32 find := False;
33 for i := 0 to TreeView1.Items.Count - 1 do
34 begin
35 if PpgInfo(TreeView1.Items[i].Data).aID = al[0].aPID then
36 begin
37 New(paRec);
38 paRec^ := al[0];
39 TreeView1.Items.AddChildObject(TreeView1.Items.Item[i],
40 paRec.aName, paRec);
41 al.Delete(0);
42 find := True;
43 Break;
44 end;
45 end;
46 if not find then
47 begin
48 al.Add(al[0]);
49 al.Delete(0);
50 end;
51
52 end;
53 end;
54 end;
55
56 //测试数据
57 procedure TForm2.Button2Click(Sender: TObject);
58 var
59 arrList: TList<TpgInfo>;
60 aRec: TpgInfo;
61 begin
62 arrList := TList<TpgInfo>.Create;
63 aRec.aName := 'A';
64 aRec.aID := '1';
65 aRec.aPID := '0';
66 arrList.Add(aRec);
67 aRec.aName := 'B';
68 aRec.aID := '2';
69 aRec.aPID := '0';
70 arrList.Add(aRec);
71 aRec.aName := 'C';
72 aRec.aID := '3';
73 aRec.aPID := '0';
74 arrList.Add(aRec);
75 aRec.aName := 'AA';
76 aRec.aID := '4';
77 aRec.aPID := '1';
78 arrList.Add(aRec);
79 aRec.aName := 'BB';
80 aRec.aID := '5';
81 aRec.aPID := '2';
82 arrList.Add(aRec);
83 add2tv(arrList);
84 FreeAndNil(arrList);
85 end;
86 //别忘了释放
87 if Assigned(TreeView1) then
88 for i := 0 to TreeView1.Items.Count - 1 do
89 Dispose(PpgInfo(TreeView1.Items[i].Data));
2 PpgInfo = ^TpgInfo;
3
4 TpgInfo = record
5 aName: string;
6 aID: string;
7 aPID: string;
8 end;
9
10 //add to tv
11 procedure TForm2.add2tv(al: TList<TpgInfo>);
12 var
13 paRec: PpgInfo;
14 i: Integer;
15 find: Boolean;
16 begin
17 while al.Count > 0 do
18 begin
19
20 if al[0].aPID = '0' then
21 begin
22 // add tv
23 New(paRec);
24 paRec^ := al[0];
25 TreeView1.Items.AddObject(TreeView1.Items.GetFirstNode,
26 al[0].aName, paRec);
27 al.Delete(0);
28 end
29 else
30 begin
31 // search pid
32 find := False;
33 for i := 0 to TreeView1.Items.Count - 1 do
34 begin
35 if PpgInfo(TreeView1.Items[i].Data).aID = al[0].aPID then
36 begin
37 New(paRec);
38 paRec^ := al[0];
39 TreeView1.Items.AddChildObject(TreeView1.Items.Item[i],
40 paRec.aName, paRec);
41 al.Delete(0);
42 find := True;
43 Break;
44 end;
45 end;
46 if not find then
47 begin
48 al.Add(al[0]);
49 al.Delete(0);
50 end;
51
52 end;
53 end;
54 end;
55
56 //测试数据
57 procedure TForm2.Button2Click(Sender: TObject);
58 var
59 arrList: TList<TpgInfo>;
60 aRec: TpgInfo;
61 begin
62 arrList := TList<TpgInfo>.Create;
63 aRec.aName := 'A';
64 aRec.aID := '1';
65 aRec.aPID := '0';
66 arrList.Add(aRec);
67 aRec.aName := 'B';
68 aRec.aID := '2';
69 aRec.aPID := '0';
70 arrList.Add(aRec);
71 aRec.aName := 'C';
72 aRec.aID := '3';
73 aRec.aPID := '0';
74 arrList.Add(aRec);
75 aRec.aName := 'AA';
76 aRec.aID := '4';
77 aRec.aPID := '1';
78 arrList.Add(aRec);
79 aRec.aName := 'BB';
80 aRec.aID := '5';
81 aRec.aPID := '2';
82 arrList.Add(aRec);
83 add2tv(arrList);
84 FreeAndNil(arrList);
85 end;
86 //别忘了释放
87 if Assigned(TreeView1) then
88 for i := 0 to TreeView1.Items.Count - 1 do
89 Dispose(PpgInfo(TreeView1.Items[i].Data));
效果图:
算法效率比较低~