/** * Returns true if this is not a leaf. * * @return true is not leaf */ public boolean isInnerNode() { return !isLeaf(); }
private boolean isLeaf() { return insertionPoint.isLeaf(); }
/** * Returns the leaves under this node in depths first traversal order. * * @return leaves under this node * @see #depthFirstIterator() */ public TreeNode[] getLeaves() { if (isLeaf()) return new TreeNode[]{this}; List<TreeNode> l = new ArrayList<TreeNode>(); for (TreeNode c : depthFirstIterator()) { if (c.isLeaf()) { l.add(c); } } TreeNode[] n = new TreeNode[l.size()]; l.toArray(n); return n; }
/** * Returns the number leaves of the tree. * (Leaves are all vertices with outdegree == 0). * <br> THIS DOES NOT CACHE and always recounts. * * @return number of leaves */ public int getNumTaxa() { int nTaxa = 0; for (TreeNode v : vertices()) { if (v.isLeaf()) nTaxa++; } return nTaxa; }
private void getLeaves(Tree model) { TreeNode root2 = (TreeNode) model.getRoot(); if (!sym) whichRoot += 1; leaves = new ArrayList<TreeNode>(); for (TreeNode dummy : root2.depthFirstIterator()) { if (dummy.isLeaf()) leaves.add(dummy); } }
private List<String> labels(TreeNode n) { List<String> l = new ArrayList<String>(); for (TreeNode node : n.depthFirstIterator()) { if (node.isLeaf()) { String label = node.getLabel(); if (label != null) { l.add(label); } } } return l; } }
/** * Adds a TreeNode to the Partiton. * * @param node */ public void add(TreeNode node) { if (!node.isLeaf()) return; // TODO: Throw some exception here String key = node.getLabel();// getUserObject()).getID(); if (key == null) { //throw new NullPointerException(); key = ""; } this.leavesMap.put(key, node); if (minKey == null) this.minKey = ((Comparable) key); else if (minKey.compareTo(key) > 0) { minKey = (Comparable) key; } if (maxKey == null) this.maxKey = ((Comparable) key); else if (maxKey.compareTo(key) < 0) { maxKey = (Comparable) key; } }
protected TreeNode find(String label, Tree tree) { if (label == null) return null; for (TreeNode v : tree.vertices()) { if (v.isLeaf() && label.equals(v.getLabel())) return v; } return null; }
/** * The set of labels of all leafs below the input tree node * * @param treeNode the input node * @return The Set of leaf labels */ public static Set<String> getLeafLabels(TreeNode treeNode) { Set<String> leafSet = new HashSet<String>(); if (treeNode.isLeaf()) { leafSet.add(treeNode.getLabel()); } else { for (TreeNode node : treeNode.depthFirstIterator()) { if (node.isLeaf()) leafSet.add(node.getLabel()); } } return leafSet; }
/** * method fills a vector with the nodes of the compare tree * * @param tree */ public static Vector<TreeNode> getInternalNodes(Tree tree) { TreeNode root = (TreeNode) tree.getRoot(); Vector<TreeNode> internalNodes = new Vector<TreeNode>(); for (TreeNode dummy : root.depthFirstIterator()) { if (!dummy.isLeaf()) { internalNodes.add(dummy); } } return internalNodes; }
/** * method fills a vector with the nodes of the compare tree * * @param compareTree */ private void getInternalNodes(Tree compareTree) { this.compareTree = compareTree; TreeNode root = (TreeNode) compareTree.getRoot(); internalNodes = new ArrayList<TreeNode>(); for (TreeNode dummy : root.depthFirstIterator()) { if (!dummy.isLeaf()) { internalNodes.add(dummy); } } }
/** * Collect all leaf siblings of a given leaf. If the given node is a * inner node this returns null. Only direct leaf siblings are added to the * list of siblings * * @param node the node * @return siblings direct leaf siblings of a given leaf */ protected List<String> siblings(TreeNode node) { if (!node.isLeaf()) return null; ArrayList<String> siblings = new ArrayList<String>(); if (node.getLabel() == null) throw new NullPointerException(); siblings.add(node.getLabel()); List<TreeNode> sibs = node.getSiblings(); if (sibs != null) { for (TreeNode n : sibs) { if (n.isLeaf()) { siblings.add(n.getLabel()); } } } return siblings; }
/** * 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); } }
public void addTree(Tree tree) { String name = tree.getName(); this.treeList.add(tree); for (TreeNode treeNode : tree.vertices()) { if (!addInternaleTaxa && treeNode.isLeaf()) taxa.addTaxon(treeNode.getLabel()); } String nn = taxa.translate(name); if (nn == null) { nn = "tree_" + treeList.size(); } this.trees.put(nn, tree); }
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; }
/** * Creates and returns the {@link Partition} of this node. Partitions are not * cached and recomputed for each call. * * @return partition of this node */ public Partition getPartition() { Partition partition = new Partition(); for (TreeNode n : depthFirstIterator()) { if (n.isLeaf()) { partition.add(n); } } return partition; }
/** * 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; }
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); } } }
THashSet<String> getLeafLabels(Tree tree1) { THashSet<String> taxonSet = new THashSet<>(tree1.vertexCount()); for (TreeNode taxon : tree1.getRoot().depthFirstIterator()) { if (taxon.isLeaf()) { taxonSet.add(taxon.getLabel()); } } return taxonSet; }