diff --git a/src/graph/Node.java b/src/abstractions/Node.java similarity index 97% rename from src/graph/Node.java rename to src/abstractions/Node.java index c6bb444..7d70ef4 100644 --- a/src/graph/Node.java +++ b/src/abstractions/Node.java @@ -1,12 +1,12 @@ -package graph; +package abstractions; import java.util.ArrayList; import java.util.List; public abstract class Node> { + protected final List> children; protected T value; protected Node parent; - protected final List> children; public Node(Node parent, T value) { this.value = value; diff --git a/src/graph/Tree.java b/src/abstractions/Tree.java similarity index 90% rename from src/graph/Tree.java rename to src/abstractions/Tree.java index 9630a08..e6178e5 100644 --- a/src/graph/Tree.java +++ b/src/abstractions/Tree.java @@ -1,4 +1,4 @@ -package graph; +package abstractions; import java.util.ArrayList; import java.util.HashSet; @@ -28,8 +28,12 @@ public abstract class Tree> { } public abstract Node addNode(T value); + public abstract boolean removeNode(T value); - public abstract boolean removeNode (Node n); + + public abstract boolean removeNode(Node n); + public abstract Node findNode(T value); + public abstract String toString(); } diff --git a/src/binarytree/BinaryTree.java b/src/binarytree/BinaryTree.java index 41d0b60..61d57eb 100644 --- a/src/binarytree/BinaryTree.java +++ b/src/binarytree/BinaryTree.java @@ -1,7 +1,7 @@ package binarytree; -import graph.Node; -import graph.Tree; +import abstractions.Node; +import abstractions.Tree; import java.util.ArrayDeque; import java.util.ArrayList; @@ -68,7 +68,7 @@ public class BinaryTree> extends Tree { this.values.remove(value); this.nodes.remove(node); - if(node.getParent() != null) { + if (node.getParent() != null) { node.getParent().getChildren().remove(node); this.nodes.remove(node); } else { // if the value had no parent, it was the root node @@ -82,7 +82,7 @@ public class BinaryTree> extends Tree { // it could happen that we have removed the root node and set the first child to be the new root // if so, only reintroduce the other children, omitting the first one int start = 0; - if(node.getParent() == null) { + if (node.getParent() == null) { start = 1; } @@ -154,12 +154,12 @@ public class BinaryTree> extends Tree { @Override public String toString() { - if(nodes.size() == 0) { + if (nodes.size() == 0) { return "Empty"; } StringBuilder sb = new StringBuilder(); - for(Node n : nodes) { + for (Node n : nodes) { sb.append(n.toString()); } diff --git a/src/binarytree/BinaryTreeNode.java b/src/binarytree/BinaryTreeNode.java index a1eb26e..518f1d8 100644 --- a/src/binarytree/BinaryTreeNode.java +++ b/src/binarytree/BinaryTreeNode.java @@ -1,6 +1,6 @@ package binarytree; -import graph.Node; +import abstractions.Node; import java.util.Random; @@ -68,7 +68,7 @@ public class BinaryTreeNode> extends Node { StringBuilder sb = new StringBuilder(); sb.append(String.format("BT(%s)", value)); - for(Node child : children) { + for (Node child : children) { sb.append(":"); sb.append(child.toString()); } diff --git a/tests/BinaryTreeTests.java b/tests/BinaryTreeTests.java index b36be50..2267734 100644 --- a/tests/BinaryTreeTests.java +++ b/tests/BinaryTreeTests.java @@ -1,6 +1,6 @@ import binarytree.BinaryTree; import binarytree.BinaryTreeNode; -import graph.Node; +import abstractions.Node; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -36,11 +36,6 @@ public class BinaryTreeTests { Assert.assertEquals(4, tree.getSize()); } - @Test - public void testAddition3() { - Node n1 = new BinaryTreeNode<>(null, 1); - } - @Test public void testRemoval() { tree.addNode(1);