node satisfies the AVL invariant, and its height is one greater than . The increase in height can increase the height of its ancestors, possibly invalidating the AVL invariant of those nodes. This can be fixed either with a double rotation if invalid at the parent or a single left rotation if invalid higher in the tree, in both cases restoring the height for any further ancestor nodes. Join will therefore require at most two rotations. The cost of this function is the difference of the heights between the two input trees. function JoinRightAVL(TL, k, TR) (l, k', c) = expose(TL) if (Height(c) <= Height(TR)+1) T' = Node(c, k, TR) if (Height(T') <= Height(l)+1) then return Node(l, k', T') else return rotateLeft(Node(l, k', rotateRight(T'))) else T' = JoinRightAVL(c, k, TR) T'' = Node(l, k', T') if (Height(T') <= Height(l)+1) return T'' else return rotateLeft(T'') function JoinLeftAVL(TL, k, TR) /* symmetric to JoinRightAVL */ function Join(TL, k, TR) if (Height(TL)>Height(TR)+1) return JoinRightAVL(TL, k, TR) if (Height(TR)>Height(TL)+1) return JoinLeftAVL(TL, k, TR) return Node(TL, k, TR) Here Height(v) is the height of a subtree (node) . (l,k,r) = expose(v) extracts 's left child , the key of 's root, and the right child . Node(l,k,r) means to create a node of left child , key , and right child . To split an AVL tree into two smaller trees, those smaller than key , and those larger than key , first draw a path from the root by inserting into the AVL. After this insertion, all values less than will be found on the left of the path, and all values greater than will be found on the right. By applying Join, all the subtrees on the left side are merged bottom-up using keys on the path as intermediate nodes from bottom to top to form the left tree, and the right part is asymmetric. The cost of Split is , order of the height of the tree. function Split(T, k) if (T = nil) return (nil, false, nil) (L,m,R) = expose(T) if (k = m) return (L, true, R) if (k<m) (L',b,R') = Split(L,k) return (L', b, Join(R', m, R)) if (k>m) (L',b,R') = Split(R, k) return (Join(L, m, L'), b, R')) The union of two AVL trees and representing sets and , is an AVL that represents . function Union(t1, t2): if t1 = nil: return t2 if t2 = nil: