samspade
Member
I'm working on a tree data structure tutorial and I thought I should post the code here first to see if anyone wanted to offer feedback on whether this is a good way to do it or whether I would be steering people the wrong way.
Here's the basic core code to create and add:
It would probably be easier to create a tree off of maps, but I thought arrays would be more interesting require less background as I wouldn't have to explain about data structure clean up.
Here's the basic core code to create and add:
GML:
/// @function tree_create()
/// @description Creates and returns a tree
return [];
/// @function tree_node_add(tree, data, [position])
/// @param {array} tree
/// @param {variable} data
/// @param {variable} data
/// @description add a node
#region rename for ease of use
var _tree, _data, _position;
_tree = argument[0];
_data = argument[1];
_position = 0;
if (argument_count == 3) {
_position = argument[2];
}
#endregion
if (_position == 0) {
array_add_to_end(_tree, tree_node_create(_data));
} else {
var _new_node, _parent_node;
_new_node = tree_node_create(_data);
_parent_node = tree_node_get(_tree, _position);
array_add_to_end(_parent_node[tree_node.children], _new_node);
}
/// @function tree_node_create(data)
/// @param {variable} data
/// @description Creates a node
/*
node = [
data = data,
children = [
node,
...,
node
]
]
*/
enum tree_node {
data,
children
}
return [argument0, []];