Trees.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.1.0).
003  * Copyright (c) 2007-2019 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.ext.util;
021 
022 import java.util.Iterator;
023 import java.util.Objects;
024 
025 import io.jenetics.util.MSeq;
026 
027 /**
028  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
029  @version 5.0
030  @since 3.9
031  */
032 final class Trees {
033     private Trees() {}
034 
035     @SuppressWarnings("unchecked")
036     static <V, T extends Tree<V, T>> T self(final Tree<?, ?> tree) {
037         return (T)tree;
038     }
039 
040     /**
041      * Builds the parents of node up to and including the root node, where the
042      * original node is the last element in the returned array. The length of
043      * the returned array gives the node's depth in the tree.
044      *
045      @param node the node to get the path for
046      @param depth  an int giving the number of steps already taken towards
047      *        the root (on recursive calls), used to size the returned array
048      @return an array of nodes giving the path from the root to the specified
049      *         node
050      */
051     static <V, T extends Tree<V, T>> MSeq<T> pathElementsFromRoot(
052         final T node,
053         final int depth
054     ) {
055         final MSeq<T> path;
056         if (node == null) {
057             path = MSeq.ofLength(depth);
058         else {
059             path = pathElementsFromRoot(
060                 node.getParent().orElse(null),
061                 depth + 1
062             );
063             path.set(path.length() - depth - 1, node);
064         }
065 
066         return path;
067     }
068 
069     static <V, T extends Tree<V, T>> int[] pathFromRoot(
070         final T node,
071         final int depth
072     ) {
073         final int[] path;
074         if (node == null) {
075             path = new int[depth - 1];
076         else {
077             final T parent = node.getParent().orElse(null);
078             path = pathFromRoot(parent, depth + 1);
079 
080             if (parent != null) {
081                 final int index = node.getParent()
082                     .map(p -> p.indexOf(node))
083                     .orElseThrow(AssertionError::new);
084 
085                 path[path.length - depth - 1= index;
086             }
087         }
088 
089         return path;
090     }
091 
092     /**
093      * Checks if the two given trees has the same structure with the same values.
094      *
095      @param a the first tree
096      @param b the second tree
097      @return {@code true} if the two given trees are structurally equals,
098      *         {@code false} otherwise
099      */
100     static boolean equals(final Tree<?, ?> a, final Tree<?, ?> b) {
101         boolean equals = a == b;
102         if (!equals && a != null && b != null) {
103             equals = a.childCount() == b.childCount();
104             if (equals) {
105                 equals = Objects.equals(a.getValue(), b.getValue());
106                 if (equals && a.childCount() 0) {
107                     equals = equals(a.childIterator(), b.childIterator());
108                 }
109             }
110         }
111 
112         return equals;
113     }
114 
115     private static boolean equals(
116         final Iterator<? extends Tree<?, ?>> a,
117         final Iterator<? extends Tree<?, ?>> b
118     ) {
119         boolean equals = true;
120         while (a.hasNext() && equals) {
121             equals = equals(a.next(), b.next());
122         }
123 
124         return equals;
125     }
126 
127     static int countChildren(final Tree<?, ?> tree) {
128         int cnt = tree.childCount();
129         for (int i = 0; i < tree.childCount(); ++i) {
130             cnt += countChildren(tree.childAt(i));
131         }
132         return cnt;
133     }
134 
135 }