-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeParent.m
73 lines (59 loc) · 1.55 KB
/
nodeParent.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
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
function nodeParent = nodeParent( tree, label )
% parent = nodeParent( tree, label )
% Takes a tree and a label, finds the parent node in the tree and returns
%
% Argument:
% node - The tree
% label - The cpc label, node name, or node number
%
% Returns:
% node - The parent node
%
% author: Navid Nourani-Vatani @ ACFR
% version: 1.0 2013-08-15
verbose = false;
nodeList = {};
nodeList{1} = tree;
nodeNum = 0;
nodeParent{1} = tree;
nodeParent{1}.name = '';
nodeParent{1}.labels = {};
nodeParent{1}.children = {};
found = false;
while ~isempty(nodeList) && ~found
nodeCurr = nodeList{nodeNum+1};
if verbose == true
fprintf( '%2d - curr: %s\n', ...
nodeCurr.num, nodeCurr.name );
end
% is it a node number?
if isa( label, 'numeric' )
if nodeCurr.num == label
nodeParent = nodeParent{nodeNum+1};
break
end
else
% is it a node name?
if strcmpi( label, nodeCurr.name )
nodeParent = nodeParent{nodeNum+1};
break;
end
% is the label here?
for i = 1:length(nodeCurr.labels)
if strcmpi( label, nodeCurr.labels{i} )
found = true;
break;
end
end
end
if found
nodeParent = nodeParent{nodeNum+1};
return
end
for i = 1:length(nodeCurr.children)
nodeList{end+1} = nodeCurr.children{i};
nodeParent{end+1} = nodeCurr;
end
nodeNum = nodeNum + 1;
end
end