public void startDocument() { m_tree = new Tree(); m_nodes = m_tree.getNodeTable(); }
/** * Get the first child node of the given parent node. * @param node the parent Node * @return the first child Node */ public Node getFirstChild(Node node) { return getChild(node, 0); }
/** * Remove a node and its entire subtree rooted at the node from the tree. * @param node the node id (node table row number) to remove * @return true if the node and its subtree is successfully removed, * false otherwise */ public boolean removeChild(int node) { while ( getChildCount(node) > 0 ) { removeChild(getLastChildRow(node)); } return removeNode(node); }
/** * Add a new root node to an empty Tree. * @return the newly added root Node */ public Node addRoot() { return getNode(addRootRow()); }
/** * Get a node's parent node id * @param node the child node id (node table row number) * @return the parent node id, or -1 if there is no parent */ public int getParent(int node) { int pe = getParentEdge(node); return ( pe < 0 ? -1 : getSourceNode(pe) ); }
/** * Returns a left deep binary tree * @param depth the depth of the tree * @return the generated tree */ public static Tree getLeftDeepTree(int depth) { Tree t = new Tree(); t.getNodeTable().addColumns(LABEL_SCHEMA); Node r = t.addRoot(); r.setString(LABEL, "0,0"); deepHelper(t, r, 2, depth, true); return t; }
/** * Get the root node. * @return the root Node */ public Node getRoot() { try { prefuse.data.Node n = ((prefuse.data.Tree) getPrefuseGraph()).getRoot(); return new PrefuseObviousNode(n); } catch (IllegalArgumentException e) { for (Node node : this.getNodes()) { int nodeRow = node.getRow(); if (((prefuse.data.Tree) getPrefuseGraph()).getParent(nodeRow) < 0) { return new PrefuseObviousNode( ((prefuse.data.Tree) getPrefuseGraph()).getNode(nodeRow)); } } return null; } }
/** * Get the node id of the next sibling of the given node id. * @param node a node id (node table row number) * @return the node id of the next sibling, or -1 if there * is no next sibling. */ public int getNextSiblingRow(int node) { int p = getParent(node); if ( p < 0 ) return -1; int[] links = (int[])m_links.get(p, OUTLINKS); int idx = m_links.getInt(node, CHILDINDEX); int max = getChildCount(p)-1; return ( idx<0 || idx>=max ? -1 : getTargetNode(links[idx+1])); }
Tree t = new Tree(); t.getNodeTable().addColumns(LABEL_SCHEMA); Node r = t.addRoot(); r.setString(LABEL, "0,0"); Node left = t.addChild(r); left.setString(LABEL, "1,0"); Node right = t.addChild(r); right.setString(LABEL, "1,1");
/** * Get the edge to the given node's parent. * @param n a Node instance * @return the parent Edge connecting the given node to its parent */ public Edge getParentEdge(Node n) { nodeCheck(n, true); int pe = getParentEdge(n.getRow()); return ( pe < 0 ? null : getEdge(pe) ); }
/** * @see prefuse.data.Node#getParent() */ public Node getParent() { return m_graph.getSpanningTree().getParent(this); }
/** * Gets a collection of children of a node in the tree. * @param node a node of the tree * @return children of the node */ public Collection<Node> getChildNodes(Node node) { Collection<Node> nodeList = new ArrayList<Node>(); prefuse.data.Node prefNode = this.getPrefuseGraph().getNodeFromKey( this.getPrefuseGraph().getKey(node.getRow())); prefuse.data.Tree prefTree = (prefuse.data.Tree) this.getPrefuseGraph(); for (int i = 0; i < prefTree.getChildCount(node.getRow()); i++) { nodeList.add(new PrefuseObviousNode(prefTree.getChild(prefNode, i))); } return nodeList; }
/** * Get the node id of the last child of the given parent node id. * @param node the parent node id (node table row number) * @return the node id of the last child */ public int getLastChildRow(int node) { return getChildRow(node, getChildCount(node)-1); }
/** * Get the previous sibling of the given node. * @param node a node * @return the previous sibling, or null if there is no previous sibling */ public Node getPreviousSibling(Node node) { int n = getPreviousSiblingRow(node.getRow()); return ( n<0 ? null : getNode(n) ); }
/** * Get the next sibling of the given node. * @param node a node * @return the next sibling, or null if there is no next sibling */ public Node getNextSibling(Node node) { int n = getNextSiblingRow(node.getRow()); return ( n<0 ? null : getNode(n) ); }
/** * Get the depth of the given node id in the tree. * @param node a node id (node table row number) * @return the depth of the node in tree. The root node * is at a depth level of 0, with each child at a greater * depth level. -1 is returned if the input node id is not * in the tree. */ public int getDepth(int node) { if ( !getNodeTable().isValidRow(node) ) return -1; int depth = 0; if ( node!=m_root && getParent(node) < 0 ) return -1; for ( int i=node; i!=m_root && i>=0; ++depth, i=getParent(i) ); return depth; }
/** * Get the child node id at the given index. * @param node the parent node id (node table row number) * @param idx the child index * @return the child node id (node table row number) */ public int getChildRow(int node, int idx) { int cc = getChildCount(node); if ( idx < 0 || idx >= cc ) return -1; int[] links = (int[])m_links.get(node, OUTLINKS); return getTargetNode(links[idx]); }
/** * Get the child node at the given index. * @param node the parent Node * @param idx the child index * @return the child Node */ public Node getChild(Node node, int idx) { int c = getChildRow(node.getRow(), idx); return ( c<0 ? null : getNode(c) ); }