tree_new.m 4.23 KB
Newer Older
Brad Kennedy's avatar
Brad Kennedy committed
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
% tree_new() - returns a new blank tree structure that has methods that allow
%              operations on the tree
%
% Usage:
%   >>  tree = tree_new();
%   >>  [element] = tree.get(tree, elementid)
%   >>  [tree, node] = tree.add(tree, parent, element)
%   >>  [tree] = tree.remove(tree, elementid)
%   >>  [children] = tree.get_children(tree, elementid);
%   >>  [tree] = tree.garbage_collect();
%          - cleans the tree structure if necessary
%   
%
% Inputs:
%   tree     - the tree structure
%   element  - the element to be stored
%   elementid- target element id
%   parent   - the element id of the parent
%    
% Outputs:
%   tree     - the tree structure with member functions
%   element  - stored element in the tree
%   node     - element id of the new node
%   children - cell array of children element ids
%

% Copyright (C) 2017 Brock University Cognitive and Affective Neuroscience Lab
%
% Code written by Brad Kennedy
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program (LICENSE.txt file in the root directory); if not, 
% write to the Free Software Foundation, Inc., 59 Temple Place,
% Suite 330, Boston, MA  02111-1307  USA


function res = tree_new()
    res = struct();

    % Used elements
    res.numelements = 0;
    % Storage for elements
    res.elements = {};

    root = new_node();
    res.elements{end+1} = root;
    res.numelements = 1;

    res.get = @tree_get;
    res.add = @tree_add;
    res.remove = @tree_remove;
    res.get_children = @tree_get_children;
    res.garbage_collect = @tree_garbage_collect;
    res.get_children_contents_of_match = @tree_get_children_contents_of_match;
end

function [element] = tree_get(tree, elementid)
    if nargin ~= 2
        error('You need 2 arguments for tree_get');
    end
    tree_exists(tree, elementid);
    if iscell(elementid)
        element = {[tree.elements([elementid{:}]){:}].element};
        return
    end
    element = tree.elements{elementid}.element;
end

function [tree, node] = tree_add(tree, parent, element)
    if nargin ~= 3
        error('You need 3 arguments for tree_add');
    end
    tree_exists(tree, parent);

    new_node = new_node();
    % end+1
    node = numel(tree.elements)+1;
    tree.elements{parent}.children{end+1} = node;
    new_node.element = element;
    tree.elements{node} = new_node;
    tree.numelements += 1;
end

function [tree] = tree_remove(tree, element)
    error('Not yet implemented');
end

function [children] = tree_get_children(tree, element)
    if nargin ~= 2
        error('You need 2 arguments for tree_get_children');
    end
    children = tree.elements{element}.children;
end

function [elements] = tree_get_children_contents_of_match(tree, ...
        comparator, parent)
    potchildren = tree.get_children(tree, parent);
    potchildren = potchildren(arrayfun(comparator, tree.get(tree, potchildren)));
    if numel(potchildren) ~= 1
        error('potential children not unique');
    end
    % Get children numbers
    cid = tree.get_children(tree, potchildren{1});
    elements = tree.get(tree, cid);
end

function [tree] = tree_garbage_collect()
    error('Not yet implemented');
end

function tree_exists(tree, element)
    % Verify element (a number) exists in the tree
    if iscell(element)
        for i=1:numel(element)
            if numel(tree.elements) >= element{i} ... 
                    && isempty(tree.elements{element{i}})
                error(sprintf('element %d is not in tree', element{i}));
            end
        end
    else
        if numel(tree.elements) >= element && isempty(tree.elements{element})
            error(sprintf('element %d is not in tree', element));
        end
    end
end

function [treenode] = new_node()
    treenode = struct();
    treenode.children = {};
    treenode.element = [];
end