Skip to content

Commit 6c4cc70

Browse files
authored
Merge branch 'master' into master
2 parents 9806e7c + ba6a3d6 commit 6c4cc70

18 files changed

+487
-1
lines changed

DataStructures/Graphs/MatrixGraphs.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
import java.util.ArrayList;
66
import java.util.LinkedList;
77

8+
/**
9+
* Implementation of a graph in a matrix form
10+
* Also known as an adjacency matrix representation
11+
* [Adjacency matrix - Uncyclopedia](https://en.wikipedia.org/wiki/Adjacency_matrix)
12+
*
13+
* @author Unknown
14+
*/
815
public class MatrixGraphs {
916

1017
public static void main(String args[]) {
@@ -31,14 +38,35 @@ public static void main(String args[]) {
3138
}
3239
}
3340

41+
/**
42+
* AdjacencyMatrixGraph Implementation
43+
*/
3444
class AdjacencyMatrixGraph {
45+
/**
46+
* The number of vertices in the graph
47+
*/
3548
private int _numberOfVertices;
49+
50+
/**
51+
* The number of edges in the graph
52+
*/
3653
private int _numberOfEdges;
54+
55+
/**
56+
* The adjacency matrix for the graph
57+
*/
3758
private int[][] _adjacency;
3859

60+
/**
61+
* Static variables to define whether or not an edge exists in the
62+
* adjacency matrix
63+
*/
3964
static final int EDGE_EXIST = 1;
4065
static final int EDGE_NONE = 0;
4166

67+
/**
68+
* Constructor
69+
*/
4270
public AdjacencyMatrixGraph(int givenNumberOfVertices) {
4371
this.setNumberOfVertices(givenNumberOfVertices);
4472
this.setNumberOfEdges(0);
@@ -50,34 +78,77 @@ public AdjacencyMatrixGraph(int givenNumberOfVertices) {
5078
}
5179
}
5280

81+
/**
82+
* Updates the number of vertices in the graph
83+
*
84+
* @param newNumberOfVertices the new number of vertices
85+
*/
5386
private void setNumberOfVertices(int newNumberOfVertices) {
5487
this._numberOfVertices = newNumberOfVertices;
5588
}
5689

90+
/**
91+
* Getter for `this._numberOfVertices`
92+
*
93+
* @return the number of vertices in the graph
94+
*/
5795
public int numberOfVertices() {
5896
return this._numberOfVertices;
5997
}
6098

99+
/**
100+
* Updates the number of edges in the graph
101+
*
102+
* @param newNumberOfEdges
103+
* */
61104
private void setNumberOfEdges(int newNumberOfEdges) {
62105
this._numberOfEdges = newNumberOfEdges;
63106
}
64107

108+
/**
109+
* Getter for `this._numberOfEdges`
110+
*
111+
* @return the number of edges
112+
*/
65113
public int numberOfEdges() {
66114
return this._numberOfEdges;
67115
}
68116

117+
/**
118+
* Sets a new matrix as the adjacency matrix
119+
*
120+
* @param newAdjacency the new adjaceny matrix
121+
*/
69122
private void setAdjacency(int[][] newAdjacency) {
70123
this._adjacency = newAdjacency;
71124
}
72125

126+
/**
127+
* Getter for the adjacency matrix
128+
*
129+
* @return the adjacency matrix
130+
*/
73131
private int[][] adjacency() {
74132
return this._adjacency;
75133
}
76134

135+
/**
136+
* Checks if two vertices are connected by an edge
137+
*
138+
* @param from the parent vertex to check for adjacency
139+
* @param to the child vertex to check for adjacency
140+
* @return whether or not the vertices are adjancent
141+
*/
77142
private boolean adjacencyOfEdgeDoesExist(int from, int to) {
78143
return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);
79144
}
80145

146+
/**
147+
* Checks if a particular vertex exists in a graph
148+
*
149+
* @param aVertex the vertex to check for existence
150+
* @return whether or not the vertex exists
151+
*/
81152
public boolean vertexDoesExist(int aVertex) {
82153
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
83154
return true;
@@ -86,6 +157,13 @@ public boolean vertexDoesExist(int aVertex) {
86157
}
87158
}
88159

160+
/**
161+
* Checks if two vertices are connected by an edge
162+
*
163+
* @param from the parent vertex to check for adjacency
164+
* @param to the child vertex to check for adjacency
165+
* @return whether or not the vertices are adjancent
166+
*/
89167
public boolean edgeDoesExist(int from, int to) {
90168
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
91169
return (this.adjacencyOfEdgeDoesExist(from, to));

DevUtils/Nodes/LargeTreeNode.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package DevUtils.Nodes;
2+
3+
import java.util.Collection;
4+
5+
/**
6+
* {@link TreeNode} extension that holds a {@link Collection}
7+
* of refrences to child Nodes.
8+
*
9+
* @param <E> The type of the data held in the Node.
10+
*
11+
* @author <a href="https://github.com/aitorfi">aitorfi</a>
12+
*/
13+
public class LargeTreeNode<E> extends TreeNode<E> {
14+
/** {@link Collection} that holds the Nodes' child nodes. */
15+
private Collection<LargeTreeNode<E>> childNodes;
16+
17+
/** Empty contructor. */
18+
public LargeTreeNode() {
19+
super();
20+
}
21+
22+
/**
23+
* Initializes the Nodes' data.
24+
*
25+
* @param data Value to which data will be initialized.
26+
* @see TreeNode#TreeNode(Object)
27+
*/
28+
public LargeTreeNode(E data) {
29+
super(data);
30+
}
31+
32+
/**
33+
* Initializes the Nodes' data and parent node reference.
34+
*
35+
* @param data Value to which data will be initialized.
36+
* @param parentNode Value to which the nodes' parent reference will be set.
37+
* @see TreeNode#TreeNode(Object, Node)
38+
*/
39+
public LargeTreeNode(E data, LargeTreeNode<E> parentNode) {
40+
super(data, parentNode);
41+
}
42+
43+
/**
44+
* Initializes the Nodes' data and parent and child nodes references.
45+
*
46+
* @param data Value to which data will be initialized.
47+
* @param parentNode Value to which the nodes' parent reference will be set.
48+
* @param childNodes {@link Collection} of child Nodes.
49+
* @see TreeNode#TreeNode(Object, Node)
50+
*/
51+
public LargeTreeNode(E data, LargeTreeNode<E> parentNode, Collection<LargeTreeNode<E>> childNodes) {
52+
super(data, parentNode);
53+
this.childNodes = childNodes;
54+
}
55+
56+
/**
57+
* @return True if the node is a leaf node, otherwise false.
58+
* @see TreeNode#isLeafNode()
59+
*/
60+
@Override
61+
public boolean isLeafNode() {
62+
return (childNodes == null || childNodes.size() == 0);
63+
}
64+
65+
public Collection<LargeTreeNode<E>> getChildNodes() {
66+
return childNodes;
67+
}
68+
69+
public void setChildNodes(Collection<LargeTreeNode<E>> childNodes) {
70+
this.childNodes = childNodes;
71+
}
72+
}

DevUtils/Nodes/Node.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package DevUtils.Nodes;
2+
3+
/**
4+
* Base class for any node implementation which
5+
* contains a generic type variable.
6+
*
7+
* All known subclasses: {@link TreeNode}, {@link SimpleNode}.
8+
*
9+
* @param <E> The type of the data held in the Node.
10+
*
11+
* @author <a href="https://github.com/aitorfi">aitorfi</a>
12+
*/
13+
public abstract class Node<E> {
14+
/** Generic type data stored in the Node. */
15+
private E data;
16+
17+
/** Empty constructor. */
18+
public Node() {}
19+
20+
/**
21+
* Initializes the Nodes' data.
22+
*
23+
* @param data Value to which data will be initialized.
24+
*/
25+
public Node(E data) {
26+
this.data = data;
27+
}
28+
29+
public E getData() {
30+
return data;
31+
}
32+
33+
public void setData(E data) {
34+
this.data = data;
35+
}
36+
}

DevUtils/Nodes/SimpleNode.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package DevUtils.Nodes;
2+
3+
/**
4+
* Simple Node implementation that holds
5+
* a reference to the next Node.
6+
*
7+
* @param <E> The type of the data held in the Node.
8+
*
9+
* @author <a href="https://github.com/aitorfi">aitorfi</a>
10+
*/
11+
public class SimpleNode<E> extends Node<E> {
12+
/** Reference to the next Node. */
13+
private SimpleNode<E> nextNode;
14+
15+
/** Empty contructor. */
16+
public SimpleNode() {
17+
super();
18+
}
19+
20+
/**
21+
* Initializes the Nodes' data.
22+
*
23+
* @param data Value to which data will be initialized.
24+
* @see Node#Node(Object)
25+
*/
26+
public SimpleNode(E data) {
27+
super(data);
28+
}
29+
30+
/**
31+
* Initializes the Nodes' data and next node reference.
32+
*
33+
* @param data Value to which data will be initialized.
34+
* @param nextNode Value to which the next node reference will be set.
35+
*/
36+
public SimpleNode(E data, SimpleNode<E> nextNode) {
37+
super(data);
38+
this.nextNode = nextNode;
39+
}
40+
41+
/**
42+
* @return True if there is a next node, otherwise false.
43+
*/
44+
public boolean hasNext() {
45+
return (nextNode != null);
46+
}
47+
48+
public SimpleNode<E> getNextNode() {
49+
return nextNode;
50+
}
51+
52+
public void setNextNode(SimpleNode<E> nextNode) {
53+
this.nextNode = nextNode;
54+
}
55+
}

DevUtils/Nodes/SimpleTreeNode.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package DevUtils.Nodes;
2+
3+
/**
4+
* Simple TreeNode extension that holds references
5+
* to two child Nodes (left and right).
6+
*
7+
* @param <E> The type of the data held in the Node.
8+
*
9+
* @author <a href="https://github.com/aitorfi">aitorfi</a>
10+
*/
11+
public class SimpleTreeNode<E> extends TreeNode<E> {
12+
/** Refrence to the child Node on the left. */
13+
private SimpleTreeNode<E> leftNode;
14+
/** Refrence to the child Node on the right. */
15+
private SimpleTreeNode<E> rightNode;
16+
17+
/** Empty contructor. */
18+
public SimpleTreeNode() {
19+
super();
20+
}
21+
22+
/**
23+
* Initializes the Nodes' data.
24+
*
25+
* @param data Value to which data will be initialized.
26+
* @see TreeNode#TreeNode(Object)
27+
*/
28+
public SimpleTreeNode(E data) {
29+
super(data);
30+
}
31+
32+
/**
33+
* Initializes the Nodes' data and parent node reference.
34+
*
35+
* @param data Value to which data will be initialized.
36+
* @param parentNode Value to which the nodes' parent reference will be set.
37+
* @see TreeNode#TreeNode(Object, Node)
38+
*/
39+
public SimpleTreeNode(E data, SimpleTreeNode<E> parentNode) {
40+
super(data, parentNode);
41+
}
42+
43+
/**
44+
* Initializes the Nodes' data and parent and child nodes references.
45+
*
46+
* @param data Value to which data will be initialized.
47+
* @param parentNode Value to which the nodes' parent reference will be set.
48+
* @param leftNode Value to which the nodes' left child reference will be set.
49+
* @param rightNode Value to which the nodes' right child reference will be set.
50+
*/
51+
public SimpleTreeNode(E data, SimpleTreeNode<E> parentNode, SimpleTreeNode<E> leftNode, SimpleTreeNode<E> rightNode) {
52+
super(data, parentNode);
53+
this.leftNode = leftNode;
54+
this.rightNode = rightNode;
55+
}
56+
57+
/**
58+
* @return True if the node is a leaf node, otherwise false.
59+
* @see TreeNode#isLeafNode()
60+
*/
61+
@Override
62+
public boolean isLeafNode() {
63+
return (leftNode == null && rightNode == null);
64+
}
65+
66+
public SimpleTreeNode<E> getLeftNode() {
67+
return leftNode;
68+
}
69+
70+
public void setLeftNode(SimpleTreeNode<E> leftNode) {
71+
this.leftNode = leftNode;
72+
}
73+
74+
public SimpleTreeNode<E> getRightNode() {
75+
return rightNode;
76+
}
77+
78+
public void setRightNode(SimpleTreeNode<E> rightNode) {
79+
this.rightNode = rightNode;
80+
}
81+
}

0 commit comments

Comments
 (0)