/** * Get a list of all children of this node. * It is helpful if one wants to iterate over all children and delete some children meanwhile. * * @return a list of all children of this node (may be empty if the node is a leaf) */ public ArrayList<TreeNode> getChildren() { ArrayList<TreeNode> children = new ArrayList<TreeNode>(); for (TreeNode child : this.children()) { children.add(child); } return children; }
public PostorderIterator(TreeNode node) { root = node; children = node.children().iterator(); subtree = new EmptyIterable<TreeNode>(); }
/** * @return siblings all siblings of this node */ public List<TreeNode> getSiblings() { TreeNode p = getParent(); if (p == null) return null; List<TreeNode> siblings = new ArrayList<TreeNode>(); for (TreeNode child : p.children()) { if (child != this) siblings.add(child); } return siblings; }
private static Map<Set<String>, TreeNode> getTreeAsLeafMap(final Tree tree, final boolean setEdgeweightsToZero) { Map<Set<String>, TreeNode> childrenSets = new HashMap<Set<String>, TreeNode>(tree.vertexCount()); for (TreeNode child : tree.getRoot().children()) { Set<String> childTaxaSet = new HashSet<String>(); addLeafesFromChildren(childrenSets, child, childTaxaSet, setEdgeweightsToZero); } return childrenSets; }
/** * Compute a list of ALL children of a {@link TreeNode}. * ALL means all nodes, that are adjacent to this node. That are all direct children * and the parent (if there is one). * <p> * Runtime: O(d) */ public ArrayList<TreeNode> getAllChildren() { ArrayList<TreeNode> children = new ArrayList<TreeNode>(); //add all children for (TreeNode child : this.children()) { children.add(child); } /**Alternative: children.addAll(node.getChildren()); => but needs maybe O(d^2)*/ //add the parent if (this.getParent() != null) { children.add(this.getParent()); } return children; }
/** * Traverses the tree rooted at this node and counts leaves. * * @return leaves under this node */ public int leafCount() { if (isLeaf()) { return 1; } int i = 0; for (TreeNode n : children()) { i += n.leafCount(); } return i; }
public static void addLeafesFromChildren(final Map<Set<String>, TreeNode> childrenSets, final TreeNode current, final Set<String> parentTaxaSet, final boolean setEdgeweightsToZero) { if (setEdgeweightsToZero) current.getEdgeToParent().setWeight(0d); if (current.isLeaf()) { parentTaxaSet.add(current.getLabel()); } else { Set<String> currentTaxaSet = new HashSet<String>(); for (TreeNode child : current.children()) { addLeafesFromChildren(childrenSets, child, currentTaxaSet, setEdgeweightsToZero); } parentTaxaSet.addAll(currentTaxaSet); childrenSets.put(currentTaxaSet, current); } }
protected Range sortNode(TreeNode node) { if (node.isLeaf()) { return new Range(node, node); } SortedMap<Range, TreeNode> ranges = new TreeMap<Range, TreeNode>(new RangeComparator()); for (TreeNode child : node.children()) { Range r = sortNode(child); ranges.put(r, child); } Range result = new Range(ranges.firstKey().min, ranges.lastKey().max); ArrayList<TreeNode> order = new ArrayList<TreeNode>(); for (Range r : ranges.keySet()) { order.add(ranges.get(r)); } node.setChildOrder(order); return result; }
s.append("("); boolean added = false; for (TreeNode n : node.children()) { appendNode(n, s, writeWeights); s.append(',');
for (TreeNode child : treeNode.children()) { List<String> labels = labels(child); if (labels.contains(leftFix)) {
/** * Little helper that recursivly collects labeled nodes under the given node * * @param n * @param leaveList * @return */ private List<String> getLeaves(TreeNode n, List<String> leaveList) { if (n.isLeaf()) { leaveList.add(n.getLabel()); return leaveList; } for (TreeNode c : n.children()) { getLeaves(c, leaveList); } if (n.getLabel() != null) { leaveList.add(n.getLabel()); } return leaveList; }
for (TreeNode child : treeNode.children()) { List<String> labels = labels(child); if (labels.contains(leftFix)) {
private static void checkForChilds(TreeNode topologyNode, TreeNode treeToMergeNode) { for (TreeNode topologyChild : topologyNode.children()) { if (topologyChild.isLeaf()) { double treeToMergeEdgeWeight = 0; for (TreeNode treeToMergeChild : treeToMergeNode.children()) { if (treeToMergeChild.isLeaf() && treeToMergeChild.getLabel().equalsIgnoreCase(topologyChild.getLabel())) { //found the correct child treeToMergeEdgeWeight = treeToMergeChild.getEdgeToParent().getWeight(); break; } } Edge topologyChildEdgeToParent = topologyChild.getEdgeToParent(); topologyChildEdgeToParent.setWeight(topologyChildEdgeToParent.getWeight() + treeToMergeEdgeWeight); } } }
/** * Computes the number of the given child for this node. * This number is 0 {@literal <=} childNum {@literal <} 'number of children', if the child is a real child of this node. * It is childNum = 'number of children', if the given child is parent of this node. * * @param child: the child, for that its number has to be computed * @return the child number for the given node */ public int getChildNumber(TreeNode child) { if (this.getParent().equalsNode(child)) { return childCount(); } else if (child.isChildOf(this)) { int num = 0; for (TreeNode nodesChild : this.children()) { if (!nodesChild.equalsNode(child)) { num++; } else { break; } } return num; } else { throw new RuntimeException("Given child is no child of this node!"); } }
/** * The second run, a preorder traversal of the tress to build the * consensus tree. * * @param node the actual node * @param parent the parents bipartition */ protected void preOrder(TreeNode node, Bipartition parent) { Bipartition part = (Bipartition) nodes2partitions.get(node); if (((CountTupel) partitions.get(part)).count >= majorityThreshold) { if (part.getParentPartition() == null) { part.setParentPartition(parent == null ? null : parent); } else { if (part.getParentPartition().getCardinality() > parent.getCardinality()) part.setParentPartition(parent); } parent = part; } // traverse the children for (TreeNode n : node.children()) { preOrder(n, parent); } }
for (TreeNode child : n.children()) { BCNScore score = sourceScrores.get(child); double child_score = score.getScore();
/** * Add subtree to existing tree, recursive iteration over the subtree to add * * @param tree the target tree * @param parent the parent node * @param newNode the current node to be added to the parent * @return node added node */ private TreeNode addSubtree(Tree tree, TreeNode parent, TreeNode newNode) { TreeNode nn = newNode.cloneNode(); nn.setIndex(-1); tree.addVertex(nn); tree.addEdge(parent, nn); for (TreeNode child : newNode.children()) { addSubtree(tree, nn, child); } return nn; }
@SuppressWarnings("unchecked") private void hangIn(TreeNode newParent, TreeNode oldParent, Tree tree) { for (TreeNode n : oldParent.children()) { TreeNode newNode = n.cloneNode(); newNode.setIndex(-1); tree.addVertex(newNode); Edge edge = tree.addEdge(newParent, newNode); edge.setWeight(n.getDistanceToParent()); hangIn(newNode, n, tree); } }
private void hangIn(TreeNode n, HashMap<String, MultiNode> labeledNodes, HashMap<TreeNode, MultiNode> all, EdgeType type) { MultiNode m = null; if (n.getLabel() != null) { m = labeledNodes.get(n.getLabel()); } if (m == null) { m = new MultiNode(); all.put(n, m); if (n.getLabel() != null) { m.setLabel(n.getLabel()); labeledNodes.put(n.getLabel(), m); } addVertex(m); } if (n.getEdgeToParent() != null) { MultiNode p = all.get(n.getParent()); addEdge(p, m, type).setWeight(n.getEdgeToParent().getWeight()); } for (TreeNode c : n.children()) { hangIn(c, labeledNodes, all, type); } }
for (TreeNode child : n.children()) { if (!pruneTraverse(t, child, newNode, labels)) { missed = true;