Considere o código de uma árvore implementado na linguagem Javascript, descrito a seguir:
class TreeNode {
constructor(value) {
this.value = value;
this.children = [];
}
addChild(child) {
this.children.push(child);
}
}
class Tree {
constructor(value) {
this.root = new TreeNode(value);
}
compute(value) {
if (!this.root) return null;
const queue = [this.root];
while (queue.length > 0) {
const current = queue.shift();
if (current.value === value) {
return current;
}
for (const child of current.children) {
queue.push(child);
}
}
return null;
}
}
O método compute do código é conhecido pelo acrônimo em inglês: