-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_tree_test.pas
141 lines (129 loc) · 3.52 KB
/
avl_tree_test.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
uses bn_avl_tree,math,sysutils,classes,BtrDbase,ObjectModel,Crypto;
var tree:tAvlTree;
type tItem = class(TAVLTreeItem)
Key: string[63];
//procedure Assign(const other: TAVLTreeNode); override;
function Compare(const other: TAVLTreeNode): integer; override;
function CompareKey(const other: TAVLTreeKey): integer; override;
constructor Create(const ikey: string);
function ToString: ansistring; override;
end;
function TItem. CompareKey(const other: TAVLTreeKey): integer;
begin
write('[CompareKey]');
result:=CompareStr(Key,string(other^));
end;
function TItem. Compare(const other: TAVLTreeNode): integer;
begin
write('[Compare]');
result:=CompareStr(Key,tItem(other).Key);
end;
function TItem. ToString: ansistring;
begin
result:=Format('%s(%s)',[classname,key]);
end;
constructor TItem. Create(const ikey: string);
begin
Key:=ikey;
end;
procedure AddSomeItems;
var item: tItem;
begin
tree:=tAVLTree.Create;
tree.Add(tItem.Create('peter'));
tree.Add(tItem.Create('sufusky'));
tree.Add(tItem.Create('kek'));
tree.Add(tItem.Create('zidan'));
end;
procedure Dump;
var item: tItem;
var ms:tMemoryStream;
var realstruct:ansistring;
begin
ms:=tMemoryStream.Create;
tree.WriteReportToStream(ms);
ms.position:=0;
SetLength(realstruct,ms.size);
ms.read(realstruct[1],ms.size);
writeln(realstruct);
ms.Free;
item:= tItem(tree.FindLowest);
while item<>nil do begin
writeln(item.Key);
item:=tItem(item.Successor);
end;
end;
procedure TryFind(key: shortstring);
var item: tItem;
begin
item:=tItem(tree.Find( pointer(@key) ));
if item<>nil then
writeln('for ',key,' found: ',item.ToString)
else
writeln('for ',key,' nothing found.');
end;
procedure TryFinds;
begin
TryFind('kek');
TryFind('vadim');
end;
procedure AssertStructure(const insert: ansistring; const struct: ansistring);
var s:tMemoryStream=nil;
var tree:tAvlTree=nil;
var node:tItem;
var i:longword;
var realstruct:ansistring;
begin
s:=tMemoryStream.Create;
tree:=tAvlTree.Create;
try
for i:=1 to length(insert) do begin
node:=tItem.Create(insert[i]);
try
tree.Add(node);
except
node.free;
raise;
end;
end;
tree.WriteSubtreeReport(s,tree.root);
if((s.size<>length(struct)) or (CompareByte(s.memory^,struct[1],s.size)<>0))
then begin
s.position:=0;
SetLength(realstruct,s.size);
s.read(realstruct[1],s.size);
raise Exception.CreateFmt('Tree structure mismatch. Expected %S got %S.',
[struct,realstruct]);
end;
finally
s.Free;
realstruct:='';
tree.Free;
end;
end;
procedure do_crc(what:shortstring);
var tmp: dword;
begin
tmp:=CRC32c(0, what[1], length(what));
writeln(what, ' $', ToHexStr(tmp,4));
end;
BEGIN
do_crc('123456789');
do_crc('The quick brown fox jumps over the lazy dog');
do_crc('');
do_crc(#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0);
do_crc('a');
do_crc('a'#0);
AddSomeItems;
Dump;
TryFinds;
tree.Clear;
tree.free;
AssertStructure('a','(tItem(a),00,-,-)');
AssertStructure('abc','(tItem(b),00,(tItem(a),00,-,-),(tItem(c),00,-,-))');
AssertStructure('acb','(tItem(b),00,(tItem(a),00,-,-),(tItem(c),00,-,-))');
AssertStructure('cab','(tItem(b),00,(tItem(a),00,-,-),(tItem(c),00,-,-))');
AssertStructure('cba','(tItem(b),00,(tItem(a),00,-,-),(tItem(c),00,-,-))');
AssertStructure('bac','(tItem(b),00,(tItem(a),00,-,-),(tItem(c),00,-,-))');
AssertStructure('bca','(tItem(b),00,(tItem(a),00,-,-),(tItem(c),00,-,-))');
end.