qilin.util.graph.TreeImpl Maven / Gradle / Ivy
/* Qilin - a Java Pointer Analysis Framework
* Copyright (C) 2021-2030 Qilin developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3.0 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
*/
package qilin.util.graph;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class TreeImpl implements Tree {
Map> data2Node;
TreeNode root;
public TreeImpl() {
this.data2Node = new HashMap<>();
}
@Override
public N getRoot() {
if (root == null) {
for (TreeNode n : data2Node.values()) {
if (n.isRoot()) {
root = n;
break;
}
}
}
return root != null ? root.getElem() : null;
}
@Override
public Collection getLeaves() {
return getAllNodes().stream().filter(this::isALeaf).collect(Collectors.toSet());
}
@Override
public boolean isALeaf(N o) {
return data2Node.containsKey(o) && data2Node.get(o).isLeaf();
}
@Override
public Collection getAllNodes() {
return data2Node.keySet();
}
public Collection> getAllTreeNodes() {
return data2Node.values();
}
@Override
public int size() {
return data2Node.size();
}
@Override
public N parentOf(N o) {
return data2Node.containsKey(o) ? data2Node.get(o).getParent().getElem() : null;
}
@Override
public Collection childrenOf(N o) {
if (data2Node.containsKey(o)) {
TreeNode node = data2Node.get(o);
return node.getChildren().stream().map(TreeNode::getElem).collect(Collectors.toSet());
}
return Collections.emptySet();
}
public boolean addTreeEdge(N parent, N child) {
TreeNode p = data2Node.computeIfAbsent(parent, k -> new TreeNode<>(parent));
TreeNode c = data2Node.computeIfAbsent(child, k -> new TreeNode<>(child));
boolean f = p.addChild(c);
c.setParent(p);
return f;
}
}