func (n *Treap[T]) Intersection(other *Treap[T], c CompareFn[T]) *Treap[T] {
	if n == nil || other == nil {
		return nil
	}

	if n.Priority < other.Priority {
		n, other = other, n
	}

	left, found, right := other.Split(n.Value, c)
	left = n.Left.Intersection(left, c)
	right = n.Right.Intersection(right, c)

	if found == nil {
		// TODO: use a destructive join as both left/right are copies
		return left.join(right)
	}

	return &Treap[T]{n.Value, n.Priority, left, right}
}
// Intersection returns a new treap with all the common values in the
// two treaps.
//
// see https://www.cs.cmu.edu/~scandal/papers/treaps-spaa98.pdf
// "Fast Set Operations Using Treaps"
//   by Guy E Blelloch and Margaret Reid-Miller.
//
// The algorithm is a very slight variation on that.
Intersection returns a new treap with all the common values in the
two treaps.



The algorithm is a very slight variation on that.
// Intersection returns a new treap with all the common values in the // two treaps . // // see https : // www . cs . cmu . edu / ~scandal / papers / treaps - spaa98 . pdf // " Fast Set Operations Using Treaps " // by Guy E Blelloch and Margaret Reid - Miller . // // The algorithm is a very slight variation on that .
func (n *Treap[T]) Intersection(other *Treap[T], c CompareFn[T]) *Treap[T] {
	if n == nil || other == nil {
		return nil
	}
	if n.Priority < other.Priority {
		n, other = other, n
	}
	left, found, right := other.Split(n.Value, c)
	left = n.Left.Intersection(left, c)
	right = n.Right.Intersection(right, c)
	if found == nil {
		return left.join(right)
	}
	return &Treap[T]{n.Value, n.Priority, left, right}
}
func ( n * Treap [ T ] ) Intersection ( other * Treap [ T ] , c CompareFn [ T ] ) * Treap [ T ] { if n == nil || other == nil { return nil } if n . Priority < other . Priority { n , other = other , n } left , found , right := other . Split ( n . Value , c ) left = n . Left . Intersection ( left , c ) right = n . Right . Intersection ( right , c ) if found == nil { return left . join ( right ) } return & Treap [ T ] { n . Value , n . Priority , left , right } }
Intersection returns a new treap with all the common values in the
two treaps.
Intersection returns a new treap with all the common values in the two treaps .
// TODO: use a destructive join as both left/right are copies