All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vavr.collection.Tree Maven / Gradle / Ivy

There is a newer version: 1.0.0-alpha-4
Show newest version
/* ____  ______________  ________________________  __________
 * \   \/   /      \   \/   /   __/   /      \   \/   /      \
 *  \______/___/\___\______/___/_____/___/\___\______/___/\___\
 *
 * Copyright 2019 Vavr, http://vavr.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.vavr.collection;

import io.vavr.*;
import io.vavr.control.Option;

import java.io.*;
import java.lang.Iterable;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;

import static io.vavr.collection.Tree.Order.PRE_ORDER;

/**
 * A general Tree interface.
 *
 * @param  component type of this Tree
 */
public abstract class Tree implements Traversable, Serializable {

    private static final long serialVersionUID = 1L;

    // sealed
    private Tree() {
    }

    /**
     * Returns a {@link java.util.stream.Collector} which may be used in conjunction with
     * {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link Tree}.
     *
     * @param  Component type of the Tree.
     * @return A io.vavr.collection.Tree Collector.
     */
    public static  Collector, Tree> collector() {
        final Supplier> supplier = ArrayList::new;
        final BiConsumer, T> accumulator = ArrayList::add;
        final BinaryOperator> combiner = (left, right) -> {
            left.addAll(right);
            return left;
        };
        final Function, Tree> finisher = Tree::ofAll;
        return Collector.of(supplier, accumulator, combiner, finisher);
    }

    /**
     * Returns the singleton empty tree.
     *
     * @param  Type of tree values.
     * @return The empty tree.
     */
    public static  Empty empty() {
        return Empty.instance();
    }

    /**
     * Narrows a widened {@code Tree} to {@code Tree}
     * by performing a type-safe cast. This is eligible because immutable/read-only
     * collections are covariant.
     *
     * @param tree An {@code Tree}.
     * @param   Component type of the {@code Tree}.
     * @return the given {@code tree} instance as narrowed type {@code Tree}.
     */
    @SuppressWarnings("unchecked")
    public static  Tree narrow(Tree tree) {
        return (Tree) tree;
    }

    /**
     * Returns a new Node containing the given value and having no children.
     *
     * @param value A value
     * @param    Value type
     * @return A new Node instance.
     */
    public static  Node of(T value) {
        return new Node<>(value, io.vavr.collection.List.empty());
    }

    /**
     * Returns a new Node containing the given value and having the given children.
     *
     * @param value    A value
     * @param children The child nodes, possibly empty
     * @param       Value type
     * @return A new Node instance.
     */
    @SuppressWarnings("varargs")
    @SafeVarargs
    public static  Node of(T value, Node... children) {
        Objects.requireNonNull(children, "children is null");
        return new Node<>(value, io.vavr.collection.List.of(children));
    }

    /**
     * Returns a new Node containing the given value and having the given children.
     *
     * @param value    A value
     * @param children The child nodes, possibly empty
     * @param       Value type
     * @return A new Node instance.
     */
    public static  Node of(T value, Iterable> children) {
        Objects.requireNonNull(children, "children is null");
        return new Node<>(value, io.vavr.collection.List.ofAll(children));
    }

    /**
     * Creates a Tree of the given elements.
     *
     * @param     Component type of the List.
     * @param values Zero or more values.
     * @return A Tree containing the given values.
     * @throws NullPointerException if {@code values} is null
     */
    @SuppressWarnings("varargs")
    @SafeVarargs
    public static  Tree of(T... values) {
        Objects.requireNonNull(values, "values is null");
        final io.vavr.collection.List list = io.vavr.collection.List.of(values);
        return list.isEmpty() ? Empty.instance() : new Node<>(list.head(), list.tail().map(Tree::of));
    }

