Take the union of two treaps.
(tree-union x y) → tree
The result might not be a union if the input trees are not binary search trees.
Function:
(defun tree-union (x y) (declare (xargs :guard (and (binary-tree-p x) (binary-tree-p y)))) (let ((__function__ 'tree-union)) (declare (ignorable __function__)) (cond ((tree-emptyp x) (tree-fix y)) ((tree-emptyp y) (tree-fix x)) ((heap< (tree-head x) (tree-head y)) (b* (((mv - left right) (tree-split (tree-head y) x))) (tree-node (tree-head y) (tree-union left (tree-left y)) (tree-union right (tree-right y))))) (t (b* (((mv - left right) (tree-split (tree-head x) y))) (tree-node (tree-head x) (tree-union (tree-left x) left) (tree-union (tree-right x) right)))))))
Theorem:
(defthm binary-tree-p-of-tree-union (b* ((tree (tree-union x y))) (binary-tree-p tree)) :rule-classes :rewrite)