+ // p-. or .-p p-. or .-p
+ // .-this-. .-right-.
+ // left .-right-. -> .-this-. c2
+ // c1 c2 left c1
+ private MInterval promote_right ()
+ {
+ int right_length = - right.total_start;
+ MInterval c1;
+
+ if (parent == null)
+ mt.root_interval = right;
+ else if (total_start == 0)
+ parent.left = right;
+ else
+ parent.right = right;
+ right.parent = parent;
+ c1 = right.left;
+ right.left = this;
+
+ parent = right;
+ right = c1;
+ if (c1 != null)
+ c1.parent = this;
+
+ parent.total_start = total_start;
+ parent.total_end = total_end;
+ total_start = 0;
+ total_end -= right_length - (c1 == null ? 0 : c1.total_end);
+
+ if (c1 != null)
+ {
+ c1.total_end = - c1.total_start;
+ c1.total_start = 0;
+ }
+ return parent;
+ }
+
+ // p-. or .-p p-. or .-p
+ // .-this-. .-left-.
+ // .-left-. .-right-. -> c1 .-this-.
+ // c1 c2 c2 right
+ private MInterval promote_left ()
+ {
+ int left_length = left.total_end;
+ MInterval c2;
+
+ if (parent == null)
+ mt.root_interval = left;
+ else if (total_start == 0)
+ parent.left = left;
+ else
+ parent.right = left;
+ left.parent = parent;
+ c2 = left.right;
+ left.right = this;
+
+ parent = left;
+ left = c2;
+ if (c2 != null)
+ c2.parent = this;
+
+ parent.total_start = total_start;
+ parent.total_end = total_end;
+ total_start -= left_length + (c2 == null ? 0 : c2.total_end);;
+ total_end = 0;
+
+ if (c2 != null)
+ {
+ c2.total_start = - c2.total_end;
+ c2.total_end = 0;
+ }
+ return parent;
+ }
+
+ private MInterval balance ()