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

org.macrocloud.kernel.toolkit.node.ForestNodeMerger Maven / Gradle / Ivy

There is a newer version: 1.1.0-RELEASE
Show newest version
package org.macrocloud.kernel.toolkit.node;

import java.util.List;


/**
 * 森林节点归并类.
 *
 * @author macro
 */
public class ForestNodeMerger {

	/**
	 * 将节点数组归并为一个森林(多棵树)(填充节点的children域)
	 * 时间复杂度为O(n^2).
	 *
	 * @param  the generic type
	 * @param items 节点域
	 * @return 多棵树的根节点集合
	 */
	public static > List merge(List items) {
		ForestNodeManager forestNodeManager = new ForestNodeManager<>(items);
		items.forEach(forestNode -> {
			if (forestNode.getParentId() != 0) {
				INode node = forestNodeManager.getTreeNodeAt(forestNode.getParentId());
				if (node != null) {
					node.getChildren().add(forestNode);
				} else {
					forestNodeManager.addParentId(forestNode.getId());
				}
			}
		});
		return forestNodeManager.getRoot();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy