Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/rxm.git
source/java/com/whitemagicsoftware/rxm/Tree.java
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.nio.CharBuffer;
/**
* Stores a tree of values.
*/
public class Tree<T> {
+ /** Data associated with this leaf. */
private T payload;
+ /** Root of this tree, possibly null. */
private Tree<T> parent;
+
+ /** Child trees of this tree, possibly empty. */
private List<Tree<T>> leaves = new ArrayList<Tree<T>>();
- private Map<T, Tree<T>> map = new HashMap<T, Tree<T>>();
/**
* Constructs a new tree, ready to have leaves added.
*
* @param payload The data associated with this tree leaf (must not
* be null).
*/
public Tree( T payload ) {
setPayload( payload );
- put( payload, this );
}
/**
* Adds a new leaf to this tree.
*
* @param payload The data to add to the leaf.
* @return The tree containing the given payload.
*/
public Tree<T> addLeaf( T payload ) {
- Tree<T> tree = createTree( payload );
-
- add( tree );
- put( payload, tree );
-
- return tree;
+ return add( createTree( payload ) );
}
/**
- * Adds a new leaf to the given root. If the root is not already
- * part of this tree, it is added as a leaf.
+ * Adds a tree to the list of leaves.
*
- * @param root The payload for the root tree to which the given payload
- * is added.
- * @param payload The payload to add to the given root.
+ * @return The tree that was added (to simplify the addLeaf method).
*/
- public void addLeaf( T root, T payload ) {
- (contains( root ) ? get( root ) : addLeaf( root )).addLeaf( payload );
- }
-
- private void add( Tree<T> tree ) {
+ private Tree<T> add( Tree<T> tree ) {
getLeaves().add( tree );
tree.setParent( this );
- tree.setMap( getMap() );
- }
-
- private boolean contains( T payload ) {
- return getMap().containsKey( payload );
- }
-
- private void put( T payload, Tree<T> parent ) {
- getMap().put( payload, parent );
- }
-
- private Tree<T> get( T payload ) {
- return getMap().get( payload );
+ return tree;
}
+ /**
+ * Sets the parent tree to this tree.
+ *
+ * @param parent The root for this tree.
+ */
private void setParent( Tree<T> parent ) {
this.parent = parent;
}
+ /**
+ * Returns the parent tree to this tree, or null if this is the tree top.
+ *
+ * @return The root of this tree, or null if no more trees exist above.
+ */
public Tree<T> getParent() {
return this.parent;
}
+ /**
+ * Sets the data for this leaf in the tree.
+ *
+ * @param payload The data to associate with this leaf.
+ */
private void setPayload( T payload ) {
this.payload = payload;
public T getPayload() {
return this.payload;
- }
-
- private void setMap( Map<T, Tree<T>> map ) {
- this.map = map;
- }
-
- private Map<T, Tree<T>> getMap() {
- return this.map;
}

Removed unused code. Added comments to tree class.

Author Dave Jarvis <email>
Date 2015-03-05 20:11:13 GMT-0800
Commit 99939d27d31473ab225cec3f3698f5807d5985af
Parent c717449
Delta 24 lines added, 41 lines removed, 17-line decrease