Bug while removing leaf node

Asked

Viewed 23 times

0

Good evening, folks. I’m having a bug in an algorithm that removes sheet node in javascript. The code is correct, but when executed, it says that the node has been removed. But when I check the binary tree, the node is there...

Code to remove the node:

remover(valor){
        if(this.raiz == undefined){
            return undefined;
        } else{
            this.removerNo(this.raiz,valor);
        }
    }

    removerNo(no, valor){
        if(no == undefined){
            console.log("Nó não encontrado");
        } else{
            if(valor == no.valor){
                console.log("O valor é igual ao valor do nó");
                if(no.left == undefined && no.right == undefined){
                    console.log("Nó removido");
                    no = undefined;
                    return no;
                }else{console.log("Apenas poderá ser removido nó folhas..."); return undefined;}
            } else{
                if(valor < no.valor){
                    console.log("Indo para o nó esquerdo");
                    this.removerNo(no.left,valor);
                } else{
                    console.log("Indo para o nó direito");
                    this.removerNo(no.right,valor);
                }
            }
        }
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.