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

RemoveNode makes bad call to RecalculateMaxEnd #2

Open
aerickson-clt opened this issue Mar 23, 2018 · 0 comments
Open

RemoveNode makes bad call to RecalculateMaxEnd #2

aerickson-clt opened this issue Mar 23, 2018 · 0 comments

Comments

@aerickson-clt
Copy link

The call to RecalculateMaxEnd occurs in a loop and goes up the tree, but RecalculateMaxEnd also does this. It seems that the extra while loop is redundant.

                while (node.Parent != Sentinel) {
                    node = node.Parent;
                    node.RecalculateMaxEnd();
                }

More context below.

private void RemoveNode(IntervalNode<T> node)
        {
            if (node == Sentinel) {
                return;
            }

            IntervalNode<T> temp = node;
            if (node.Right != Sentinel && node.Left != Sentinel) {
                // Trick when deleting node with both children, switch it with closest in order node
                // swap values and delete the bottom node converting it to other cases

                temp = node.GetSuccessor();
                node.Interval = temp.Interval;

                node.RecalculateMaxEnd();
                while (node.Parent != Sentinel) {
                    node = node.Parent;
                    node.RecalculateMaxEnd();
                }
            }
            node = temp;
            temp = node.Left != Sentinel ? node.Left : node.Right;

            // we will replace node with temp and delete node
            temp.Parent = node.Parent;
            if (node.IsRoot) {
                Root = temp; // Set new root
            } else {

                // Reattach node to parent
                if (node.ParentDirection == NodeDirection.RIGHT) {
                    node.Parent.Left = temp;
                } else {
                    node.Parent.Right = temp;
                }

                IntervalNode<T> maxAux = node.Parent;
                maxAux.RecalculateMaxEnd();
                while (maxAux.Parent != Sentinel) {
                    maxAux = maxAux.Parent;
                    maxAux.RecalculateMaxEnd();
                }
            }

            if (node.Color == NodeColor.BLACK) {
                RenewConstraintsAfterDelete(temp);
            }
             
        }
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