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

io.jenetics.ext.util.TreeNodePreorderIterator Maven / Gradle / Ivy

/*
 * Java Genetic Algorithm Library (jenetics-8.1.0).
 * Copyright (c) 2007-2024 Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package io.jenetics.ext.util;

import static java.util.Objects.requireNonNull;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * Preorder iterator of the tree.
 *
 * @author Franz Wilhelmstötter
 * @version 5.2
 * @since 3.9
 */
final class TreeNodePreorderIterator>
	implements Iterator
{
	private final Deque> _deque = new ArrayDeque<>();

	/**
	 * Create a new preorder iterator of the given tree {@code root}.
	 *
	 * @param root the root node of the tree
	 * @throws NullPointerException if the given {@code root} node is
	 *        {@code null}
	 */
	TreeNodePreorderIterator(final T root) {
		requireNonNull(root);
		_deque.push(List.of(root).iterator());
	}

	@Override
	public boolean hasNext() {
		final Iterator peek = _deque.peek();
		return peek != null && peek.hasNext();
	}

	@Override
	public T next() {
		final Iterator it = _deque.peek();
		if (it == null) {
			throw new NoSuchElementException("No next element.");
		}

		final T node = it.next();
		if (!it.hasNext()) {
			_deque.pop();
		}

		final Iterator children = node.childIterator();
		if (children.hasNext()) {
			_deque.push(children);
		}

		return node;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy