private AANode<T> skew(AANode<T> node) { if (node == null) { return null; } else if (node.getRight() == null || node.getRight().getRight() == null) { return node; } else if (node.getLevel() == node.getRight().getRight().getLevel()) { AANode<T> right = node.getRight(); node.setRight(right.getLeft()); right.setLeft(node); right.setLevel(right.getLevel() + 1); return right; } else { return node; } }
private AANode<T> decreaseLevel(AANode<T> node) { int shouldBe = 0; if (node.getLeft() != null && node.getRight() != null) { shouldBe = Math.min(node.getLeft().getLevel(), node.getRight().getLevel()) + 1; } else if (node.getLeft() != null) { shouldBe = node.getLeft().getLevel() + 1; } else if (node.getRight() != null) { shouldBe = node.getRight().getLevel() + 1; } else { return node; } if (shouldBe < node.getLevel()) { node.setLevel(shouldBe); if (shouldBe < node.getRight().getLevel()) { node.getRight().setLevel(shouldBe); } } return node; }