Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avl-tree.js: deleting root value deletes the tree by setting root to undefined #115

Open
jmb20 opened this issue Jul 24, 2022 · 0 comments

Comments

@jmb20
Copy link

jmb20 commented Jul 24, 2022

The remove function of the AVL tree appears to delete the entire tree by setting the root property of the tree to undefined if the root value is removed.

This is because the function's call this.root = balanceUpstream( node.parent ); has parameter node.parent = null if node is the root (see commented out function call below). Function balanceUpstream will then immediately return undefined which remove assigns to this.root deleting the tree.

I have fixed this in my implementation of avl-tree.js using a ternary operator:

remove( value ) {
        const node = super.find( value );
        if ( node ) {
            const found = super.remove( value );
            const parent = node.parent ? node.parent : this.root;
            this.root = balanceUpstream( parent );
            //this.root = balanceUpstream( node.parent );
            return found;
        }
        return false;
    }

Posted as a (potential) issue as I am not sure whether this is an issue only in my implementation which differs somewhat from the one in this repository.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant