From ba7711b8546e08fa6376a85271d2ca1c34cc902b Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 29 Sep 2015 16:24:37 -0400 Subject: Moved around tree stuff. --- .../main/java/bjc/utils/funcdata/bst/TreeLeaf.java | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java new file mode 100644 index 0000000..1928185 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java @@ -0,0 +1,101 @@ +package bjc.utils.funcdata.bst; + +import java.util.Comparator; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * A leaf in a tree. + * @author ben + * + * @param The data stored in the tree. + */ +public class TreeLeaf implements ITreePart { + protected T data; + protected boolean deleted; + + /** + * Create a new leaf holding the specified data. + * @param dat The data for the leaf to hold. + */ + public TreeLeaf(T dat) { + data = dat; + } + + /* + * Can't add things to a leaf. + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#add(java.lang.Object, java.util.Comparator) + */ + @Override + public void add(T dat, Comparator comp) { + throw new IllegalArgumentException("Can't add to a leaf."); + } + + /* + * Just transform our data. + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#collapse(java.util.function.Function, java.util.function.BiFunction) + */ + @Override + public E collapse(Function f, BiFunction bf) { + return f.apply(data); + } + + /* + * Only check our data. + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#contains(java.lang.Object, java.util.Comparator) + */ + @Override + public boolean contains(T data, Comparator cmp) { + return this.data.equals(data); + } + + /* + * Just get the data + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#data() + */ + @Override + public T data() { + return data; + } + + /* + * Just mark ourselves as "not here" + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#delete(java.lang.Object, java.util.Comparator) + */ + @Override + public void delete(T dat, Comparator cmp) { + if(data.equals(dat)) { + deleted = true; + } + } + + /* + * Just walk our data and only succede if the walk does, because there's nowhere left to go. + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#directedWalk(bjc.utils.data.bst.DirectedWalkFunction) + */ + @Override + public boolean directedWalk(DirectedWalkFunction ds) { + switch(ds.walk(data)) { + case SUCCESS: + return true; + default: + return false; + } + } + + /* + * Just check our data. + * (non-Javadoc) + * @see bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart.TreeLinearizationMethod, java.util.function.Predicate) + */ + public boolean forEach(TreeLinearizationMethod tlm, Predicate c) { + return c.test(data); + } +} -- cgit v1.2.3