clean bst

This commit is contained in:
Sem van der Hoeven
2020-06-07 17:10:36 +02:00
parent 37cefd6d88
commit 749429b171
3 changed files with 138 additions and 120 deletions

View File

@@ -393,7 +393,7 @@ public class MainGame extends Game implements ClientCallback {
} else if (data instanceof TeamData) { } else if (data instanceof TeamData) {
// check if it is not our own message // check if it is not our own message
if (!((TeamData) data).getUsername().equals(this.username)) { if (!((TeamData) data).getUsername().equals(this.username)) {
// if we have already chosen a faction, so we were first // if we have not yet chosen a faction, select the opposing faction
TeamData teamData = (TeamData) data; TeamData teamData = (TeamData) data;
Faction enemyFaction = teamData.getFaction(); Faction enemyFaction = teamData.getFaction();
if (this.chosenFaction == null) { if (this.chosenFaction == null) {

View File

@@ -1,115 +1,112 @@
package netwerkprog.game.util.tree; package netwerkprog.game.util.tree;
import java.util.ArrayList;
public class BST<E extends Comparable<E>> extends AbstractTree<E> { public class BST<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> root; protected TreeNode<E> root;
protected int size = 0; protected int size = 0;
// Helper methode
public int sum () { public int sum() {
return this.sum(this.getRoot()); return this.sum(this.getRoot());
} }
// Opgave 1b (10 punten): Maak de recursieve methode sum af in de klasse bst.BST. Deze methode telt de getallen public int sum(TreeNode<E> node) {
// van alle elementen van de binaire zoekboom bij elkaar op. De methode geeft de totale som terug van alle getallen
// in de boom.
public int sum( TreeNode<E> node ) {
// Schrijf hier jouw code...
if (node == null) { if (node == null) {
return 0; return 0;
} }
int nodeValue = (Integer) node.element; // Tip, omdat E nog onbekend is doen we het zo (niet helemaal netjes) int nodeValue = (Integer) node.element;
return sum(node.left) + sum(node.right); return sum(node.left) + sum(node.right);
} }
// Helper methode
public int totalLeaves () { public int totalLeaves() {
return this.totalLeaves(this.getRoot()); return this.totalLeaves(this.getRoot());
} }
// Opgave 1c (10 punten): Maak de methode totalLeaves af om de klasse bst.BST. Deze methode telt het aantal
// bladeren (leaves) van de gegeven binaire zoekboom en geeft deze terug. Je hoeft deze methode niet recursief te public int totalLeaves(TreeNode<E> node) {
// implementeren. Het mag wel. if (node == null) {
public int totalLeaves ( TreeNode<E> node ) { return 0;
if (node == null) { }
return 0; if (node.left == null && node.right == null) {
} return 1;
if (node.left == null && node.right == null) { }
return 1; return totalLeaves(node.left) + totalLeaves(node.right);
}
return totalLeaves(node.left) + totalLeaves(node.right);
} }
/** Create a default binary tree */ /**
* Create a default binary tree
*/
public BST() { public BST() {
} }
/** Create a binary tree from an array of objects */ /**
* Create a binary tree from an array of objects
*/
public BST(E[] objects) { public BST(E[] objects) {
for (int i = 0; i < objects.length; i++) for (E object : objects) insert(object);
insert(objects[i]);
} }
@Override /** Returns true if the element is in the tree */ /**
* Returns true if the element is in the tree
*/
@Override
public boolean search(E e) { public boolean search(E e) {
return search(e, root); return search(e, root);
} }
private boolean search(E e, TreeNode<E> tree) private boolean search(E e, TreeNode<E> tree) {
{
// nog niet correct // nog niet correct
if (tree == null) if (tree == null) {
{
return false; return false;
} }
if (e.compareTo(tree.element) == 0) if (e.compareTo(tree.element) == 0) {
{
return true; return true;
} }
if (e.compareTo(tree.element) < 0) if (e.compareTo(tree.element) < 0) {
{
return search(e, tree.left); return search(e, tree.left);
} } else // (e.compareTo(tree.element) > 0)
else // (e.compareTo(tree.element) > 0)
{ {
return search(e, tree.right); return search(e, tree.right);
} }
} }
@Override /** Insert element o into the binary tree /**
* Return true if the element is inserted successfully */ * Insert element o into the binary tree
public boolean insert(E e) { * Return true if the element is inserted successfully
*/
@Override
public void insert(E e) {
if (root == null) { if (root == null) {
root = createNewNode(e); // Create a new root root = createNewNode(e); // Create a new root
size++; size++;
return true; } else {
} insert(e, root);
else {
return insert(e, root);
} }
} }
/** Insert element o into the binary tree /**
* Insert element o into the binary tree
* Return true if the element is inserted successfully * Return true if the element is inserted successfully
pre: root != null * pre: root != null
*/ */
public boolean insert(E e, TreeNode<E> tree) { public boolean insert(E e, TreeNode<E> tree) {
if (e.compareTo(tree.element) == 0) { if (e.compareTo(tree.element) == 0) {
return false; // Duplicate node not inserted return false; // Duplicate node not inserted
} } else if (e.compareTo(tree.element) < 0 && tree.left != null)
else if (e.compareTo(tree.element) < 0 && tree.left != null) return insert(e, tree.left);
return insert(e, tree.left);
else if (e.compareTo(tree.element) > 0 && tree.right != null) else if (e.compareTo(tree.element) > 0 && tree.right != null)
return insert(e, tree.right); return insert(e, tree.right);
// Create the new node and attach it to the parent node // Create the new node and attach it to the parent node
else { else {
if (e.compareTo(tree.element) < 0) { if (e.compareTo(tree.element) < 0) {
tree.left = createNewNode(e); tree.left = createNewNode(e);
} } else {
else {
tree.right = createNewNode(e); tree.right = createNewNode(e);
} }
size++; size++;
@@ -119,15 +116,20 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> createNewNode(E e) { protected TreeNode<E> createNewNode(E e) {
return new TreeNode<E>(e); return new TreeNode<>(e);
} }
@Override /** Inorder traversal from the root*/ /**
* Inorder traversal from the root
*/
@Override
public void inorder() { public void inorder() {
inorder(root); inorder(root);
} }
/** Inorder traversal from a subtree */ /**
* Inorder traversal from a subtree
*/
protected void inorder(TreeNode<E> root) { protected void inorder(TreeNode<E> root) {
if (root == null) return; if (root == null) return;
inorder(root.left); inorder(root.left);
@@ -135,12 +137,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
inorder(root.right); inorder(root.right);
} }
@Override /** Postorder traversal from the root */ /**
* Post order traversal from the root
*/
@Override
public void postorder() { public void postorder() {
postorder(root); postorder(root);
} }
/** Postorder traversal from a subtree */ /**
* Post order traversal from a subtree
*/
protected void postorder(TreeNode<E> root) { protected void postorder(TreeNode<E> root) {
if (root == null) return; if (root == null) return;
postorder(root.left); postorder(root.left);
@@ -148,12 +155,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
System.out.print(root.element + " "); System.out.print(root.element + " ");
} }
@Override /** Preorder traversal from the root */ /**
* Preorder traversal from the root
*/
@Override
public void preorder() { public void preorder() {
preorder(root); preorder(root);
} }
/** Preorder traversal from a subtree */ /**
* Preorder traversal from a subtree
*/
protected void preorder(TreeNode<E> root) { protected void preorder(TreeNode<E> root) {
if (root == null) return; if (root == null) return;
System.out.print(root.element + " "); System.out.print(root.element + " ");
@@ -161,8 +173,10 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
preorder(root.right); preorder(root.right);
} }
/** This inner class is static, because it does not access /**
any instance members defined in its outer class */ * This inner class is static, because it does not access
* any instance members defined in its outer class
*/
public static class TreeNode<E extends Comparable<E>> { public static class TreeNode<E extends Comparable<E>> {
protected E element; protected E element;
protected TreeNode<E> left; protected TreeNode<E> left;
@@ -173,41 +187,49 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
} }
} }
@Override /** Get the number of nodes in the tree */ /**
* Get the number of nodes in the tree
*/
@Override
public int getSize() { public int getSize() {
return size; return size;
} }
/** Returns the root of the tree */ /**
* Returns the root of the tree
*/
public TreeNode<E> getRoot() { public TreeNode<E> getRoot() {
return root; return root;
} }
/** Returns a path from the root leading to the specified element */ /**
public java.util.ArrayList<TreeNode<E>> path(E e) { * Returns a path from the root leading to the specified element
java.util.ArrayList<TreeNode<E>> list = */
new java.util.ArrayList<TreeNode<E>>(); public ArrayList<TreeNode<E>> path(E e) {
ArrayList<TreeNode<E>> list =
new ArrayList<>();
TreeNode<E> current = root; // Start from the root TreeNode<E> current = root; // Start from the root
while (current != null) { while (current != null) {
list.add(current); // Add the node to the list list.add(current); // Add the node to the list
if (e.compareTo(current.element) < 0) { if (e.compareTo(current.element) < 0) {
current = current.left; current = current.left;
} } else if (e.compareTo(current.element) > 0) {
else if (e.compareTo(current.element) > 0) {
current = current.right; current = current.right;
} } else
else
break; break;
} }
return list; // Return an array list of nodes return list; // Return an array list of nodes
} }
@Override /** Delete an element from the binary tree. /**
* Delete an element from the binary tree.
* Return true if the element is deleted successfully * Return true if the element is deleted successfully
* Return false if the element is not in the tree */ * Return false if the element is not in the tree
public boolean delete(E e) { */
@Override
public void delete(E e) {
// Locate the node to be deleted and also locate its parent node // Locate the node to be deleted and also locate its parent node
TreeNode<E> parent = null; TreeNode<E> parent = null;
TreeNode<E> current = root; TreeNode<E> current = root;
@@ -215,32 +237,28 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
if (e.compareTo(current.element) < 0) { if (e.compareTo(current.element) < 0) {
parent = current; parent = current;
current = current.left; current = current.left;
} } else if (e.compareTo(current.element) > 0) {
else if (e.compareTo(current.element) > 0) {
parent = current; parent = current;
current = current.right; current = current.right;
} } else
else
break; // Element is in the tree pointed at by current break; // Element is in the tree pointed at by current
} }
if (current == null) if (current == null)
return false; // Element is not in the tree return; // Element is not in the tree
// Case 1: current has no left child // Case 1: current has no left child
if (current.left == null) { if (current.left == null) {
// Connect the parent with the right child of the current node // Connect the parent with the right child of the current node
if (parent == null) { if (parent == null) {
root = current.right; root = current.right;
} } else {
else {
if (e.compareTo(parent.element) < 0) if (e.compareTo(parent.element) < 0)
parent.left = current.right; parent.left = current.right;
else else
parent.right = current.right; parent.right = current.right;
} }
} } else {
else {
// Case 2: The current node has a left child // Case 2: The current node has a left child
// Locate the rightmost node in the left subtree of // Locate the rightmost node in the left subtree of
// the current node and also its parent // the current node and also its parent
@@ -259,15 +277,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
if (parentOfRightMost.right == rightMost) if (parentOfRightMost.right == rightMost)
parentOfRightMost.right = rightMost.left; parentOfRightMost.right = rightMost.left;
else else
// Special case: parentOfRightMost == current // Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left; parentOfRightMost.left = rightMost.left;
} }
size--; size--;
return true; // Element deleted successfully
} }
@Override /** Obtain an iterator. Use inorder. */ /**
* Obtain an iterator. Use inorder.
*/
@Override
public java.util.Iterator<E> iterator() { public java.util.Iterator<E> iterator() {
return new InorderIterator(); return new InorderIterator();
} }
@@ -275,41 +295,51 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
// Inner class InorderIterator // Inner class InorderIterator
private class InorderIterator implements java.util.Iterator<E> { private class InorderIterator implements java.util.Iterator<E> {
// Store the elements in a list // Store the elements in a list
private java.util.ArrayList<E> list = private final java.util.ArrayList<E> list =
new java.util.ArrayList<E>(); new java.util.ArrayList<>();
private int current = 0; // Point to the current element in list private int current = 0; // Point to the current element in list
public InorderIterator() { public InorderIterator() {
inorder(); // Traverse binary tree and store elements in list inorder(); // Traverse binary tree and store elements in list
} }
/** Inorder traversal from the root*/ /**
* Inorder traversal from the root
*/
private void inorder() { private void inorder() {
inorder(root); inorder(root);
} }
/** Inorder traversal from a subtree */ /**
* Inorder traversal from a subtree
*/
private void inorder(TreeNode<E> root) { private void inorder(TreeNode<E> root) {
if (root == null)return; if (root == null) return;
inorder(root.left); inorder(root.left);
list.add(root.element); list.add(root.element);
inorder(root.right); inorder(root.right);
} }
@Override /** More elements for traversing? */ /**
* More elements for traversing?
*/
@Override
public boolean hasNext() { public boolean hasNext() {
if (current < list.size()) return current < list.size();
return true;
return false;
} }
@Override /** Get the current element and move to the next */ /**
* Get the current element and move to the next
*/
@Override
public E next() { public E next() {
return list.get(current++); return list.get(current++);
} }
@Override /** Remove the current element */ /**
* Remove the current element
*/
@Override
public void remove() { public void remove() {
delete(list.get(current)); // Delete the current element delete(list.get(current)); // Delete the current element
list.clear(); // Clear the list list.clear(); // Clear the list
@@ -317,7 +347,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
} }
} }
/** Remove all elements from the tree */ /**
* Remove all elements from the tree
*/
public void clear() { public void clear() {
root = null; root = null;
size = 0; size = 0;
@@ -325,9 +357,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
@Override @Override
public String toString() { public String toString() {
String res = ""; StringBuilder res = new StringBuilder();
for (E e : this) { for (E e : this) {
res += e.toString(); res.append(e.toString());
} }
return "BST{" + return "BST{" +
"root=" + root + "root=" + root +
@@ -335,18 +367,4 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
", " + res + ", " + res +
'}'; '}';
} }
// if (tree == null) {
// return false;
// }
// else if (e.compareTo(tree.element) > 0) {
// return search(e, tree.right);
// }
// else if (e.compareTo(tree.element) < 0) {
// return search(e, tree.left);
// }
// else {
// return true;
// }
//
} }

View File

@@ -6,11 +6,11 @@ public interface Tree<E> extends Iterable<E> {
/** Insert element o into the binary tree /** Insert element o into the binary tree
* Return true if the element is inserted successfully */ * Return true if the element is inserted successfully */
public boolean insert(E e); public void insert(E e);
/** Delete the specified element from the tree /** Delete the specified element from the tree
* Return true if the element is deleted successfully */ * Return true if the element is deleted successfully */
public boolean delete(E e); public void delete(E e);
/** Inorder traversal from the root*/ /** Inorder traversal from the root*/
public void inorder(); public void inorder();