-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTree.m
More file actions
117 lines (96 loc) · 2.21 KB
/
Tree.m
File metadata and controls
117 lines (96 loc) · 2.21 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
classdef Tree < handle
properties
node
value
expanded = false;
childArr = Tree.empty
end
methods
function t = Tree(node, varargin)
if nargin == 0
return;
end
t.node = node;
for i = numel(varargin):-1:1
if isa(varargin{i}, 'Tree')
t.childArr(i) = varargin{i};
else
error('[Tree] %dth argument is not a tree', (i+1));
end
end
end
function n = numChildren(t)
n = numel(t.childArr);
end
function n = getNode(t)
n = t.node;
end
function v = getValue(t)
v = t.value;
end
function setValue(t, v)
t.value = v;
end
function expanded = isExpanded(t)
expanded = t.expanded;
end
function setExpanded(t, expanded)
t.expanded = expanded;
end
function c = getChild(t, i)
c = [];
if i >= 0 && i <= t.numChildren()
c = t.childArr(i);
else
error('[Tree.getChild] Tree does not have %d children', i)
end
end
function setChild(t, i, c)
if i <= 0
error('[Tree.setChild] Unexpected index: %d', i);
end
if isa(c, 'Tree')
t.childArr(i) = c;
else
error('[Tree.setChild] Child argument is not a Tree object');
end
end
function lf = isLeaf(t)
lf = isempty(t.childArr);
end
function d = depth(t)
if isempty(t.childArr)
d = 1;
else
d = 1 + max(arrayfun(@depth, t.childArr));
end
end
function count = numLeaves(t)
if isempty(t.childArr)
count = 1;
else
count = sum(arrayfun(@numLeaves, t.childArr));
end
end
% Sequence of nodes leading to each leaf
function pathCA = getLeafPaths(t)
pathCA = cell(t.numLeaves(), 1);
pathCAIndex = 1;
% Allocate array to hold sequence of node values representing a path
path(t.depth()) = t.node;
depthFirstTraversal(t, 1);
function depthFirstTraversal(tr, currDepth)
path(currDepth) = tr.node;
if isempty(tr.childArr)
% A leaf has been reached, store path in pathCA
pathCA{pathCAIndex} = path(1:currDepth);
pathCAIndex = pathCAIndex + 1;
else
for i = 1:tr.numChildren()
depthFirstTraversal(tr.getChild(i), currDepth+1);
end
end
end
end
end % methods
end % classdef