Skip to content

Added Node generic classes #2782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions DevUtils/Nodes/LargeTreeNode.java
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions DevUtils/Nodes/Node.java
Original file line number Diff line number Diff line change
@@ -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 changes: 55 additions & 0 deletions DevUtils/Nodes/SimpleNode.java
Original file line number Diff line number Diff line change
@@ -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 changes: 81 additions & 0 deletions DevUtils/Nodes/SimpleTreeNode.java
Original file line number Diff line number Diff line change
@@ -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 changes: 72 additions & 0 deletions DevUtils/Nodes/TreeNode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Searches;
package DevUtils.Searches;

/**
* The common interface of most searching algorithms
Expand Down
1 change: 1 addition & 0 deletions Searches/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
Expand Down
1 change: 1 addition & 0 deletions Searches/ExponentalSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import DevUtils.Searches.SearchAlgorithm;

import static java.lang.String.format;

Expand Down
2 changes: 2 additions & 0 deletions Searches/FibonacciSearch.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions Searches/IterativeBinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down
1 change: 1 addition & 0 deletions Searches/IterativeTernarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down
2 changes: 2 additions & 0 deletions Searches/JumpSearch.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Searches;

import DevUtils.Searches.SearchAlgorithm;

public class JumpSearch implements SearchAlgorithm {

public static void main(String[] args) {
Expand Down
1 change: 1 addition & 0 deletions Searches/LinearSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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
Expand Down
1 change: 1 addition & 0 deletions Searches/LowerBound.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down
1 change: 1 addition & 0 deletions Searches/TernarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down
Loading