-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetdepth.m
41 lines (32 loc) · 843 Bytes
/
getdepth.m
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
function depth=getdepth(expr)
%GETDEPTH GPTIPS function to returns the tree depth of a GP expression.
%
% [DEPTH]=GETDEPTH(EXPR) returns the DEPTH of the tree expression EXPR.
%
% Remarks:
% A single node is considered to be a tree of depth 1.
%
% (c) Dominic Searson 2009
%
% v1.0
%
% See also GETNUMNODES
% workaround for arity zero functions, which are represented by f()
% string replace '()' with the empty string
expr=strrep(expr,'()','');
open_br=findstr(expr,'(');
close_br=findstr(expr,')');
num_open=numel(open_br);
if ~num_open %i.e. a single node
depth=1;
return
elseif num_open==1
depth=2;
return
else
%depth = max consecutive number of open brackets+1
br_vec=zeros(1,length(expr));
br_vec(open_br)=1;
br_vec(close_br)=-1;
depth=max(cumsum(br_vec))+1;
end