From 4ba17336e900a5c1fcf9d9b0a9d87fa6f62d688f Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Thu, 11 Apr 2019 18:58:10 -0300 Subject: Tree updates --- base/src/main/java/bjc/utils/data/ITree.java | 27 ++++++++++++++++++++++++++- base/src/main/java/bjc/utils/data/Tree.java | 17 +++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/base/src/main/java/bjc/utils/data/ITree.java b/base/src/main/java/bjc/utils/data/ITree.java index bbeaa3f..8ccf50e 100644 --- a/base/src/main/java/bjc/utils/data/ITree.java +++ b/base/src/main/java/bjc/utils/data/ITree.java @@ -27,6 +27,14 @@ public interface ITree { */ void addChild(ITree child); + /** + * Append a child to this node. + * + * @param child + * The child to append to this node. + */ + void addChild(ContainedType child); + /** * Prepend a child to this node. * @@ -111,6 +119,15 @@ public interface ITree { */ int getChildrenCount(); + /** + * Get a count of the number of direct children this node has. + * + * @return The number of direct children this node has. + */ + default int size() { + return getChildrenCount(); + } + /** * Get the data stored in this node. * @@ -237,7 +254,7 @@ public interface ITree { * Check if this tree contains any nodes that satisfy the predicate. * * @param pred - * The predicate to look for. + * The predicate to look for. * * @return Whether or not any items satisfied the predicate. */ @@ -250,4 +267,12 @@ public interface ITree { return tog.get(); } + + /** + * Set the head of the tree. + * + * @param dat + * The value to set as the head of the tree. + */ + void setHead(ContainedType dat); } diff --git a/base/src/main/java/bjc/utils/data/Tree.java b/base/src/main/java/bjc/utils/data/Tree.java index 40ed0f5..c8e167e 100644 --- a/base/src/main/java/bjc/utils/data/Tree.java +++ b/base/src/main/java/bjc/utils/data/Tree.java @@ -41,6 +41,13 @@ public class Tree implements ITree { /* The next ID to assign to a node. */ private static int nextID = 0; + /** + * Create a new leaf node in a tree. + */ + public Tree() { + this(null); + } + /** * Create a new leaf node in a tree. * @@ -100,6 +107,11 @@ public class Tree implements ITree { } } + @Override + public void addChild(final ContainedType child) { + addChild(new Tree(child)); + } + @Override public void addChild(final ITree child) { if(hasChildren == false) { @@ -368,6 +380,11 @@ public class Tree implements ITree { return transformer.apply(data); } + @Override + public void setHead(ContainedType dat) { + this.data = dat; + } + @Override public int hashCode() { final int prime = 31; -- cgit v1.2.3