Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function findRoot(p){
for(var i = 0; i < p.length; i++){
if(i == p[i]){
//console.log("we found " + i);
return i;
}
}
}
function changeRoot(parent, newRoot) {
var historico = [];
var index = newRoot;
var root = 0;
var valor = parent[index];
var swap = newRoot;
var oldroot = findRoot(parent);
while(index != oldroot){
valor = parent[index];
console.log(index,parent[index],historico);
if(root < 2){
swap = swap;
root += 1;
historico.push(index);
}else{
swap = historico.pop();
}
historico.push(index);
parent[index] = swap;
index = valor;
//historico.push(valor);
//console.log(index);
}
console.log(index,valor,oldroot,swap,historico);
parent[index] = historico.pop();
return parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.