default RedBlackTree<T> intersection(RedBlackTree<T> tree) { Objects.requireNonNull(tree, "tree is null"); if (isEmpty()) { return this; } else if (tree.isEmpty()) { return tree; } else { final Node<T> that = (Node<T>) tree; final Tuple2<RedBlackTree<T>, RedBlackTree<T>> split = Node.split(this, that.value); if (contains(that.value)) { return Node.join(split._1.intersection(that.left), that.value, split._2.intersection(that.right)); } else { return Node.merge(split._1.intersection(that.left), split._2.intersection(that.right)); } } }
@SuppressWarnings("unchecked") @Override public TreeSet<T> intersect(Set<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty()) { return this; } else if (elements instanceof TreeSet) { final TreeSet<T> that = (TreeSet<T>) elements; return new TreeSet<>(tree.intersection(that.tree)); } else { return retainAll(elements); } }
default RedBlackTree<T> intersection(RedBlackTree<T> tree) { Objects.requireNonNull(tree, "tree is null"); if (isEmpty()) { return this; } else if (tree.isEmpty()) { return tree; } else { final Node<T> that = (Node<T>) tree; final Tuple2<RedBlackTree<T>, RedBlackTree<T>> split = Node.split(this, that.value); if (contains(that.value)) { return Node.join(split._1.intersection(that.left), that.value, split._2.intersection(that.right)); } else { return Node.merge(split._1.intersection(that.left), split._2.intersection(that.right)); } } }
@SuppressWarnings("unchecked") @Override public TreeSet<T> intersect(Set<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty()) { return this; } else if (elements instanceof TreeSet) { final TreeSet<T> that = (TreeSet<T>) elements; return new TreeSet<>(tree.intersection(that.tree)); } else { return retainAll(elements); } }