Trees.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-7.1.2).
003  * Copyright (c) 2007-2023 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     /**
036      * Builds the parents of node up to and including the root node, where the
037      * original node is the last element in the returned array. The length of
038      * the returned array gives the node's depth in the tree.
039      *
040      @param node the node to get the path for
041      @param depth  an int giving the number of steps already taken towards
042      *        the root (on recursive calls), used to size the returned array
043      @return an array of nodes giving the path from the root to the specified
044      *         node
045      */
046     static <V, T extends Tree<V, T>> MSeq<T> pathElementsFromRoot(
047         final T node,
048         final int depth
049     ) {
050         final MSeq<T> path;
051         if (node == null) {
052             path = MSeq.ofLength(depth);
053         else {
054             path = pathElementsFromRoot(
055                 node.parent().orElse(null),
056                 depth + 1
057             );
058             path.set(path.length() - depth - 1, node);
059         }
060 
061         return path;
062     }
063 
064     static <V, T extends Tree<V, T>> int[] pathFromRoot(
065         final T node,
066         final int depth
067     ) {
068         final int[] path;
069         if (node == null) {
070             path = new int[depth - 1];
071         else {
072             final T parent = node.parent().orElse(null);
073             path = pathFromRoot(parent, depth + 1);
074 
075             if (parent != null) {
076                 final int index = node.parent()
077                     .map(p -> p.indexOf(node))
078                     .orElseThrow(AssertionError::new);
079 
080                 path[path.length - depth - 1= index;
081             }
082         }
083 
084         return path;
085     }
086 
087     /**
088      * Checks if the two given trees has the same structure with the same values.
089      *
090      @param a the first tree
091      @param b the second tree
092      @return {@code true} if the two given trees are structurally equals,
093      *         {@code false} otherwise
094      */
095     static boolean equals(final Tree<?, ?> a, final Tree<?, ?> b) {
096         boolean equals = a == b;
097         if (!equals && a != null && b != null) {
098             equals = a.childCount() == b.childCount();
099             if (equals) {
100                 equals = Objects.equals(a.value(), b.value());
101                 if (equals && a.childCount() 0) {
102                     equals = equals(a.childIterator(), b.childIterator());
103                 }
104             }
105         }
106 
107         return equals;
108     }
109 
110     private static boolean equals(
111         final Iterator<? extends Tree<?, ?>> a,
112         final Iterator<? extends Tree<?, ?>> b
113     ) {
114         boolean equals = true;
115         while (a.hasNext() && equals) {
116             equals = equals(a.next(), b.next());
117         }
118 
119         return equals;
120     }
121 
122     static int countChildren(final Tree<?, ?> tree) {
123         int cnt = tree.childCount();
124         for (int i = 0; i < tree.childCount(); ++i) {
125             cnt += countChildren(tree.childAt(i));
126         }
127         return cnt;
128     }
129 
130 }