From 79cab78bf3d688bb8a359eedcfaf2575ef9d4092 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Sat, 18 Mar 2017 14:20:21 -0400 Subject: Add revFind to trees --- BJC-Utils2/src/main/java/bjc/utils/data/ITree.java | 13 ++++++++++++- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) (limited to 'BJC-Utils2/src') 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 @@ -176,4 +176,15 @@ public interface ITree { * The action to perform on each tree node */ public void traverse(TreeLinearizationMethod linearizationMethod, Consumer 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 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 implements ITree { return true; } + + @Override + public int revFind(Predicate 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; + } } -- cgit v1.2.3