private AVLNode removeMin() { if (getLeftSubTree() == null) { return(removeSelf()); } setLeft(left.removeMin(), left.left); if (relativePosition > 0) { relativePosition--; } recalcHeight(); return(balance()); }
/** * Removes this node from the tree. * * @return the node that replaces this one in the parent */ private AVLNode removeSelf() { if (getRightSubTree() == null && getLeftSubTree() == null) { return(null); } if (getRightSubTree() == null) { if (relativePosition > 0) { left.relativePosition += relativePosition + (relativePosition > 0 ? 0 : 1); } left.max().setRight(null, right); return(left); } if (getLeftSubTree() == null) { right.relativePosition += relativePosition - (relativePosition < 0 ? 0 : 1); right.min().setLeft(null, left); return(right); } if (heightRightMinusLeft() > 0) { // more on the right, so delete from the right AVLNode rightMin = right.min(); value = rightMin.value; if (leftIsPrevious) { left = rightMin.left; } right = right.removeMin(); if (relativePosition < 0) { relativePosition++; } } else { // more on the left or equal, so delete from the left AVLNode leftMax = left.max(); value = leftMax.value; if (rightIsNext) { right = leftMax.right; } AVLNode leftPrevious = left.left; left = left.removeMax(); if (left == null) { // special case where left that was deleted was a double link // only occurs when height difference is equal left = leftPrevious; leftIsPrevious = true; } if (relativePosition > 0) { relativePosition--; } } recalcHeight(); return(this); }