-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinTree.m
More file actions
362 lines (323 loc) · 11.7 KB
/
BinTree.m
File metadata and controls
362 lines (323 loc) · 11.7 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
classdef BinTree < handle
% BinTree: A weighted binary tree.
%
% The BinTreeNode have a key and value property. The key values used only must
% implement the lt/gt/eq methods, so either be native data-types or objects. Strings cannot be used
% as indices for this data structure.
%
% @author Daniel Wirtz @date 2011-05-16
%
% @change{0,5,dw,2011-07-07} Bugfix in FindClosest: If the root key was already lower or bigger than
% all following nodes, an empty node instead of the root node was returned, leading to an error. Now
% the lower and upper closest nodes are always initialized to the root node.
%
% @new{0,4,dw,2011-05-16} Added this class.
%
% This class is part of the framework
% KerMor - Model Order Reduction using Kernels:
% - \c Homepage http://www.morepas.org/software/index.html
% - \c Documentation http://www.morepas.org/software/kermor/index.html
% - \c License @ref licensing
properties(Access=private)
root = [];
end
properties(Dependent)
Height;
Values;
end
methods
function h = get.Height(this)
h = this.height(this.root);
end
function v = get.Values(this)
v = this.getvalues(this.root);
end
function clear(this)
if ~isempty(this.root)
this.root.delete;
this.root = [];
end
end
function Insert(this, key, value)
if nargin < 3
value = key;
end
if numel(key) ~= numel(value)
error('If key and value arguments are given they must have the same number of elements.');
end
if KerMor.App.Verbose > 2
cnt = 0; cp = 0;
m = numel(value);
fprintf('Filling tree (%d values).. ',m);
for idx=1:m
this.root = this.insert(this.root, key(idx), value(idx));
p = round(idx*100/m);
if p >= cp
fprintf('%d%% ',p);
cp = cp+10;
end
end
fprintf('\n');
else
for idx=1:numel(value)
this.root = this.insert(this.root, key(idx), value(idx));
end
end
end
function values = Find(this, keys)
% if numel(keys) > 1
% values = this.findMulti(repmat(this.root,1,length(keys)), keys);
% else
values = this.find(this.root, keys);
% end
end
function [l, u] = FindClosest(this, keys)
n = numel(keys);
if n == 1
[l, u] = this.findclosest(this.root, keys);
elseif n < 15
l = zeros(1:n);
u = zeros(1:n);
for idx=1:n
[l(idx), u(idx)] = this.findclosest(this.root, keys(idx));
end
else
[l, u] = this.findclosestmulti(repmat(this.root,1,length(keys)), keys);
end
end
function display(this)
this.printTree(this.root,0);
end
end
methods(Access=private)
function value = find(this, n, key)%#ok (not needed as nonrecursive)
% Performs an in-place search for the value associated with the given key.
value = [];
while ~isempty(n)
if lt(key,n.Key)
n = n.left;
elseif gt(key,n.Key)
n = n.right;
else
value = n.Value;
return;
end
end
end
function [l,u] = findclosest(this, n, key)%#ok (not needed as nonrecursive)
% Performs an in-place search for the value associated with the given key.
u = []; l = [];
% minu = [];
% maxl = [];
% Initialize with this node n, as it might be the smallest/largest
minu = n;
maxl = n;
while ~isempty(n)
if lt(key,n.Key)
minu = n;
n = n.left;
elseif gt(key,n.Key)
maxl = n;
n = n.right;
else
l = n.Value;
u = l;
return;
end
end
if ~isempty(minu)
u = minu.Value;
end
if ~isempty(maxl)
l = maxl.Value;
end
end
function [l,u] = findclosestmulti(this, n, keys)%#ok (not needed as nonrecursive)
% Performs an in-place search for the value associated with the given key.
u = inf(size(keys));
l = -u;
minu = BinTreeNode.empty(0,1);
maxl = minu;
while true
lempt = cellfun('isempty',{n.left});
rempt = cellfun('isempty',{n.right});
low = lt(keys,[n.Key]);
gre = gt(keys,[n.Key]);
minu(low) = n(low);
maxl(gre) = n(gre);
golow = low & ~lempt;
if any(golow)
n(golow) = [n(golow).left];
end
goup = gre & ~rempt;
if any(goup)
n(goup) = [n(goup).right];
end
eq = ~low & ~gre;
if any(eq)
u(eq) = [n(eq).Value];
l(eq) = [n(eq).Value];
end
if all(~golow & ~goup)
break;
end
end
hasu = ~isnan([minu.Key]);
u(hasu) = [minu(hasu).Value];
hasl = ~isnan([maxl.Key]);
l(hasl) = [maxl(hasl).Value];
end
function v = getvalues(this, n)
v = [];
if ~isempty(n)
v = [this.getvalues(n.left) n.Value this.getvalues(n.right)];
end
end
% function values = findMulti(this, n, keys)%#ok (not needed as nonrecursive)
% % Performs an in-place search for the value associated with the given key.
% values = nan(size(keys));
% while ~isempty(n)
% low = lt(keys,[n(:).Key]);
% if any(low)
% n(low) = n(low).left;
% end
% gre = gt(keys,[n(:).Key]);
% if any(gre)
% n(gre) = n(gre).right;
% end
% eq = ~low & ~gre;
% if any(eq)
% values(eq) = [n(eq).Value];
% end
% if all(eq)
% return
% end
% end
% end
function n = insert(this, n, key, value)
if isempty(n)
n = BinTreeNode(key, value);
elseif lt(key,n.Key)
n.left = this.insert(n.left, key, value);
if n.left.height - this.height(n.right)== 2
if lt(key, n.left.Key)
n = this.rotateWithLeftChild(n);
else
n = this.doubleWithLeftChild(n);
end
end
elseif gt(key, n.Key)
n.right = this.insert(n.right, key, value);
if n.right.height - this.height(n.left) == 2
if gt(key, n.right.Key)
n = this.rotateWithRightChild(n);
else
n = this.doubleWithRightChild(n);
end
end
else
error('Duplicate entry!');
end
n.height = max(this.height(n.left), this.height(n.right)) + 1;
end
function h = height(this, n)%#ok
if isempty(n)
h = -1;
else
h = n.height;
end
end
function n = rotateWithLeftChild(this, n2)
n = n2.left;
n2.left = n.right;
n.right = n2;
n2.height = max(this.height(n2.left), this.height(n2.right)) + 1;
n.height = max(this.height(n.left), n2.height) + 1;
end
function n = rotateWithRightChild(this, n2)
n = n2.right;
n2.right = n.left;
n.left = n2;
n2.height = max(this.height(n2.left), this.height(n2.right)) + 1;
n.height = max(this.height(n.right), n2.height ) + 1;
end
function n = doubleWithLeftChild(this, n)
n.left = this.rotateWithRightChild(n.left);
n = this.rotateWithLeftChild(n);
end
function n = doubleWithRightChild(this, n)
n.right = this.rotateWithLeftChild(n.right);
n = this.rotateWithRightChild(n);
end
function printTree(this, n, ntabs)
if ~isempty(n)
this.printTree(n.left,ntabs+1);
fprintf([repmat('\t',1,ntabs) '%f => %f\n'], n.Key, n.Value);
this.printTree(n.right,ntabs+1);
end
end
end
methods(Static)
function res = test_BinTree
res = true;
%% Init
t = BinTree;
n = 2^4;
%% Test usage as Key-Value BinaryTree
k = randperm(n);
v = randperm(n);
t.Insert(k,v);
% Find all values for keys
for idx = 1:length(k)
res = res && v(idx) == t.Find(k(idx));
end
% Test nonexistent key
res = res && isempty(t.Find(2*n));
% Test multi-find
% vs = t.Find([k n+1]);
% res = res && all(v == vs(1:end-1)) && isnan(vs(end));
%% Test usage as simple BinaryTree
t.clear;
v = randperm(n);
t.Insert(v);
% Check for correct ordering
res = res && all(sort(v) == t.Values);
% Find all values
for value=v
res = res && ~isempty(t.Find(value));
end
% Height check
h = t.Height;
hlp = n / (2^(h+1));
res = res && hlp <= 1;
% Closeness check - select some random positions and check
ex = randperm(n-1)+1;
ex = ex(1:round(n/2));
ks = sort(k);
for i=1:length(ex)
ru = ks(ex(i));
rl = ru-1;
[l,u] = t.FindClosest((rl+ru)/2);
res = res && l == rl && u == ru;
end
M = [2, 10, 100, 1000, 10000];
R = [100, 60, 40, 20, 10];
for j = 1:length(R)
m = M(j);
r = R(j);
k = linspace(0,n+1,m);
tmp = tic;
for idx = 1:length(k)
[l,u] = t.FindClosest(k(idx)); %#ok<*NASGU>
end
ti(1,j) = toc(tmp); %#ok<*AGROW>
tmp = tic;
[l,u] = t.FindClosest(k);
ti(2,j) = toc(tmp);
ti = sum(ti,2)/r;
fprintf('Speed test of multi-find-closest of %d values (averaged over %d runs): %fs loop to %fs multi (factor %f faster).\n',m,r,ti(1),ti(2),ti(1)/ti(2));
end
fprintf('BinTree test finished with n=%d, h=%d, n/2^(h+1)=%f <! 1\n',n,h,hlp);
end
end
end