Skip to content

RemoveNode makes bad call to RecalculateMaxEnd #2

@aerickson-clt

Description

@aerickson-clt

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);
            }
             
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions