Package repast.simphony.util.collections
Class NaryTree<T>
- java.lang.Object
-
- repast.simphony.util.collections.NaryTree<T>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
NaryTree.ChildIterator<T>
protected class
NaryTree.NodeComparator<T>
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNode(T parent, T child)
Adds the specified child to the tree as a child of the specified parent.boolean
contains(T parent, T child)
Checks if the tree contains the specified node with the specified parent.protected boolean
containsChecker(repast.simphony.util.collections.NaryTree.Node<T> node, T parent, T child)
Collection<T>
getChildren(T obj)
Gets the direct children of the specified node.T
getRoot()
Gets the root of the tree.Collection<T>
getSiblings(T obj)
Retrieves the siblings of the specified object in the tree.protected void
preOrderTraveralsOfNodes(repast.simphony.util.collections.NaryTree.Node<T> node, TreeVisitor<repast.simphony.util.collections.NaryTree.Node<T>> visitor)
void
preOrderTraversal(TreeVisitor<T> visitor)
Traverse the tree in preOrder - depth first, processing parents before children - applying the visitor to the nodes.boolean
removeNode(T obj)
Removes the specified object from the tree.void
replaceNode(T oldObj, T newObj)
Replaces the old object in the tree with the new one.int
size()
Gets the number of nodes currently in the tree.void
sortChildren(Comparator<T> comparator)
Sorts the children of each node w/r to each other according the specified comparator
-
-
-
Constructor Detail
-
NaryTree
public NaryTree(T rootObj)
Creates a NaryTree with the specified object as the root- Parameters:
rootObj
- the root object
-
-
Method Detail
-
getRoot
public T getRoot()
Gets the root of the tree.
-
getChildren
public Collection<T> getChildren(T obj)
Gets the direct children of the specified node. This will not return grand children etc.- Specified by:
getChildren
in interfaceTree<T>
- Returns:
- the direct children of the specified node.
- Throws:
IllegalArgumentException
- if the object is not currently in the tree.
-
addNode
public void addNode(T parent, T child)
Adds the specified child to the tree as a child of the specified parent.- Specified by:
addNode
in interfaceTree<T>
- Parameters:
parent
- the parent nodechild
- the child node- Throws:
IllegalArgumentException
- if the parent is not currently in the tree.
-
getSiblings
public Collection<T> getSiblings(T obj)
Retrieves the siblings of the specified object in the tree.- Parameters:
obj
- the object whose siblings to get- Returns:
- null if the passed in object is null, otherwise the object's siblings
-
removeNode
public boolean removeNode(T obj)
Removes the specified object from the tree. You cannot replace the root node, only#replaceNode(T, T)
it.- Specified by:
removeNode
in interfaceTree<T>
- Parameters:
obj
- the object to remove- Returns:
- true if the object was successfully removed, otherwise false.
-
replaceNode
public void replaceNode(T oldObj, T newObj)
Replaces the old object in the tree with the new one.- Specified by:
replaceNode
in interfaceTree<T>
- Parameters:
oldObj
- the old object to replacenewObj
- the new object- Throws:
IllegalArgumentException
- if the old object is not currently in the tree.
-
sortChildren
public void sortChildren(Comparator<T> comparator)
Sorts the children of each node w/r to each other according the specified comparator- Specified by:
sortChildren
in interfaceTree<T>
- Parameters:
comparator
- the comparator used to sort the children
-
size
public int size()
Gets the number of nodes currently in the tree.
-
preOrderTraversal
public void preOrderTraversal(TreeVisitor<T> visitor)
Traverse the tree in preOrder - depth first, processing parents before children - applying the visitor to the nodes.- Specified by:
preOrderTraversal
in interfaceTree<T>
- Parameters:
visitor
- the visitor to apply to the nodes
-
preOrderTraveralsOfNodes
protected void preOrderTraveralsOfNodes(repast.simphony.util.collections.NaryTree.Node<T> node, TreeVisitor<repast.simphony.util.collections.NaryTree.Node<T>> visitor)
-
containsChecker
protected boolean containsChecker(repast.simphony.util.collections.NaryTree.Node<T> node, T parent, T child)
-
-