1
var LockingTree = function (parent) {4
this.locked = new Map();6
for (let i = 0; i < this.parents.length; ++i) {10
for (let i = 1; i < this.parents.length; ++i) {11
const parent = this.parents[i];13
this.children[parent].push(i);17
LockingTree.prototype.lock = function (num, user) {18
if (this.locked.has(num)) return false;20
this.locked.set(num, user);25
LockingTree.prototype.unlock = function (num, user) {26
if (!this.locked.has(num) || this.locked.get(num) != user) return false;28
this.locked.delete(num);33
LockingTree.prototype.upgrade = function (num, user) {34
let isLocked = traverseUp(num, this.parents, this.locked);36
if (isLocked) return false;44
while (queue.length > 0) {45
const node = queue.shift();47
if (node != num && this.locked.has(node)) {49
this.locked.delete(node);52
for (let i = 0; i < this.children[node].length; ++i) {53
queue.push(this.children[node][i]);57
if (!isLocked) return false;59
this.locked.set(num, user);63
function traverseUp(num, parents, locked) {64
if (locked.has(num)) return true;65
if (num === 0) return false;67
const parentIdx = parents[num];69
return traverseUp(parentIdx, parents, locked);