From 7f3eb1b6dc582728f21596fd58c5d8273db0fabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Fidalgo=20S=C3=A1nchez?= <64830228+aitorfi@users.noreply.github.com> Date: Mon, 8 Nov 2021 19:03:06 +0100 Subject: [PATCH] Add generic Node classes (#2782) Co-authored-by: Andrii Siriak --- DevUtils/Nodes/LargeTreeNode.java | 72 +++++++++++++++++ DevUtils/Nodes/Node.java | 36 +++++++++ DevUtils/Nodes/SimpleNode.java | 55 +++++++++++++ DevUtils/Nodes/SimpleTreeNode.java | 81 +++++++++++++++++++ DevUtils/Nodes/TreeNode.java | 72 +++++++++++++++++ .../Searches}/SearchAlgorithm.java | 2 +- Searches/BinarySearch.java | 1 + Searches/ExponentalSearch.java | 1 + Searches/FibonacciSearch.java | 2 + Searches/IterativeBinarySearch.java | 1 + Searches/IterativeTernarySearch.java | 1 + Searches/JumpSearch.java | 2 + Searches/LinearSearch.java | 1 + Searches/LowerBound.java | 1 + Searches/TernarySearch.java | 1 + Searches/UpperBound.java | 1 + 16 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 DevUtils/Nodes/LargeTreeNode.java create mode 100644 DevUtils/Nodes/Node.java create mode 100644 DevUtils/Nodes/SimpleNode.java create mode 100644 DevUtils/Nodes/SimpleTreeNode.java create mode 100644 DevUtils/Nodes/TreeNode.java rename {Searches => DevUtils/Searches}/SearchAlgorithm.java (94%) diff --git a/DevUtils/Nodes/LargeTreeNode.java b/DevUtils/Nodes/LargeTreeNode.java new file mode 100644 index 000000000..678608e73 --- /dev/null +++ b/DevUtils/Nodes/LargeTreeNode.java @@ -0,0 +1,72 @@ +package DevUtils.Nodes; + +import java.util.Collection; + +/** + * {@link TreeNode} extension that holds a {@link Collection} + * of refrences to child Nodes. + * + * @param The type of the data held in the Node. + * + * @author aitorfi + */ +public class LargeTreeNode extends TreeNode { + /** {@link Collection} that holds the Nodes' child nodes. */ + private Collection> childNodes; + + /** Empty contructor. */ + public LargeTreeNode() { + super(); + } + + /** + * Initializes the Nodes' data. + * + * @param data Value to which data will be initialized. + * @see TreeNode#TreeNode(Object) + */ + public LargeTreeNode(E data) { + super(data); + } + + /** + * Initializes the Nodes' data and parent node reference. + * + * @param data Value to which data will be initialized. + * @param parentNode Value to which the nodes' parent reference will be set. + * @see TreeNode#TreeNode(Object, Node) + */ + public LargeTreeNode(E data, LargeTreeNode parentNode) { + super(data, parentNode); + } + + /** + * Initializes the Nodes' data and parent and child nodes references. + * + * @param data Value to which data will be initialized. + * @param parentNode Value to which the nodes' parent reference will be set. + * @param childNodes {@link Collection} of child Nodes. + * @see TreeNode#TreeNode(Object, Node) + */ + public LargeTreeNode(E data, LargeTreeNode parentNode, Collection> childNodes) { + super(data, parentNode); + this.childNodes = childNodes; + } + + /** + * @return True if the node is a leaf node, otherwise false. + * @see TreeNode#isLeafNode() + */ + @Override + public boolean isLeafNode() { + return (childNodes == null || childNodes.size() == 0); + } + + public Collection> getChildNodes() { + return childNodes; + } + + public void setChildNodes(Collection> childNodes) { + this.childNodes = childNodes; + } +} diff --git a/DevUtils/Nodes/Node.java b/DevUtils/Nodes/Node.java new file mode 100644 index 000000000..a00b79e1a --- /dev/null +++ b/DevUtils/Nodes/Node.java @@ -0,0 +1,36 @@ +package DevUtils.Nodes; + +/** + * Base class for any node implementation which + * contains a generic type variable. + * + * All known subclasses: {@link TreeNode}, {@link SimpleNode}. + * + * @param The type of the data held in the Node. + * + * @author aitorfi + */ +public abstract class Node { + /** Generic type data stored in the Node. */ + private E data; + + /** Empty constructor. */ + public Node() {} + + /** + * Initializes the Nodes' data. + * + * @param data Value to which data will be initialized. + */ + public Node(E data) { + this.data = data; + } + + public E getData() { + return data; + } + + public void setData(E data) { + this.data = data; + } +} diff --git a/DevUtils/Nodes/SimpleNode.java b/DevUtils/Nodes/SimpleNode.java new file mode 100644 index 000000000..b1a112eb9 --- /dev/null +++ b/DevUtils/Nodes/SimpleNode.java @@ -0,0 +1,55 @@ +package DevUtils.Nodes; + +/** + * Simple Node implementation that holds + * a reference to the next Node. + * + * @param The type of the data held in the Node. + * + * @author aitorfi + */ +public class SimpleNode extends Node { + /** Reference to the next Node. */ + private SimpleNode nextNode; + + /** Empty contructor. */ + public SimpleNode() { + super(); + } + + /** + * Initializes the Nodes' data. + * + * @param data Value to which data will be initialized. + * @see Node#Node(Object) + */ + public SimpleNode(E data) { + super(data); + } + + /** + * Initializes the Nodes' data and next node reference. + * + * @param data Value to which data will be initialized. + * @param nextNode Value to which the next node reference will be set. + */ + public SimpleNode(E data, SimpleNode nextNode) { + super(data); + this.nextNode = nextNode; + } + + /** + * @return True if there is a next node, otherwise false. + */ + public boolean hasNext() { + return (nextNode != null); + } + + public SimpleNode getNextNode() { + return nextNode; + } + + public void setNextNode(SimpleNode nextNode) { + this.nextNode = nextNode; + } +} diff --git a/DevUtils/Nodes/SimpleTreeNode.java b/DevUtils/Nodes/SimpleTreeNode.java new file mode 100644 index 000000000..25611fc6b --- /dev/null +++ b/DevUtils/Nodes/SimpleTreeNode.java @@ -0,0 +1,81 @@ +package DevUtils.Nodes; + +/** + * Simple TreeNode extension that holds references + * to two child Nodes (left and right). + * + * @param The type of the data held in the Node. + * + * @author aitorfi + */ +public class SimpleTreeNode extends TreeNode { + /** Refrence to the child Node on the left. */ + private SimpleTreeNode leftNode; + /** Refrence to the child Node on the right. */ + private SimpleTreeNode rightNode; + + /** Empty contructor. */ + public SimpleTreeNode() { + super(); + } + + /** + * Initializes the Nodes' data. + * + * @param data Value to which data will be initialized. + * @see TreeNode#TreeNode(Object) + */ + public SimpleTreeNode(E data) { + super(data); + } + + /** + * Initializes the Nodes' data and parent node reference. + * + * @param data Value to which data will be initialized. + * @param parentNode Value to which the nodes' parent reference will be set. + * @see TreeNode#TreeNode(Object, Node) + */ + public SimpleTreeNode(E data, SimpleTreeNode parentNode) { + super(data, parentNode); + } + + /** + * Initializes the Nodes' data and parent and child nodes references. + * + * @param data Value to which data will be initialized. + * @param parentNode Value to which the nodes' parent reference will be set. + * @param leftNode Value to which the nodes' left child reference will be set. + * @param rightNode Value to which the nodes' right child reference will be set. + */ + public SimpleTreeNode(E data, SimpleTreeNode parentNode, SimpleTreeNode leftNode, SimpleTreeNode rightNode) { + super(data, parentNode); + this.leftNode = leftNode; + this.rightNode = rightNode; + } + + /** + * @return True if the node is a leaf node, otherwise false. + * @see TreeNode#isLeafNode() + */ + @Override + public boolean isLeafNode() { + return (leftNode == null && rightNode == null); + } + + public SimpleTreeNode getLeftNode() { + return leftNode; + } + + public void setLeftNode(SimpleTreeNode leftNode) { + this.leftNode = leftNode; + } + + public SimpleTreeNode getRightNode() { + return rightNode; + } + + public void setRightNode(SimpleTreeNode rightNode) { + this.rightNode = rightNode; + } +} diff --git a/DevUtils/Nodes/TreeNode.java b/DevUtils/Nodes/TreeNode.java new file mode 100644 index 000000000..bac29770f --- /dev/null +++ b/DevUtils/Nodes/TreeNode.java @@ -0,0 +1,72 @@ +package DevUtils.Nodes; + +/** + * Base class for any tree node which + * holds a reference to the parent node. + * + * All known subclasses: {@link SimpleTreeNode}, {@link LargeTreeNode}. + * + * @param The type of the data held in the Node. + * + * @author aitorfi + */ +public abstract class TreeNode extends Node { + /** Refernce to the parent Node. */ + private TreeNode parentNode; + /** Indicates the depth at which this node is in the tree. */ + private int depth; + + /** Empty contructor. */ + public TreeNode() { + super(); + depth = 0; + } + + /** + * Initializes the Nodes' data. + * + * @param data Value to which data will be initialized. + * @see Node#Node(Object) + */ + public TreeNode(E data) { + super(data); + depth = 0; + } + + /** + * Initializes the Nodes' data and parent node reference. + * + * @param data Value to which data will be initialized. + * @param parentNode Value to which the nodes' parent reference will be set. + */ + public TreeNode(E data, TreeNode parentNode) { + super(data); + this.parentNode = parentNode; + depth = this.parentNode.getDepth() + 1; + } + + /** + * @return True if the node is a leaf node, otherwise false. + */ + public abstract boolean isLeafNode(); + + /** + * @return True if the node is the root node, otherwise false. + */ + public boolean isRootNode() { + return (parentNode == null); + } + + public TreeNode getParent() { + return parentNode; + } + + public void setParent(TreeNode parentNode) { + this.parentNode = parentNode; + depth = this.parentNode.getDepth() + 1; + } + + public int getDepth() { + return depth; + } +} diff --git a/Searches/SearchAlgorithm.java b/DevUtils/Searches/SearchAlgorithm.java similarity index 94% rename from Searches/SearchAlgorithm.java rename to DevUtils/Searches/SearchAlgorithm.java index 1d5262d3a..875b98fc3 100644 --- a/Searches/SearchAlgorithm.java +++ b/DevUtils/Searches/SearchAlgorithm.java @@ -1,4 +1,4 @@ -package Searches; +package DevUtils.Searches; /** * The common interface of most searching algorithms diff --git a/Searches/BinarySearch.java b/Searches/BinarySearch.java index b7dc37dc9..52e5cc998 100644 --- a/Searches/BinarySearch.java +++ b/Searches/BinarySearch.java @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; +import DevUtils.Searches.SearchAlgorithm; /** * Binary search is one of the most popular algorithms The algorithm finds the position of a target diff --git a/Searches/ExponentalSearch.java b/Searches/ExponentalSearch.java index 4b8e7afca..8095031f4 100644 --- a/Searches/ExponentalSearch.java +++ b/Searches/ExponentalSearch.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; +import DevUtils.Searches.SearchAlgorithm; import static java.lang.String.format; diff --git a/Searches/FibonacciSearch.java b/Searches/FibonacciSearch.java index ecc2a89ff..1cb0a54df 100644 --- a/Searches/FibonacciSearch.java +++ b/Searches/FibonacciSearch.java @@ -1,5 +1,7 @@ package Searches; +import DevUtils.Searches.SearchAlgorithm; + /* * Fibonacci Search is a popular algorithm which finds the position of a target value in * a sorted array diff --git a/Searches/IterativeBinarySearch.java b/Searches/IterativeBinarySearch.java index d41cbc246..651f3bfa4 100644 --- a/Searches/IterativeBinarySearch.java +++ b/Searches/IterativeBinarySearch.java @@ -5,6 +5,7 @@ import static java.lang.String.format; import java.util.Arrays; import java.util.Random; import java.util.stream.Stream; +import DevUtils.Searches.SearchAlgorithm; /** * Binary search is one of the most popular algorithms This class represents iterative version diff --git a/Searches/IterativeTernarySearch.java b/Searches/IterativeTernarySearch.java index 7c6adc505..92cefb9f1 100644 --- a/Searches/IterativeTernarySearch.java +++ b/Searches/IterativeTernarySearch.java @@ -5,6 +5,7 @@ import static java.lang.String.format; import java.util.Arrays; import java.util.Random; import java.util.stream.Stream; +import DevUtils.Searches.SearchAlgorithm; /** * A iterative version of a ternary search algorithm This is better way to implement the ternary diff --git a/Searches/JumpSearch.java b/Searches/JumpSearch.java index 2770bbf86..9026180b3 100644 --- a/Searches/JumpSearch.java +++ b/Searches/JumpSearch.java @@ -1,5 +1,7 @@ package Searches; +import DevUtils.Searches.SearchAlgorithm; + public class JumpSearch implements SearchAlgorithm { public static void main(String[] args) { diff --git a/Searches/LinearSearch.java b/Searches/LinearSearch.java index e8da9ab19..bef0c3361 100644 --- a/Searches/LinearSearch.java +++ b/Searches/LinearSearch.java @@ -2,6 +2,7 @@ package Searches; import java.util.Random; import java.util.stream.Stream; +import DevUtils.Searches.SearchAlgorithm; /** * Linear search is the easiest search algorithm It works with sorted and unsorted arrays (an binary diff --git a/Searches/LowerBound.java b/Searches/LowerBound.java index 116d0582b..b18a20781 100644 --- a/Searches/LowerBound.java +++ b/Searches/LowerBound.java @@ -5,6 +5,7 @@ import static java.lang.String.format; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; +import DevUtils.Searches.SearchAlgorithm; /** * The LowerBound method is used to return an index pointing to the first element in the range diff --git a/Searches/TernarySearch.java b/Searches/TernarySearch.java index 20b0c436a..e01c0ad70 100644 --- a/Searches/TernarySearch.java +++ b/Searches/TernarySearch.java @@ -5,6 +5,7 @@ import static java.lang.String.format; import java.util.Arrays; import java.util.Random; import java.util.stream.Stream; +import DevUtils.Searches.SearchAlgorithm; /** * A ternary search algorithm is a technique in computer science for finding the minimum or maximum diff --git a/Searches/UpperBound.java b/Searches/UpperBound.java index 6f895fcaf..106a37af1 100644 --- a/Searches/UpperBound.java +++ b/Searches/UpperBound.java @@ -5,6 +5,7 @@ import static java.lang.String.format; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; +import DevUtils.Searches.SearchAlgorithm; /** * The UpperBound method is used to return an index pointing to the first element in the range