diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-18 14:20:21 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-18 14:20:21 -0400 |
| commit | 79cab78bf3d688bb8a359eedcfaf2575ef9d4092 (patch) | |
| tree | a15fecfa56d48f6a20bf27c8423db46326b935b6 /BJC-Utils2/src/main/java/bjc/utils/data | |
| parent | a64dc2b9e8ad9b71e284f491fdfe4c2b3e60d712 (diff) | |
Add revFind to trees
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/ITree.java | 13 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 17 |
2 files changed, 28 insertions, 2 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java index 63a82b8..63d16d4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java @@ -9,7 +9,7 @@ import java.util.function.Predicate; import java.util.function.UnaryOperator; /** - * A node in a homogenous tree with a unlimited amount of children + * A node in a homogeneous tree with a unlimited amount of children * * @author ben * @param <ContainedType> @@ -176,4 +176,15 @@ public interface ITree<ContainedType> { * The action to perform on each tree node */ public void traverse(TreeLinearizationMethod linearizationMethod, Consumer<ContainedType> action); + + /** + * Find the farthest to right child that satisfies the given predicate. + * + * @param childPred + * The predicate to satisfy. + * + * @return The index of the right-most child that satisfies the + * predicate, or -1 if one doesn't exist. + */ + int revFind(Predicate<ContainedType> childPred); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index 68da7ac..697dd66 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -10,7 +10,7 @@ import java.util.function.Predicate; import java.util.function.UnaryOperator; /** - * A node in a homogenous tree. + * A node in a homogeneous tree. * * @author ben * @@ -332,4 +332,19 @@ public class Tree<ContainedType> implements ITree<ContainedType> { return true; } + + @Override + public int revFind(Predicate<ContainedType> childPred) { + if(childCount == 0) { + return -1; + } else { + for(int i = childCount - 1; i >= 0; i--) { + if(childPred.test(getChild(i).getHead())) { + return i; + } + } + } + + return -1; + } } |
