mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-24 04:54:21 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:

committed by
GitHub

parent
8e533d2617
commit
9fb3364ccc
@ -0,0 +1,77 @@
|
||||
package com.thealgorithms.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;
|
||||
}
|
||||
}
|
42
src/main/java/com/thealgorithms/devutils/nodes/Node.java
Normal file
42
src/main/java/com/thealgorithms/devutils/nodes/Node.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.thealgorithms.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;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.thealgorithms.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;
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package com.thealgorithms.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;
|
||||
}
|
||||
}
|
78
src/main/java/com/thealgorithms/devutils/nodes/TreeNode.java
Normal file
78
src/main/java/com/thealgorithms/devutils/nodes/TreeNode.java
Normal file
@ -0,0 +1,78 @@
|
||||
package com.thealgorithms.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;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.thealgorithms.devutils.searches;
|
||||
|
||||
/**
|
||||
* The common interface of most searching algorithms
|
||||
*
|
||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||
*/
|
||||
public interface SearchAlgorithm {
|
||||
|
||||
/**
|
||||
* @param key is an element which should be found
|
||||
* @param array is an array where the element should be found
|
||||
* @param <T> Comparable type
|
||||
* @return first found index of the element
|
||||
*/
|
||||
<T extends Comparable<T>> int find(T array[], T key);
|
||||
}
|
Reference in New Issue
Block a user