diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-04-11 18:58:10 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-04-11 18:58:10 -0300 |
| commit | 4ba17336e900a5c1fcf9d9b0a9d87fa6f62d688f (patch) | |
| tree | 27b65eff5f6fdbd943dfee93f2ddee2c2a675d51 /base/src/main/java | |
| parent | 1084970ceb94424cf08a4dae8d20cb8521c685df (diff) | |
Tree updates
Diffstat (limited to 'base/src/main/java')
| -rw-r--r-- | base/src/main/java/bjc/utils/data/ITree.java | 27 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/data/Tree.java | 17 |
2 files changed, 43 insertions, 1 deletions
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 @@ -28,6 +28,14 @@ public interface ITree<ContainedType> { void addChild(ITree<ContainedType> 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. * * @param child @@ -112,6 +120,15 @@ public interface ITree<ContainedType> { 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. * * @return The data stored in this node. @@ -237,7 +254,7 @@ public interface ITree<ContainedType> { * 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<ContainedType> { 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 @@ -43,6 +43,13 @@ public class Tree<ContainedType> implements ITree<ContainedType> { /** * Create a new leaf node in a tree. + */ + public Tree() { + this(null); + } + + /** + * Create a new leaf node in a tree. * * @param leaf * The data to store as a leaf node. @@ -101,6 +108,11 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override + public void addChild(final ContainedType child) { + addChild(new Tree(child)); + } + + @Override public void addChild(final ITree<ContainedType> child) { if(hasChildren == false) { hasChildren = true; @@ -369,6 +381,11 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override + public void setHead(ContainedType dat) { + this.data = dat; + } + + @Override public int hashCode() { final int prime = 31; int result = 1; |
