59 #if 0 //mtoc++: 'get.Height'
61 h = this.height(this.root);
68 #if 0 //mtoc++: 'get.Values'
70 v = this.getvalues(this.root);
77 if ~isempty(this.root)
88 if numel(key) ~= numel(value)
89 error(
" If key and value arguments are given they must have the same number of elements. ");
94 fprintf(
" Filling tree (%d values).. ",m);
96 this.root= this.insert(this.root, key(idx), value(idx));
105 for idx=1:numel(value)
106 this.root= this.insert(this.root, key(idx), value(idx));
113 values = this.find(this.root, keys);
129 [
l, u] = this.findclosest(this.root, keys);
134 [
l(idx), u(idx)] = this.findclosest(this.root, keys(idx));
137 [
l, u] = this.findclosestmulti(repmat(this.root,1,length(keys)), keys);
143 this.printTree(this.root,0);
150 function value = find(n,key) {
173 function [
l , u ] = findclosest(n,key) {
210 function [l , u ] = findclosestmulti(n,keys) {
216 lempt = cellfun(
" isempty ",[n.left]);
217 rempt = cellfun(
" isempty ",[n.right]);
219 low = lt(keys,[n.Key]);
220 gre = gt(keys,[n.Key]);
225 golow = low & ~lempt;
227 n(golow) = [n(golow).left];
231 n(goup) = [n(goup).right];
235 u(
eq) = [n(
eq).Value];
236 l(
eq) = [n(
eq).Value];
238 if
all(~golow & ~goup)
242 hasu = ~isnan([minu.Key]);
243 u(hasu) = [minu(hasu).Value];
244 hasl = ~isnan([maxl.Key]);
245 l(hasl) = [maxl(hasl).Value];
257 function v = getvalues(n) {
260 v = [this.getvalues(n.left) n.Value this.getvalues(n.right)];
266 function n = insert(n,key,value) {
270 n.left= this.insert(n.left, key, value);
271 if n.left.height - this.height(n.right)== 2
272 if lt(key, n.left.Key)
273 n = this.rotateWithLeftChild(n);
275 n = this.doubleWithLeftChild(n);
278 elseif gt(key, n.Key)
279 n.right= this.insert(n.right, key, value);
280 if n.right.height - this.height(n.left) == 2
281 if gt(key, n.right.Key)
282 n = this.rotateWithRightChild(n);
284 n = this.doubleWithRightChild(n);
288 error(" Duplicate entry! ");
290 n.height= max(this.height(n.left), this.height(n.right)) + 1;
294 function h = height(n) {
303 function n = rotateWithLeftChild(n2) {
307 n2.height= max(this.height(n2.left),
this.height(n2.right)) + 1;
308 n.height= max(this.height(n.left), n2.height) + 1;
312 function n = rotateWithRightChild(n2) {
316 n2.height= max(this.height(n2.left),
this.height(n2.right)) + 1;
317 n.height= max(this.height(n.right), n2.height ) + 1;
321 function n = doubleWithLeftChild(n) {
322 n.left= this.rotateWithRightChild(n.left);
323 n = this.rotateWithLeftChild(n);
327 function n = doubleWithRightChild(n) {
328 n.right= this.rotateWithLeftChild(n.right);
329 n = this.rotateWithRightChild(n);
333 function printTree(n,ntabs) {
335 this.printTree(n.left,ntabs+1);
336 fprintf([repmat(" \
t ",1,ntabs) " %f => %f\n "], n.Key, n.Value);
337 this.printTree(n.right,ntabs+1);
357 for idx = 1:length(
k)
358 res = res && v(idx) == t.Find(
k(idx));
361 res = res && isempty(t.Find(2*n));
373 res = res &&
all(
sort(v) == t.Values);
377 res = res && ~isempty(t.Find(value));
383 res = res && hlp <= 1;
386 ex = randperm(n-1)+1;
387 ex = ex(1:round(n/2));
392 [
l,u] = t.FindClosest((rl+ru)/2);
393 res = res && l == rl && u == ru;
396 M = [2, 10, 100, 1000, 10000];
397 R = [100, 60, 40, 20, 10];
401 k = linspace(0,n+1,m);
403 for idx = 1:length(
k)
404 [
l,u] = t.FindClosest(
k(idx));
411 [
l,u] = t.FindClosest(
k);
414 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));
417 fprintf(
" BinTree test finished with n=%d, h=%d, n/2^(h+1)=%f <! 1\n ",n,h,hlp);
function [ l , u ] = FindClosest(keys)
BinTree: A weighted binary tree.
sort
ort the handle objects in any array in ascending or descending order.
function values = Find(keys)
if numel(keys) > 1 values = this.findMulti(repmat(this.root,1,length(keys)), keys); else ...
function Insert(key, value)
Matlab's base handle class (documentation generation substitute)
static function res = test_BinTree()
eq
Relational functions example. See details for more information.
Global configuration class for all KerMor run-time settings.
static function KerMor theinstance = App()
The singleton KerMor instance.