假設平衡因子是左子樹的高度減去右子樹的高度所得到的值,又假设由于在二叉排序树上插入节点而失去平衡的最小子树根节点的指针为a(即a是离插入点最近,且平衡因子绝对值超过1的祖先节点),则失去平衡后进行的规律可归纳为下列四种情况:
balance(null) -> null;
balance({null, _, null}=Tree) -> Tree;
balance({Left, Value, Right}=Tree) ->
Diff = count(Left)-count(Right),
if (Diff < 2) and (Diff > -2) -> {balance(Left), Value, balance(Right)};
(Diff > 1) -> balance(rotate_right(Tree));
(Diff< -1) -> balance(rotate_left(Tree));
true -> exit('This is impossible!')
end.
rotate_right({Left, Value, Right}) ->
merge_max(Left, {null, Value, Right}).
rotate_left({Left, Value, Right}) ->
merge_min(Right, {Left, Value, null}).
merge_min({null, Value, Right}, Tree2) ->
{Tree2, Value, Right};
merge_min({Left, _, _}, Tree2) ->
merge_min(Left, Tree2).
merge_max({Left , Value, null}, Tree2) ->
{Left, Value, Tree2};
merge_max({_, _, Right}, Tree2) ->
merge_max(Right, Tree2).