mirror of https://github.com/TheMessik/TreeLib
small reformat
parent
33b9f913dd
commit
5e3301bbfb
|
@ -1,12 +1,12 @@
|
||||||
package graph;
|
package abstractions;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class Node<T extends Comparable<T>> {
|
public abstract class Node<T extends Comparable<T>> {
|
||||||
|
protected final List<Node<T>> children;
|
||||||
protected T value;
|
protected T value;
|
||||||
protected Node<T> parent;
|
protected Node<T> parent;
|
||||||
protected final List<Node<T>> children;
|
|
||||||
|
|
||||||
public Node(Node<T> parent, T value) {
|
public Node(Node<T> parent, T value) {
|
||||||
this.value = value;
|
this.value = value;
|
|
@ -1,4 +1,4 @@
|
||||||
package graph;
|
package abstractions;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -28,8 +28,12 @@ public abstract class Tree<T extends Comparable<T>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Node<T> addNode(T value);
|
public abstract Node<T> addNode(T value);
|
||||||
|
|
||||||
public abstract boolean removeNode(T value);
|
public abstract boolean removeNode(T value);
|
||||||
public abstract boolean removeNode (Node<T> n);
|
|
||||||
|
public abstract boolean removeNode(Node<T> n);
|
||||||
|
|
||||||
public abstract Node<T> findNode(T value);
|
public abstract Node<T> findNode(T value);
|
||||||
|
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package binarytree;
|
package binarytree;
|
||||||
|
|
||||||
import graph.Node;
|
import abstractions.Node;
|
||||||
import graph.Tree;
|
import abstractions.Tree;
|
||||||
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -68,7 +68,7 @@ public class BinaryTree<T extends Comparable<T>> extends Tree<T> {
|
||||||
this.values.remove(value);
|
this.values.remove(value);
|
||||||
this.nodes.remove(node);
|
this.nodes.remove(node);
|
||||||
|
|
||||||
if(node.getParent() != null) {
|
if (node.getParent() != null) {
|
||||||
node.getParent().getChildren().remove(node);
|
node.getParent().getChildren().remove(node);
|
||||||
this.nodes.remove(node);
|
this.nodes.remove(node);
|
||||||
} else { // if the value had no parent, it was the root node
|
} else { // if the value had no parent, it was the root node
|
||||||
|
@ -82,7 +82,7 @@ public class BinaryTree<T extends Comparable<T>> extends Tree<T> {
|
||||||
// it could happen that we have removed the root node and set the first child to be the new root
|
// 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
|
// if so, only reintroduce the other children, omitting the first one
|
||||||
int start = 0;
|
int start = 0;
|
||||||
if(node.getParent() == null) {
|
if (node.getParent() == null) {
|
||||||
start = 1;
|
start = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,12 +154,12 @@ public class BinaryTree<T extends Comparable<T>> extends Tree<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if(nodes.size() == 0) {
|
if (nodes.size() == 0) {
|
||||||
return "Empty";
|
return "Empty";
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for(Node<T> n : nodes) {
|
for (Node<T> n : nodes) {
|
||||||
sb.append(n.toString());
|
sb.append(n.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package binarytree;
|
package binarytree;
|
||||||
|
|
||||||
import graph.Node;
|
import abstractions.Node;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ public class BinaryTreeNode<T extends Comparable<T>> extends Node<T> {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(String.format("BT(%s)", value));
|
sb.append(String.format("BT(%s)", value));
|
||||||
|
|
||||||
for(Node<T> child : children) {
|
for (Node<T> child : children) {
|
||||||
sb.append(":");
|
sb.append(":");
|
||||||
sb.append(child.toString());
|
sb.append(child.toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import binarytree.BinaryTree;
|
import binarytree.BinaryTree;
|
||||||
import binarytree.BinaryTreeNode;
|
import binarytree.BinaryTreeNode;
|
||||||
import graph.Node;
|
import abstractions.Node;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -36,11 +36,6 @@ public class BinaryTreeTests {
|
||||||
Assert.assertEquals(4, tree.getSize());
|
Assert.assertEquals(4, tree.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAddition3() {
|
|
||||||
Node<Integer> n1 = new BinaryTreeNode<>(null, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemoval() {
|
public void testRemoval() {
|
||||||
tree.addNode(1);
|
tree.addNode(1);
|
||||||
|
|
Loading…
Reference in New Issue