예제 #1
0
 private AvlTreeNode<T> insert(T x, AvlTreeNode<T> t, int[] countLarger) {
   if (t == null) {
     return new AvlTreeNode<>(x);
   }
   int compareRes = x.compareTo(t.val);
   if (compareRes < 0) {
     countLarger[0] += getSizeOfSubtree(t.right) + 1;
     t.left = insert(x, t.left, countLarger);
   } else if (compareRes > 0) {
     t.right = insert(x, t.right, countLarger);
   }
   t.height =
       1 + Math.max(t.left == null ? 0 : t.left.height, t.right == null ? 0 : t.right.height);
   t.sizeOfSubtree = 1 + getSizeOfSubtree(t.left) + getSizeOfSubtree(t.right);
   return balance(t);
 }
예제 #2
0
 private AvlTreeNode<T> remove(T x, AvlTreeNode<T> t) {
   if (t == null) {
     return null;
   }
   int compareRes = x.compareTo(t.val);
   if (compareRes < 0) {
     t.left = remove(x, t.left);
   } else if (compareRes > 0) {
     t.right = remove(x, t.right);
   } else if (t.left != null && t.right != null) {
     t.val = findMin(t.right).val;
     t.right = remove(t.val, t.right);
   } else {
     t = t.left != null ? t.left : t.right;
   }
   return balance(t);
 }