private static <V> SortedSet<Var<V>>
	extractVars(final TreeNode<Decl<V>> pattern) {
		final SortedSet<Var<V>> variables = new TreeSet<>();
		for (Tree<Decl<V>, ?> n : pattern) {
			if (n.value() instanceof Var<V> var) {
				if (!n.isLeaf()) {
					throw new IllegalArgumentException(format(
						"Variable node '%s' is not a leaf: %s",
						n.value(), n.toParenthesesString()
					));
				}

				variables.add(var);
			}
		}

		return Collections.unmodifiableSortedSet(variables);
	}
Create a new tree-pattern object from the given pattern tree.

@param pattern the pattern-tree
@throws NullPointerException if the given {@code pattern} is {@code null}
@throws IllegalArgumentException if {@link Var} nodes are not leaf nodes;
        {@link Tree#isLeaf()} is {@code false}
jenetics.ext/src/main/java/io/jenetics/ext/rewriting/TreePattern.java