Package repast.simphony.util.collections
Class NaryTree<T>
java.lang.Object
repast.simphony.util.collections.NaryTree<T>
- All Implemented Interfaces:
Tree<T>
A rooted tree where each node can have n number of children.
- Version:
- $Revision$ $Date$
- Author:
- Nick Collier
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
protected class
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the specified child to the tree as a child of the specified parent.boolean
Checks if the tree contains the specified node with the specified parent.protected boolean
containsChecker
(NaryTree<T>.repast.simphony.util.collections.NaryTree.Node<T> node, T parent, T child) getChildren
(T obj) Gets the direct children of the specified node.getRoot()
Gets the root of the tree.getSiblings
(T obj) Retrieves the siblings of the specified object in the tree.protected void
preOrderTraveralsOfNodes
(NaryTree<T>.repast.simphony.util.collections.NaryTree.Node<T> node, TreeVisitor<NaryTree<T>.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 Details
-
NaryTree
Creates a NaryTree with the specified object as the root- Parameters:
rootObj
- the root object
-
-
Method Details
-
getRoot
Gets the root of the tree. -
getChildren
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
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
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
Removes the specified object from the tree. You cannot replace the root node, onlyreplaceNode(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
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
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
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
-
containsChecker
-
contains
Checks if the tree contains the specified node with the specified parent. This will stop when it finds the first node corresponding to the parent, meaning, if multiple nodes correspond to the parent, only the first one will be checked.
-