/** * Removes the edge from n1 to n2 from the tree (if such edge exists) */ public void removeEdge(TreeNode n1, TreeNode n2) { Edge<TreeNode> e = n1.getEdge(n2); if (e != null) { n1.removeEdge(e); n2.removeEdge(e); } }
/** * Rotates the children of this node. * * @param clockwise */ public void rotateChildren(boolean clockwise) { if (edges != null && edges.size() >= 2) { if (clockwise) { Edge<TreeNode> first = edges.get(0); removeEdge(first); addEdge(first); } else { Edge<TreeNode> first = edges.get(edges.size() - 1); removeEdge(first); edges.add(0, first); } } }
/** * Clears the node. Removes all edges and null references. */ @SuppressWarnings("unchecked") public void clear() { if (incompingEdge != null) { incompingEdge.getSource().removeEdge(incompingEdge); incompingEdge = null; } if (edges != null) { for (Edge out : edges) { out.getTarget().removeEdge(out); } edges.clear(); } }
/** * Create a new {@link Tree} that represents the subtree of this tree rooted at the given * node and removes this subtree from this tree. The returned tree is a new tree that contains the original {@link TreeNode}s of this tree. * <p> * If this tree does not contain the given node, null is returned. * * @param n the root node of the subtree * @return subtree rooted at the given node or null */ public List<TreeNode> removeSubtree(TreeNode n) { if (n == null) throw new NullPointerException(); if (nodes.get(n.getIndex()) != n) { return null; } LinkedList<TreeNode> nodes = new LinkedList<>(); for (TreeNode node : n.depthFirstIterator()) { nodes.push(node); this.nodes.remove(node.getIndex()); } if (n.incompingEdge != null) { n.incompingEdge.getSource().removeEdge(n.incompingEdge); n.incompingEdge = null; } nodes.push(n); this.nodes.remove(n.getIndex()); return nodes; }
ancestor.removeEdge(t.getEdge(ancestor, node)); x++;