Permalink
Browse files

Added parent property to Generic Tree Node. Change find and exists to…

… use data instead of node.
  • Loading branch information...
1 parent 6dbebaf commit 15c59c992355cbb56ddbae1f825839c8d5bf3bb8 @vivin committed May 25, 2011
@@ -43,32 +43,32 @@ private int auxiliaryGetNumberOfNodes(GenericTreeNode<T> node) {
return numberOfNodes;
}
- public boolean exists(GenericTreeNode<T> nodeToFind) {
- return (find(nodeToFind) != null);
+ public boolean exists(T dataToFind) {
+ return (find(dataToFind) != null);
}
- public GenericTreeNode<T> find(GenericTreeNode<T> nodeToFind) {
+ public GenericTreeNode<T> find(T dataToFind) {
GenericTreeNode<T> returnNode = null;
if(root != null) {
- returnNode = auxiliaryFind(root, nodeToFind);
+ returnNode = auxiliaryFind(root, dataToFind);
}
return returnNode;
}
- private GenericTreeNode<T> auxiliaryFind(GenericTreeNode<T> currentNode, GenericTreeNode<T> nodeToFind) {
+ private GenericTreeNode<T> auxiliaryFind(GenericTreeNode<T> currentNode, T dataToFind) {
GenericTreeNode<T> returnNode = null;
int i = 0;
- if (currentNode.equals(nodeToFind)) {
+ if (currentNode.getData().equals(dataToFind)) {
returnNode = currentNode;
}
else if(currentNode.hasChildren()) {
i = 0;
while(returnNode == null && i < currentNode.getNumberOfChildren()) {
- returnNode = auxiliaryFind(currentNode.getChildAt(i), nodeToFind);
+ returnNode = auxiliaryFind(currentNode.getChildAt(i), dataToFind);
i++;
}
}
@@ -12,8 +12,9 @@
public class GenericTreeNode<T> {
- public T data;
- public List<GenericTreeNode<T>> children;
+ private T data;
+ private List<GenericTreeNode<T>> children;
+ private GenericTreeNode<T> parent;
public GenericTreeNode() {
super();
@@ -25,6 +26,10 @@ public GenericTreeNode(T data) {
setData(data);
}
+ public GenericTreeNode<T> getParent() {
+ return this.parent;
+ }
+
public List<GenericTreeNode<T>> getChildren() {
return this.children;
}
@@ -38,14 +43,20 @@ public boolean hasChildren() {
}
public void setChildren(List<GenericTreeNode<T>> children) {
+ for(GenericTreeNode<T> child : children) {
+ child.parent = this;
+ }
+
this.children = children;
}
public void addChild(GenericTreeNode<T> child) {
+ child.parent = this;
children.add(child);
}
public void addChildAt(int index, GenericTreeNode<T> child) throws IndexOutOfBoundsException {
+ child.parent = this;
children.add(index, child);
}
@@ -73,38 +84,38 @@ public String toString() {
return getData().toString();
}
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- GenericTreeNode<?> other = (GenericTreeNode<?>) obj;
- if (data == null) {
- if (other.data != null) {
- return false;
- }
- } else if (!data.equals(other.data)) {
- return false;
- }
- return true;
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((data == null) ? 0 : data.hashCode());
- return result;
- }
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ GenericTreeNode<?> other = (GenericTreeNode<?>) obj;
+ if (data == null) {
+ if (other.data != null) {
+ return false;
+ }
+ } else if (!data.equals(other.data)) {
+ return false;
+ }
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((data == null) ? 0 : data.hashCode());
+ return result;
+ }
public String toStringVerbose() {
String stringRepresentation = getData().toString() + ":[";
View
@@ -3,6 +3,8 @@
Distributed under the BSD License
*/
+package net.vivin;
+
import org.testng.annotations.Test;
import java.util.*;
import static org.testng.Assert.*;
@@ -29,17 +31,17 @@ public void TestIsEmptyIsTrueOnNewTreeCreation() {
@Test
void TestExistsIsFalseOnNewTreeCreation() {
GenericTree<String> tree = new GenericTree<String>();
- GenericTreeNode<String> nodeToFind = new GenericTreeNode<String>();
+ String dataToFind = "";
- assertFalse(tree.exists(nodeToFind));
+ assertFalse(tree.exists(dataToFind));
}
@Test
void TestFindReturnsNullOnNewTreeCreation() {
GenericTree<String> tree = new GenericTree<String>();
- GenericTreeNode<String> nodeToFind = new GenericTreeNode<String>();
+ String dataToFind = "";
- assertNull(tree.find(nodeToFind));
+ assertNull(tree.find(dataToFind));
}
@Test
@@ -190,9 +192,9 @@ void TestExistsReturnsTrue() {
tree.setRoot(rootA);
- GenericTreeNode<String> nodeToFindD = new GenericTreeNode<String>("D");
+ String dataToFindD = "D";
- assertTrue(tree.exists(nodeToFindD));
+ assertTrue(tree.exists(dataToFindD));
}
@Test
@@ -210,9 +212,9 @@ void TestFindReturnsNonNull() {
tree.setRoot(rootA);
- GenericTreeNode<String> nodeToFindD = new GenericTreeNode<String>("D");
+ String dataToFindD = "D";
- assertNotNull(tree.find(nodeToFindD));
+ assertNotNull(tree.find(dataToFindD));
}
@Test
@@ -230,9 +232,9 @@ void TestExistsReturnsFalse() {
tree.setRoot(rootA);
- GenericTreeNode<String> nodeToFindE = new GenericTreeNode<String>("E");
+ String dataToFindE = "E";
- assertFalse(tree.exists(nodeToFindE));
+ assertFalse(tree.exists(dataToFindE));
}
@Test
@@ -250,9 +252,9 @@ void TestFindReturnsNull() {
tree.setRoot(rootA);
- GenericTreeNode<String> nodeToFindE = new GenericTreeNode<String>("E");
+ String dataToFindE = "E";
- assertNull(tree.find(nodeToFindE));
+ assertNull(tree.find(dataToFindE));
}
// Pre-order traversal will give us A B C D
@@ -62,6 +62,22 @@ public void TestNodeSetAndGetChildren() {
assertEquals(node.getChildren(), children);
}
+ @Test
+ public void TestNodeSetAndGetChildrenHasCorrectParent() {
+ GenericTreeNode<String> node = new GenericTreeNode<String>();
+ GenericTreeNode<String> child = new GenericTreeNode<String>();
+
+ List<GenericTreeNode<String>> children = new ArrayList<GenericTreeNode<String>>();
+ children.add(child);
+
+ node.setChildren(children);
+ assertEquals(node.getChildren(), children);
+
+ for(GenericTreeNode<String> childNode : children) {
+ assertEquals(node, childNode.getParent());
+ }
+ }
+
@Test
public void TestNodeRemoveChildren() {
GenericTreeNode<String> node = new GenericTreeNode<String>();
@@ -75,6 +91,15 @@ public void TestNodeRemoveChildren() {
assertEquals(node.getChildren().size(), 0);
}
+ @Test
+ public void TestNodeAddChildHasCorrectParent() {
+ GenericTreeNode<String> node = new GenericTreeNode<String>();
+ GenericTreeNode<String> child = new GenericTreeNode<String>();
+
+ node.addChild(child);
+ assertEquals(node, child.getParent());
+ }
+
@Test
public void TestNodeAddChildHasOneChild() {
GenericTreeNode<String> node = new GenericTreeNode<String>();
@@ -91,7 +116,6 @@ public void TestNodeAddChildHasChildrenIsTrue() {
node.addChild(child);
assertTrue(node.hasChildren());
-
}
@Test

0 comments on commit 15c59c9

Please sign in to comment.