Add generic Node classes (#2782)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
parent
78f770683a
commit
7f3eb1b6dc
72
DevUtils/Nodes/LargeTreeNode.java
Normal file
72
DevUtils/Nodes/LargeTreeNode.java
Normal file
@ -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 <E> The type of the data held in the Node.
|
||||
*
|
||||
* @author <a href="https://github.com/aitorfi">aitorfi</a>
|
||||
*/
|
||||
public class LargeTreeNode<E> extends TreeNode<E> {
|
||||
/** {@link Collection} that holds the Nodes' child nodes. */
|
||||
private Collection<LargeTreeNode<E>> 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<E> 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<E> parentNode, Collection<LargeTreeNode<E>> 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<LargeTreeNode<E>> getChildNodes() {
|
||||
return childNodes;
|
||||
}
|
||||
|
||||
public void setChildNodes(Collection<LargeTreeNode<E>> childNodes) {
|
||||
this.childNodes = childNodes;
|
||||
}
|
||||
}
|
36
DevUtils/Nodes/Node.java
Normal file
36
DevUtils/Nodes/Node.java
Normal file
@ -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 <E> The type of the data held in the Node.
|
||||
*
|
||||
* @author <a href="https://github.com/aitorfi">aitorfi</a>
|
||||
*/
|
||||
public abstract class Node<E> {
|
||||
/** 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;
|
||||
}
|
||||
}
|
55
DevUtils/Nodes/SimpleNode.java
Normal file
55
DevUtils/Nodes/SimpleNode.java
Normal file
@ -0,0 +1,55 @@
|
||||
package DevUtils.Nodes;
|
||||
|
||||
/**
|
||||
* Simple Node implementation that holds
|
||||
* a reference to the next Node.
|
||||
*
|
||||
* @param <E> The type of the data held in the Node.
|
||||
*
|
||||
* @author <a href="https://github.com/aitorfi">aitorfi</a>
|
||||
*/
|
||||
public class SimpleNode<E> extends Node<E> {
|
||||
/** Reference to the next Node. */
|
||||
private SimpleNode<E> 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<E> nextNode) {
|
||||
super(data);
|
||||
this.nextNode = nextNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if there is a next node, otherwise false.
|
||||
*/
|
||||
public boolean hasNext() {
|
||||
return (nextNode != null);
|
||||
}
|
||||
|
||||
public SimpleNode<E> getNextNode() {
|
||||
return nextNode;
|
||||
}
|
||||
|
||||
public void setNextNode(SimpleNode<E> nextNode) {
|
||||
this.nextNode = nextNode;
|
||||
}
|
||||
}
|
81
DevUtils/Nodes/SimpleTreeNode.java
Normal file
81
DevUtils/Nodes/SimpleTreeNode.java
Normal file
@ -0,0 +1,81 @@
|
||||
package DevUtils.Nodes;
|
||||
|
||||
/**
|
||||
* Simple TreeNode extension that holds references
|
||||
* to two child Nodes (left and right).
|
||||
*
|
||||
* @param <E> The type of the data held in the Node.
|
||||
*
|
||||
* @author <a href="https://github.com/aitorfi">aitorfi</a>
|
||||
*/
|
||||
public class SimpleTreeNode<E> extends TreeNode<E> {
|
||||
/** Refrence to the child Node on the left. */
|
||||
private SimpleTreeNode<E> leftNode;
|
||||
/** Refrence to the child Node on the right. */
|
||||
private SimpleTreeNode<E> 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<E> 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<E> parentNode, SimpleTreeNode<E> leftNode, SimpleTreeNode<E> 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<E> getLeftNode() {
|
||||
return leftNode;
|
||||
}
|
||||
|
||||
public void setLeftNode(SimpleTreeNode<E> leftNode) {
|
||||
this.leftNode = leftNode;
|
||||
}
|
||||
|
||||
public SimpleTreeNode<E> getRightNode() {
|
||||
return rightNode;
|
||||
}
|
||||
|
||||
public void setRightNode(SimpleTreeNode<E> rightNode) {
|
||||
this.rightNode = rightNode;
|
||||
}
|
||||
}
|
72
DevUtils/Nodes/TreeNode.java
Normal file
72
DevUtils/Nodes/TreeNode.java
Normal file
@ -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 <E> The type of the data held in the Node.
|
||||
*
|
||||
* @author <a href="https://github.com/aitorfi">aitorfi</a>
|
||||
*/
|
||||
public abstract class TreeNode<E> extends Node<E> {
|
||||
/** Refernce to the parent Node. */
|
||||
private TreeNode<E> 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<E> 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<E> getParent() {
|
||||
return parentNode;
|
||||
}
|
||||
|
||||
public void setParent(TreeNode<E> parentNode) {
|
||||
this.parentNode = parentNode;
|
||||
depth = this.parentNode.getDepth() + 1;
|
||||
}
|
||||
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package Searches;
|
||||
package DevUtils.Searches;
|
||||
|
||||
/**
|
||||
* The common interface of most searching algorithms
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -1,5 +1,7 @@
|
||||
package Searches;
|
||||
|
||||
import DevUtils.Searches.SearchAlgorithm;
|
||||
|
||||
public class JumpSearch implements SearchAlgorithm {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user