    /**
     * Creates a Tree of the given elements.
     * 

* If the given iterable is a tree, it is returned as result. * if the iteration order of the elements is stable. * * @param Component type of the List. * @param iterable An Iterable of elements. * @return A list containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("unchecked") public static Tree ofAll(Iterable iterable) { Objects.requireNonNull(iterable, "iterable is null"); if (iterable instanceof Tree) { return (Tree) iterable; } else { final io.vavr.collection.List list = io.vavr.collection.List.ofAll(iterable); return list.isEmpty() ? Empty.instance() : new Node<>(list.head(), list.tail().map(Tree::of)); } } /** * Creates a Tree that contains the elements of the given {@link java.util.stream.Stream}. * * @param javaStream A {@link java.util.stream.Stream} * @param Component type of the Stream. * @return A Tree containing the given elements in the same order. */ public static Tree ofAll(java.util.stream.Stream javaStream) { Objects.requireNonNull(javaStream, "javaStream is null"); return ofAll(io.vavr.collection.Iterator.ofAll(javaStream.iterator())); } /** * Returns a Tree containing {@code n} values of a given Function {@code f} * over a range of integer values from 0 to {@code n - 1}. * * @param Component type of the Tree * @param n The number of elements in the Tree * @param f The Function computing element values * @return A Tree consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code f} is null */ public static Tree tabulate(int n, Function f) { Objects.requireNonNull(f, "f is null"); return io.vavr.collection.Collections.tabulate(n, f, empty(), Tree::of); } /** * Returns a Tree containing {@code n} values supplied by a given Supplier {@code s}. * * @param Component type of the Tree * @param n The number of elements in the Tree * @param s The Supplier computing element values * @return A Tree of size {@code n}, where each element contains the result supplied by {@code s}. * @throws NullPointerException if {@code s} is null */ public static Tree fill(int n, Supplier s) { Objects.requireNonNull(s, "s is null"); return io.vavr.collection.Collections.fill(n, s, empty(), Tree::of); } /** * Returns a Tree containing {@code n} times the given {@code element} * * @param Component type of the Tree * @param n The number of elements in the Tree * @param element The element * @return A Tree of size {@code n}, where each element is the given {@code element}. */ public static Tree fill(int n, T element) { return io.vavr.collection.Collections.fillObject(n, element, empty(), Tree::of); } /** * Recursively builds a non-empty {@code Tree}, starting with the given {@code seed} value and proceeding in depth-first order. *

* The children of a node are created by *

    *
  1. applying the {@code descend} function to the node value
  2. *
  3. calling this method recursively by using each derived child value as new seed (in iteration order).
  4. *
*

* Example: *

{@code
     * // = (1 (2 4 5) 3)
     * Tree.recurse(1, i ->
     *   (i == 1) ? List.of(2, 3) :
     *   (i == 2) ? List.(4, 5) :
     *   List.empty()
     * ).toLispString();
     * }
* * @param seed The start value for the Tree * @param descend A function to calculate the child values * @param Value type * @return a new, non-empty {@code Tree} instance * @throws NullPointerException if {@code descend} is null */ public static Node recurse(T seed, Function> descend) { Objects.requireNonNull(descend, "descend is null"); return Tree.of(seed, Stream.of(seed).flatMap(descend).map(children -> recurse(children, descend))); } /** * Build a {@code List} with roots of {@code Tree} from flat source. *

* {@code parentMapper} must return {@code null} for root element. * *

{@code
     *  // = [(1, null, "I"), (2, 1, "II"), (3, 1, "III"), (4, 2, "IV"), (5, 2, "V")]
     *  List items = ...; // MenuItem(id, parentId, label)
     *
     *  //      I
     *  //     / \
     *  //   II  III
     *  //   /\
     *  //  IV V
     *  Tree menu = Tree.build(items, MenuItem::getId, MenuItem::getParentId);
     * }
* * @param source Flat source * @param idMapper A mapper from source item to unique identifier of that item * @param parentMapper A mapper from source item to unique identifier of parent item. Need return null for root items * @param Value type * @param Id type * @return a new, maybe empty {@code List} instance with non-empty {@code Tree} instances * @throws NullPointerException if {@code source}, {@code idMapper} or {@code parentMapper} is null */ public static List> build(Iterable source, Function idMapper, Function parentMapper) { Objects.requireNonNull(source, "source is null"); Objects.requireNonNull(source, "idMapper is null"); Objects.requireNonNull(source, "parentMapper is null"); final List list = List.ofAll(source); final Map> byParent = list.groupBy(parentMapper); final Function> descend = idMapper .andThen(byParent::get) .andThen(o -> o.getOrElse(List::empty)); final List roots = byParent.get(null).getOrElse(List::empty); return roots.map(v -> recurse(v, descend)); } @Override public final Tree collect(PartialFunction partialFunction) { return ofAll(iterator().collect(partialFunction)); } /** * Gets the value of this tree. * * @return The value of this tree. * @throws java.lang.UnsupportedOperationException if this tree is empty */ public abstract T getValue(); /** * Returns the children of this tree. * * @return the tree's children */ public abstract io.vavr.collection.List> getChildren(); /** * Checks if this Tree is a leaf. A tree is a leaf if it is a Node with no children. * Because the empty tree is no Node, it is not a leaf by definition. * * @return true if this tree is a leaf, false otherwise. */ public abstract boolean isLeaf(); /** * Checks if this Tree is a branch. A Tree is a branch if it is a Node which has children. * Because the empty tree is not a Node, it is not a branch by definition. * * @return true if this tree is a branch, false otherwise. */ public final boolean isBranch() { return !(isEmpty() || isLeaf()); } /** * A {@code Tree} is computed synchronously. * * @return false */ @Override public final boolean isAsync() { return false; } @Override public final boolean isDistinct() { return false; } /** * A {@code Tree} is computed eagerly. * * @return false */ @Override public final boolean isLazy() { return false; } @Override public final boolean isSequential() { return true; } /** * Traverses this tree values in a specific {@link Order}. * * @param order A traversal order * @return A new Iterator */ public final io.vavr.collection.Iterator iterator(Order order) { return values(order).iterator(); } /** * Creates a Lisp-like representation of this {@code Tree}. * * @return This {@code Tree} as Lisp-string, i.e. represented as list of lists. */ public abstract String toLispString(); /** * Transforms this {@code Tree}. * * @param f A transformation * @param Type of transformation result * @return An instance of type {@code U} * @throws NullPointerException if {@code f} is null */ public final U transform(Function, ? extends U> f) { Objects.requireNonNull(f, "f is null"); return f.apply(this); } /** * Traverses this tree in {@link Order#PRE_ORDER}. * * @return A sequence of nodes. */ public final Seq> traverse() { return traverse(PRE_ORDER); } /** * Traverses this tree in a specific order. * * @param order the tree traversal order * @return A sequence of nodes. * @throws java.lang.NullPointerException if order is null */ public final Seq> traverse(Order order) { Objects.requireNonNull(order, "order is null"); if (isEmpty()) { return Stream.empty(); } else { final Node node = (Node) this; switch (order) { case PRE_ORDER: return Tree.traversePreOrder(node); case IN_ORDER: return Tree.traverseInOrder(node); case POST_ORDER: return Tree.traversePostOrder(node); case LEVEL_ORDER: return Tree.traverseLevelOrder(node); default: throw new IllegalStateException("Unknown order: " + order.name()); } } } /** * Traverses this tree values in {@link Order#PRE_ORDER}. * Syntactic sugar for {@code traverse().map(Node::getValue)}. * * @return A sequence of the tree values. */ public final Seq values() { return traverse(PRE_ORDER).map(Node::getValue); } /** * Traverses this tree values in a specific order. * Syntactic sugar for {@code traverse(order).map(Node::getValue)}. * * @param order the tree traversal order * @return A sequence of the tree values. * @throws java.lang.NullPointerException if order is null */ public final Seq values(Order order) { return traverse(order).map(Node::getValue); } /** * Counts the number of branches of this tree. The empty tree and a leaf have no branches. * * @return The number of branches of this tree. */ public final int branchCount() { if (isEmpty() || isLeaf()) { return 0; } else { return getChildren().foldLeft(1, (count, child) -> count + child.branchCount()); } } /** * Counts the number of leaves of this tree. The empty tree has no leaves. * * @return The number of leaves of this tree. */ public final int leafCount() { if (isEmpty()) { return 0; } else if (isLeaf()) { return 1; } else { return getChildren().foldLeft(0, (count, child) -> count + child.leafCount()); } } /** * Counts the number of nodes (i.e. branches and leaves) of this tree. The empty tree has no nodes. * * @return The number of nodes of this tree. */ public final int nodeCount() { return length(); } // -- Methods inherited from Traversable @Override public final Seq distinct() { return values().distinct(); } @Override public final Seq distinctBy(Comparator comparator) { Objects.requireNonNull(comparator, "comparator is null"); if (isEmpty()) { return Stream.empty(); } else { return values().distinctBy(comparator); } } @Override public final Seq distinctBy(Function keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); if (isEmpty()) { return Stream.empty(); } else { return values().distinctBy(keyExtractor); } } @Override public final Seq drop(int n) { if (n >= length()) { return Stream.empty(); } else { return values().drop(n); } } @Override public final Seq dropRight(int n) { if (n >= length()) { return Stream.empty(); } else { return values().dropRight(n); } } @Override public final Seq dropUntil(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return dropWhile(predicate.negate()); } @Override public final Seq dropWhile(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Stream.empty(); } else { return values().dropWhile(predicate); } } @Override public final Seq filter(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Stream.empty(); } else { return values().filter(predicate); } } @Override public final Seq filterNot(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Stream.empty(); } else { return values().filterNot(predicate); } } @Deprecated @Override public final Seq reject(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Stream.empty(); } else { return values().reject(predicate); } } @Override public final Tree flatMap(Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return isEmpty() ? Empty.instance() : Tree.flatMap((Node) this, mapper); } @Override public final U foldRight(U zero, BiFunction f) { Objects.requireNonNull(f, "f is null"); if (isEmpty()) { return zero; } else { return iterator().foldRight(zero, f); } } @SuppressWarnings("unchecked") @Override public final Map> groupBy(Function classifier) { return io.vavr.collection.Collections.groupBy(values(), classifier, Stream::ofAll); } @Override public final io.vavr.collection.Iterator> grouped(int size) { return sliding(size, size); } @Override public final boolean hasDefiniteSize() { return true; } @Override public final T head() { if (isEmpty()) { throw new NoSuchElementException("head of empty tree"); } else { return iterator().next(); } } @Override public final Seq init() { if (isEmpty()) { throw new UnsupportedOperationException("init of empty tree"); } else { return values().init(); } } @Override public final Option> initOption() { return isEmpty() ? Option.none() : Option.some(init()); } @Override public final boolean isTraversableAgain() { return true; } @Override public final io.vavr.collection.Iterator iterator() { return values().iterator(); } @Override public final Tree map(Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return isEmpty() ? Empty.instance() : Tree.map((Node) this, mapper); } @Override public final Tree orElse(Iterable other) { return isEmpty() ? ofAll(other) : this; } @Override public final Tree orElse(Supplier> supplier) { return isEmpty() ? ofAll(supplier.get()) : this; } @SuppressWarnings("unchecked") @Override public final Tuple2, Seq> partition(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Tuple.of(Stream.empty(), Stream.empty()); } else { return (Tuple2, Seq>) values().partition(predicate); } } @Override public final Tree peek(Consumer action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; } @Override public final Tree replace(T currentElement, T newElement) { if (isEmpty()) { return Empty.instance(); } else { return Tree.replace((Node) this, currentElement, newElement); } } @Override public final Tree replaceAll(T currentElement, T newElement) { return map(t -> Objects.equals(t, currentElement) ? newElement : t); } @Override public final Seq retainAll(Iterable elements) { Objects.requireNonNull(elements, "elements is null"); return values().retainAll(elements); } @Override public final Seq scan(T zero, BiFunction operation) { return scanLeft(zero, operation); } @Override public final Seq scanLeft(U zero, BiFunction operation) { return io.vavr.collection.Collections.scanLeft(this, zero, operation, io.vavr.collection.Iterator::toStream); } @Override public final Seq scanRight(U zero, BiFunction operation) { return io.vavr.collection.Collections.scanRight(this, zero, operation, io.vavr.collection.Iterator::toStream); } @Override public final io.vavr.collection.Iterator> slideBy(Function classifier) { return iterator().slideBy(classifier); } @Override public final io.vavr.collection.Iterator> sliding(int size) { return sliding(size, 1); } @Override public final io.vavr.collection.Iterator> sliding(int size, int step) { return iterator().sliding(size, step); } @SuppressWarnings("unchecked") @Override public final Tuple2, Seq> span(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Tuple.of(Stream.empty(), Stream.empty()); } else { return (Tuple2, Seq>) values().span(predicate); } } @Override public final String stringPrefix() { return "Tree"; } @Override public final Seq tail() { if (isEmpty()) { throw new UnsupportedOperationException("tail of empty tree"); } else { return values().tail(); } } @Override public final Option> tailOption() { return isEmpty() ? Option.none() : Option.some(tail()); } @Override public final Seq take(int n) { if (isEmpty()) { return Stream.empty(); } else { return values().take(n); } } @Override public final Seq takeRight(int n) { if (isEmpty()) { return Stream.empty(); } else { return values().takeRight(n); } } @Override public final Seq takeUntil(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return values().takeUntil(predicate); } @Override public final Seq takeWhile(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return values().takeWhile(predicate); } @SuppressWarnings("unchecked") @Override public final Tuple2, Tree> unzip( Function> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (isEmpty()) { return Tuple.of(Empty.instance(), Empty.instance()); } else { return (Tuple2, Tree>) (Object) Tree.unzip((Node) this, unzipper); } } @SuppressWarnings("unchecked") @Override public final Tuple3, Tree, Tree> unzip3( Function> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (isEmpty()) { return Tuple.of(Empty.instance(), Empty.instance(), Empty.instance()); } else { return (Tuple3, Tree, Tree>) (Object) Tree.unzip3((Node) this, unzipper); } } @Override public final Tree> zip(Iterable that) { return zipWith(that, Tuple::of); } @Override public final Tree zipWith(Iterable that, BiFunction mapper) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(mapper, "mapper is null"); if (isEmpty()) { return Empty.instance(); } else { return Tree.zip((Node) this, that.iterator(), mapper); } } @Override public final Tree> zipAll(Iterable that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); if (isEmpty()) { return io.vavr.collection.Iterator.ofAll(that).map(elem -> Tuple.of(thisElem, elem)).toTree(); } else { final java.util.Iterator thatIter = that.iterator(); final Tree> tree = Tree.zipAll((Node) this, thatIter, thatElem); if (thatIter.hasNext()) { final Iterable>> remainder = io.vavr.collection.Iterator .ofAll(thatIter) .map(elem -> of(Tuple.of(thisElem, elem))); return new Node<>(tree.getValue(), tree.getChildren().appendAll(remainder)); } else { return tree; } } } @Override public final Tree> zipWithIndex() { return zipWithIndex(Tuple::of); } @Override public final Tree zipWithIndex(BiFunction mapper) { Objects.requireNonNull(mapper, "mapper is null"); return zipWith(io.vavr.collection.Iterator.from(0), mapper); } /** * Creates a neat 2-dimensional drawing of a tree. Unicode characters are used to draw node junctions. * * @return A nice string representation of the tree. */ public abstract String draw(); /** * Represents a tree node. * * @param value type * @deprecated will be removed from the public API */ @Deprecated public static final class Node extends Tree implements Serializable { private static final long serialVersionUID = 1L; private final T value; private final io.vavr.collection.List> children; private final int size; /** * Constructs a rose tree branch. * * @param value A value. * @param children A non-empty list of children. * @throws NullPointerException if children is null * @throws IllegalArgumentException if children is empty */ public Node(T value, io.vavr.collection.List> children) { Objects.requireNonNull(children, "children is null"); this.value = value; this.children = children; this.size = children.foldLeft(1, (acc, child) -> acc + child.size); } @Override public io.vavr.collection.List> getChildren() { return children; } @Override public T getValue() { return value; } @Override public boolean isEmpty() { return false; } @Override public int length() { return size; } @Override public boolean isLeaf() { return size == 1; } @Override public T last() { return children.isEmpty() ? value : children.last().last(); } @Override public boolean equals(Object o) { if (o == this) { return true; } else if (o instanceof Node) { final Node that = (Node) o; return Objects.equals(this.getValue(), that.getValue()) && Objects.equals(this.getChildren(), that.getChildren()); } else { return false; } } @Override public int hashCode() { return Tuple.hash(value, children); } @Override public String toString() { return mkString(stringPrefix() + "(", ", ", ")"); } @Override public String toLispString() { return toLispString(this); } @Override public String draw() { final StringBuilder builder = new StringBuilder(); drawAux("", builder); return builder.toString(); } private void drawAux(String indent, StringBuilder builder) { builder.append(value); for (io.vavr.collection.List> it = children; !it.isEmpty(); it = it.tail()) { final boolean isLast = it.tail().isEmpty(); builder.append('\n') .append(indent) .append(isLast ? "└──" : "├──"); it.head().drawAux(indent + (isLast ? " " : "│ "), builder); } } private static String toLispString(Tree tree) { final String value = String.valueOf(tree.getValue()); if (tree.isLeaf()) { return value; } else { final String children = tree.getChildren().map(child -> toLispString(child)).mkString(" "); return "(" + value + " " + children + ")"; } } // -- Serializable implementation /** * {@code writeReplace} method for the serialization proxy pattern. *

* The presence of this method causes the serialization system to emit a SerializationProxy instance instead of * an instance of the enclosing class. * * @return A SerializationProxy for this enclosing class. */ @GwtIncompatible("The Java serialization protocol is explicitly not supported") private Object writeReplace() { return new SerializationProxy<>(this); } /** * {@code readObject} method for the serialization proxy pattern. *

* Guarantees that the serialization system will never generate a serialized instance of the enclosing class. * * @param stream An object serialization stream. * @throws java.io.InvalidObjectException This method will throw with the message "Proxy required". */ @GwtIncompatible("The Java serialization protocol is explicitly not supported") private void readObject(ObjectInputStream stream) throws InvalidObjectException { throw new InvalidObjectException("Proxy required"); } /** * A serialization proxy which, in this context, is used to deserialize immutable nodes with final * instance fields. * * @param The component type of the underlying tree. */ // DEV NOTE: The serialization proxy pattern is not compatible with non-final, i.e. extendable, // classes. Also, it may not be compatible with circular object graphs. @GwtIncompatible("The Java serialization protocol is explicitly not supported") private static final class SerializationProxy implements Serializable { private static final long serialVersionUID = 1L; // the instance to be serialized/deserialized private transient Node node; /** * Constructor for the case of serialization, called by {@link Node#writeReplace()}. *

* The constructor of a SerializationProxy takes an argument that concisely represents the logical state of * an instance of the enclosing class. * * @param node a Branch */ SerializationProxy(Node node) { this.node = node; } /** * Write an object to a serialization stream. * * @param s An object serialization stream. * @throws java.io.IOException If an error occurs writing to the stream. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeObject(node.value); s.writeObject(node.children); } /** * Read an object from a deserialization stream. * * @param s An object deserialization stream. * @throws ClassNotFoundException If the object's class read from the stream cannot be found. * @throws IOException If an error occurs reading from the stream. */ @SuppressWarnings("unchecked") private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); final T value = (T) s.readObject(); final io.vavr.collection.List> children = (io.vavr.collection.List>) s.readObject(); node = new Node<>(value, children); } /** * {@code readResolve} method for the serialization proxy pattern. *

* Returns a logically equivalent instance of the enclosing class. The presence of this method causes the * serialization system to translate the serialization proxy back into an instance of the enclosing class * upon deserialization. * * @return A deserialized instance of the enclosing class. */ private Object readResolve() { return node; } } } /** * The empty tree. Use Tree.empty() to create an instance. * * @param type of the tree's values * @deprecated will be removed from the public API */ @Deprecated public static final class Empty extends Tree implements Serializable { private static final long serialVersionUID = 1L; private static final Empty INSTANCE = new Empty<>(); // hidden private Empty() { } @SuppressWarnings("unchecked") public static Empty instance() { return (Empty) INSTANCE; } @Override public io.vavr.collection.List> getChildren() { return List.empty(); } @Override public T getValue() { throw new UnsupportedOperationException("getValue of empty Tree"); } @Override public boolean isEmpty() { return true; } @Override public int length() { return 0; } @Override public boolean isLeaf() { return false; } @Override public T last() { throw new NoSuchElementException("last of empty tree"); } @Override public boolean equals(Object o) { return o == this; } @Override public int hashCode() { return 1; } @Override public String toString() { return stringPrefix() + "()"; } @Override public String toLispString() { return "()"; } @Override public String draw() { return "▣"; } // -- Serializable implementation /** * Instance control for object serialization. * * @return The singleton instance of Nil. * @see java.io.Serializable */ private Object readResolve() { return INSTANCE; } } /** * Tree traversal order. *

* Example tree: *

     * 
     *         1
     *        / \
     *       /   \
     *      /     \
     *     2       3
     *    / \     /
     *   4   5   6
     *  /       / \
     * 7       8   9
     * 
     * 
*

* See also *

*/ // see http://programmers.stackexchange.com/questions/138766/in-order-traversal-of-m-way-trees public enum Order { /** * 1 2 4 7 5 3 6 8 9 (= depth-first) */ PRE_ORDER, /** * 7 4 2 5 1 8 6 9 3 */ IN_ORDER, /** * 7 4 5 2 8 9 6 3 1 */ POST_ORDER, /** * 1 2 3 4 5 6 7 8 9 (= breadth-first) */ LEVEL_ORDER } @SuppressWarnings("unchecked") private static Tree flatMap(Node node, Function> mapper) { final Tree mapped = ofAll(mapper.apply(node.getValue())); if (mapped.isEmpty()) { return empty(); } else { final io.vavr.collection.List> children = (io.vavr.collection.List>) (Object) node .getChildren() .map(child -> flatMap(child, mapper)) .filter(Tree::nonEmpty); return of(mapped.get(), children.prependAll(mapped.getChildren())); } } private static Node map(Node node, Function mapper) { final U value = mapper.apply(node.getValue()); final io.vavr.collection.List> children = node.getChildren().map(child -> map(child, mapper)); return new Node<>(value, children); } // Idea: // Traverse (depth-first) until a match is found, then stop and rebuild relevant parts of the tree. // If not found, return the same tree instance. private static Node replace(Node node, T currentElement, T newElement) { if (Objects.equals(node.getValue(), currentElement)) { return new Node<>(newElement, node.getChildren()); } else { for (Node child : node.getChildren()) { final Node newChild = replace(child, currentElement, newElement); final boolean found = newChild != child; if (found) { final io.vavr.collection.List> newChildren = node.getChildren().replace(child, newChild); return new Node<>(node.getValue(), newChildren); } } return node; } } private static Stream> traversePreOrder(Node node) { return node.getChildren().foldLeft(Stream.of(node), (acc, child) -> acc.appendAll(traversePreOrder(child))); } private static Stream> traverseInOrder(Node node) { if (node.isLeaf()) { return Stream.of(node); } else { final io.vavr.collection.List> children = node.getChildren(); return children .tail() .foldLeft(Stream.>empty(), (acc, child) -> acc.appendAll(traverseInOrder(child))) .prepend(node) .prependAll(traverseInOrder(children.head())); } } private static Stream> traversePostOrder(Node node) { return node .getChildren() .foldLeft(Stream.>empty(), (acc, child) -> acc.appendAll(traversePostOrder(child))) .append(node); } private static Stream> traverseLevelOrder(Node node) { Stream> result = Stream.empty(); final java.util.Queue> queue = new java.util.LinkedList<>(); queue.add(node); while (!queue.isEmpty()) { final Node next = queue.remove(); result = result.prepend(next); queue.addAll(next.getChildren().toJavaList()); } return result.reverse(); } private static Tuple2, Node> unzip(Node node, Function> unzipper) { final Tuple2 value = unzipper.apply(node.getValue()); final io.vavr.collection.List, Node>> children = node .getChildren() .map(child -> unzip(child, unzipper)); final Node node1 = new Node<>(value._1, children.map(t -> t._1)); final Node node2 = new Node<>(value._2, children.map(t -> t._2)); return Tuple.of(node1, node2); } private static Tuple3, Node, Node> unzip3(Node node, Function> unzipper) { final Tuple3 value = unzipper.apply(node.getValue()); final io.vavr.collection.List, Node, Node>> children = node.getChildren() .map(child -> unzip3(child, unzipper)); final Node node1 = new Node<>(value._1, children.map(t -> t._1)); final Node node2 = new Node<>(value._2, children.map(t -> t._2)); final Node node3 = new Node<>(value._3, children.map(t -> t._3)); return Tuple.of(node1, node2, node3); } @SuppressWarnings("unchecked") private static Tree zip(Node node, java.util.Iterator that, BiFunction mapper) { if (!that.hasNext()) { return Empty.instance(); } else { final R value = mapper.apply(node.getValue(), that.next()); final io.vavr.collection.List> children = (io.vavr.collection.List>) (Object) node .getChildren() .map(child -> zip(child, that, mapper)) .filter(Tree::nonEmpty); return new Node<>(value, children); } } @SuppressWarnings("unchecked") private static Tree> zipAll(Node node, java.util.Iterator that, U thatElem) { if (!that.hasNext()) { return node.map(value -> Tuple.of(value, thatElem)); } else { final Tuple2 value = Tuple.of(node.getValue(), that.next()); final io.vavr.collection.List>> children = (io.vavr.collection.List>>) (Object) node .getChildren() .map(child -> zipAll(child, that, thatElem)) .filter(Tree::nonEmpty); return new Node<>(value, children); } } }