// Removes all nodes from the tree void clear() { if(m_root != null_vertex()) { delete m_root; m_root = static_cast< node_t* >(null_vertex()); } }
// Is vertex u (of the root graph) contained in this subgraph? // If so, return the matching local vertex. std::pair<vertex_descriptor, bool> find_vertex(vertex_descriptor u_global) const { if (is_root()) return std::make_pair(u_global, true); typename LocalVertexMap::const_iterator i = m_local_vertex.find(u_global); bool valid = i != m_local_vertex.end(); return std::make_pair((valid ? (*i).second : null_vertex()), valid); }
// If n has a single child, cuts out n and splices its child c to its parent (or if n was the root, sets c to be the new root), then returns (c, true) // Otherwise, returns (null_vertex(), false) t_vert_bool cut_and_splice(node_descriptor n) { // Can only cut and splice a node with exactly 1 child if(child_count(n) != 1) { return t_vert_bool(null_vertex(), false); } // Get a descriptor for the only child node_descriptor c = get_nth_child(n, 0).first; // And having gotten this, we can now disconnect it and its subbranch from n n->children.clear(); // Get n's parent, p, if it exists bool has_parent; node_descriptor p; boost::tie(p, has_parent) = get_parent(n); if(has_parent) { // Replace the vertex descriptor for n in p's children list with c child_list_t::iterator c_it = child_it_from_node_descriptor(n); *c_it = c; // Set c's parent to be p c->parent = p; } else { // Must be root node, just set c to be the new root m_root = static_cast< node_t* >(c); c->parent = null_vertex(); } // Now n is disconnected we can safely delete it delete static_cast< node_t* >(n); return t_vert_bool(c, true); }
// Removes an entire subbranch starting from the specified node void remove_branch(node_descriptor n) { bool has_parent; node_descriptor p; boost::tie(p, has_parent) = get_parent(n); if(has_parent) { node_t* node = static_cast< node_t* >(n); child_list_t::iterator it = child_it_from_node_descriptor(node); delete node; p->children.erase(it); } else { // n must be the root // TODO: assert(n == m_root) delete m_root; m_root = static_cast< node_t* >(null_vertex()); } }
generic_tree(): m_root(static_cast< node_t* >(null_vertex())) {}
/*! Returns a pre order iterator range for the subtree starting at n_start, or for the whole tree if n_start == null_vertex() */ inline pre_order_iterator_range pre_order_traversal(node_descriptor n_start = null_vertex()) const { return super_t::pre_order_traversal(n_start == null_vertex() ? m_root : n_start); }
// Is the tree empty? inline bool empty() const { return m_root == null_vertex(); }
static inline node_t* copy_node(node_descriptor n) { return n == null_vertex() ? static_cast< node_t* >(null_vertex()) : new node_t(*static_cast< node_t* >(n)); }