H
Hieran_Del8
Guest
EDIT: My apologies, I posted in the wrong forum. I Meant this for "Programming".
I'm creating a tree structure, and wanted to set the leaf nodes to point to their parent, using something akin to the this keyword. As of now, I had written:
But I'm not confident that returning self or setting the child's parent to be self actually is anything but the constant -1. I realize in with statements, there's special interpretation of what self means, but am I using self correctly so far?
I'm creating a tree structure, and wanted to set the leaf nodes to point to their parent, using something akin to the this keyword. As of now, I had written:
GML:
function _tree_node(_data) constructor {
parent = undefined;
children = [];
data = _data;
appendNode = function (node) {
array_push(children, node);
node.parent = self;
return self;
}
insertNode = function (node, pos) {
array_insert(children, pos, node);
node.parent = self;
return self;
}
removeNode = function (node) {
for (var i = array_length(children) - 1; i >= 0; i--) {
if (children[i] == node) {
node.parent = undefined;
array_delete(children, i, 1);
break;
}
}
return self;
}
copy = function () {
var newNode = new _tree_node(data);
var childCount = array_length(children);
for (var i = 0; i < childCount; i++) {
var newChild = children[i].copy();
newNode.appendNode(newChild);
}
return newNode;
}
}
function _tree() : _tree_node(undefined) constructor {
copy = function () {
var newNode = new _tree();
var childCount = array_length(children);
for (var i = 0; i < childCount; i++) {
var newChild = children[i].copy();
newNode.appendNode(newChild);
}
return newNode;
}
}
Last edited by a moderator: