MATLAB operators for trees
Contents
I did my best to stick with the MATLAB touch when designing the tree class. As a result, you can use trees as if they were regular MATLAB arrays. Most of the standard operators are implemented, and have their element-wise meaning, with scalar expansion if needed. For instance:
% Grab the duration example tree, which holds only numerical data
[ lineage, duration ] = tree.example;
a = ( ( duration .* (100 - duration.^2) ./ 5 ) ) < - duration;
disp(a.tostring)
false +---------------------------------------+-------------------------+ | | false false +-----------+------------+ +------------------+---------------------+ | | | | false false false false +-----+------+ +------+-----+ +-----------+---------+ +-----------+------+ | | | | | | | | true false false true false true false false +--+--+ +--+---+ +--+--+ +--+---+ +--+------+ +-----+------+ +--+---+ +----+------+ | | | | | | | | | | | | | | | | true true true false true true false false true false false true true false true true +--+--+ +--+--+ +--+---+ +-+---+ +--+--+ | | | | | | | | | | true true true true true false false true true true
Of course, all will be well if the content of all nodes allows these operations. If you have heterogeneous data, there might be errors. If you combine two tree with different structures or node orders, there might be unexpected results. There is no check against pathological operations; we again assume you know what you are doing.
List of implement operators
Here is a brief list of implemented operators, with their common meaning. We assume that a is a tree and that b is either another tree or a scalar number. All binary operators support scalar expansion.
a + b | Binary addition |
a - b | Binary subtraction |
-a | Unary minus |
+a | Unary plus |
a.*b | Element-wise multiplication |
a./b | Right element-wise division |
a.\b | Left element-wise division |
a.^b | Element-wise power |
a < b | Less than |
a > b | Greater than |
a <= b | Less than or equal to |
a >= b | Greater than or equal to |
a ~= b | Not equal to |
a == b | Equality |
a & b | Logical AND |
a | b | Logical OR |
xor(a, b) | Logical exclusive OR |
~a | Logical NOT |
any(a) | True if any node content is a nonzero number |
all(a) | True if all node content are nonzero numbers |
Here is a list of operators that are not implemented, because they make sense only for matrices:
a*b | Matrix multiplication |
a/b | Matrix right division |
a\b | Matrix left division |
a^b | Matrix power |
There are others, such as trigonometric operators, that are not implemented yet, because I did not find a use for them in my work. Should you need them, press me.
Calling generic function on nodes content
Another solution, if you do not find happiness with the above operators, is to call a generic function on the nodes content. There is two methods to do that.
treefun apply a 1-argument function on the content of each node and generate a new tree holding the result. For instance:
t = lineage.subtree(19);
letterCount = t.treefun(@numel);
disp( [t.tostring letterCount.tostring ] ) % Count the number of letters in each string of the t tree
EMS 3 +-----------+-----+ +-----+--+ | | | | MS E 2 1 +--+--+ +-----+-----+ ++-+ +--+--+ | | | | | | | | MS.a MS.p E.a E.p 4 4 3 3 +--+--+ +--+--+ ++-+ ++-+ | | | | | | | | E.al E.ar E.pl E.pr 4 4 4 4
treefun2 does the same, but with two trees and a 2-arguments function:
lastLetter = t.treefun2(letterCount, @(a, b) a(b));
disp(lastLetter.tostring) % Extract the last letter
S +-----+--+ | | S E ++-+ +--+--+ | | | | a p a p ++-+ ++-+ | | | | l r l r
Misc functions
There is also a variety of methods that generate synchronized trees, made to simplify developping using the class:
- isemptynode return a logical tree where each node is true if the mathcine node in the source true is empty
Back to main page.