-
Notifications
You must be signed in to change notification settings - Fork 802
/
Copy paththinTree.m
88 lines (75 loc) · 2.62 KB
/
thinTree.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
classdef thinTree
% Attributes
properties (SetAccess = private)
nodes = { [] }; % Array of the nodes
depth = 0; % Depth of the tree
w = 0 % Number of parents for each node
links = []; % The matrix representing the links between the nodes
end
% Methods
methods
% Constructor
function [obj root_ID] = thinTree(d, w)
% If 0 input arguments, assume d = 1 and w = 1
if nargin < 1
[obj root_ID] = thinTree(1, 1);
return
end
% If 1 input argument, assume w = 1
if nargin < 1
[obj root_ID] = thinTree(d, 1);
return
end
% Else
if w > d-1
error('MATLAB:thinTree:thinTree', ...
'Cannot have %d parents on a binary tree of depth %d. You must have nParents < %d here.\n', w, d, d);
end
root_ID = 1;
obj.nodes = cell(2^d - 1,1); % Creation of the d^2 empty cells
obj.depth = d;
obj.w = w;
obj.links = eye(2^d - 1); % Creation of the links matrix
% Link the cells
end
% Function to add a content for a specific node
function [obj] = addContent(obj, content, nodeID)
obj.nodes{nodeID} = content;
return
end
% Function to return the ID's of a node's parents
function ids = getParents(obj, nodeID)
% Initialisation
node = nodeID;
depthOfNode = obj.getNodeDepth(nodeID);
if depthOfNode == 1
ids = 1;
else
ids = zeros(1,min(obj.w, depthOfNode-1));
% Loop on w, the number of parents associated to one node
for i=1:min(obj.w, depthOfNode-1)
ids(i) = floor(node/2);
node = floor(node/2);
end
end
% Return
return
end
% Accessors
function output = getDepth(obj)
output = obj.depth;
return
end
function output = getW(obj)
output = obj.w;
return
end
function output = getNumberOfElements(obj)
output = 2^obj.depth - 1;
end
% Returns the depth of a node
function output = getNodeDepth(obj, nodeID)
output = ceil(log(nodeID+1)/log(2));
end
end % Methods
end % Class