E
- Kind of elements used as this graph's nodes.public final class Graph<E> extends Object
This boils down to maintaining a list of children and parents of each node, updating them in sync with each other.
Take note that the elements of this graph are not necessarily all connected together. This can be used to represent a set of trees, a set of undirected graphs, a set of roots with no children...
Constructor and Description |
---|
Graph()
Constructs an empty graph.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Adds a new element to this graph, if it does not exists yet.
|
void |
addChildren(E element,
Set<E> newChildren)
Connects the given set of elements to a given parent.
|
PruningIterator<E> |
breadthFirstIterator()
Returns a breadth-first iterator over this whole graph.
|
void |
clear()
Clears this graph and goes back to a pristine state.
|
boolean |
contains(E element)
Checks whether this graph already contains the given element.
|
Set<E> |
getDirectParents(E element)
Returns the direct parents of the given
element . |
Set<E> |
getSubgraphContaining(E element)
Returns the set of all elements of the subgraph containing the given element.
|
Set<E> |
getSubgraphContaining(E element,
Set<E> endPoints)
Returns the set of all elements of the subgraph containing the given element and ending at the given
boundaries.
|
Set<E> |
getTreeFrom(E root)
Returns the tree starting from the given root element if it is contained in the graph.
|
Set<E> |
getTreeFrom(E root,
Set<E> endPoints)
Returns the tree starting from the given root element and ending at the given boundaries..
|
boolean |
hasChild(E parent,
E potentialChild)
Checks if the given element is a parent of the given potential child, directly or not.
|
void |
remove(E element)
Removes the given element's node from this graph.
|
void |
removeAll(Collection<E> elements)
Removes the given elements' nodes from this graph.
|
public boolean contains(E element)
element
- Element we need to check.true
if this graph already contains the given elment, false
otherwise.public void clear()
public boolean add(E element)
element
- The element to add as a new root to this graph.true
if this element did not previously exist in the graph.public void remove(E element)
element
- The element which is to be removed from this graph.public void removeAll(Collection<E> elements)
elements
- The elements which are to be removed from this graph.public void addChildren(E element, Set<E> newChildren)
element
- The element that is to be connected with new children.newChildren
- The set of elements to connect to the given parent.public boolean hasChild(E parent, E potentialChild)
parent
- Element that could be a parent of potentialChild
.potentialChild
- The potential child of parent
.true
if parent
is an ancestor of potentialChild
.public Set<E> getDirectParents(E element)
element
.
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
element
- The element which parents we seek.element
.public Set<E> getTreeFrom(E root)
Contrarily to getSubgraphContaining(Object)
, this will only iterate over the children (and
recursively) of the given node, without ever "going up" to parents of these children.
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
root
- The element we are to consider as the root of a tree.public Set<E> getTreeFrom(E root, Set<E> endPoints)
Contrarily to getSubgraphContaining(Object, Set)
, this will only iterate over the children
(and recursively) of the given node, without ever "going up" to parents of these children.
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
root
- The element we are to consider as the root of a tree.endPoints
- Boundaries of the tree.public Set<E> getSubgraphContaining(E element)
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
element
- Element we need the subgraph of.public Set<E> getSubgraphContaining(E element, Set<E> endPoints)
Note that the returned set is a view over a sub-graph of this graph, and that changes to it will not be reflected within the graph itself.
element
- Element we need the subgraph of.endPoints
- Boundaries of the needed subgraph.public PruningIterator<E> breadthFirstIterator()
The returned iterator does not support removal, and will fail or returned undefined results if this graph is modified after the iterator's creation.
Copyright (c) 2006, 2014 Obeo and others. All rights reserved.