constant B = "B", R = "R" function balance(sequence t) sequence s = match_algebraic({{B,{R,{R,0,0,0},0,0},0,0}, {B,{R,0,0,{R,0,0,0}},0,0}, {B,0,0,{R,{R,0,0,0},0,0}}, {B,0,0,{R,0,0,{R,0,0,0}}}},t) if length(s) then object {a,x,b,y,c,z,d} = s t = {R,{B,a,x,b},y,{B,c,z,d}} end if return t end function function ins(object tree, object leaf) if tree=NULL then tree = {R,NULL,leaf,NULL} else object {c,l,k,r} = tree if leaf!=k then if leaf