private static <V> boolean matches(
		final Tree<V, ?> node,
		final Tree<Decl<V>, ?> pattern,
		final Map<Var<V>, Tree<V, ?>> vars
	) {
		final Decl<V> decl = pattern.value();

		if (decl instanceof Var<V> var) {
			final Tree<? extends V, ?> tree = vars.get(decl);
			if (tree == null) {
				vars.put(var, node);
				return true;
			}

			return tree.equals(node);
		} else {
			final Val<V> p = (Val<V>)decl;
			final V v = node.value();

			if (Objects.equals(v, p.value())) {
				if (node.childCount() == pattern.childCount()) {
					for (int i = 0; i < node.childCount(); ++i) {
						final Tree<V, ?> cn = node.childAt(i);
						final Tree<Decl<V>, ?> cp = pattern.childAt(i);

						if (!matches(cn, cp, vars)) {
							return false;
						}
					}
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
	}
Tests whether the given input {@code tree} matches {@code this} pattern.

@param tree the tree to be matched
@return {@code true} if the {@code tree} matches {@code this} pattern,
        {@code false} otherwise
@throws NullPointerException if one of the arguments is {@code null}
jenetics.ext/src/main/java/io/jenetics/ext/rewriting/TreePattern.java