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) {
// check if it is not our own message
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;
Faction enemyFaction = teamData.getFaction();
if (this.chosenFaction == null) {

View File

@@ -1,115 +1,112 @@
package netwerkprog.game.util.tree;
import java.util.ArrayList;
public class BST<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> root;
protected int size = 0;
// Helper methode
public int sum () {
public int sum() {
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
// 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...
public int sum(TreeNode<E> node) {
if (node == null) {
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);
}
// Helper methode
public int totalLeaves () {
public int totalLeaves() {
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
// implementeren. Het mag wel.
public int totalLeaves ( TreeNode<E> node ) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
return 1;
}
return totalLeaves(node.left) + totalLeaves(node.right);
public int totalLeaves(TreeNode<E> node) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
return 1;
}
return totalLeaves(node.left) + totalLeaves(node.right);
}
/** Create a default binary tree */
/**
* Create a default binary tree
*/
public BST() {
}
/** Create a binary tree from an array of objects */
/**
* Create a binary tree from an array of objects
*/
public BST(E[] objects) {
for (int i = 0; i < objects.length; i++)
insert(objects[i]);
for (E object : objects) insert(object);
}
@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) {
return search(e, root);
}
private boolean search(E e, TreeNode<E> tree)
{
private boolean search(E e, TreeNode<E> tree) {
// nog niet correct
if (tree == null)
{
if (tree == null) {
return false;
}
if (e.compareTo(tree.element) == 0)
{
if (e.compareTo(tree.element) == 0) {
return true;
}
if (e.compareTo(tree.element) < 0)
{
if (e.compareTo(tree.element) < 0) {
return search(e, tree.left);
}
else // (e.compareTo(tree.element) > 0)
} else // (e.compareTo(tree.element) > 0)
{
return search(e, tree.right);
}
}
@Override /** Insert element o into the binary tree
* Return true if the element is inserted successfully */
public boolean insert(E e) {
/**
* Insert element o into the binary tree
* Return true if the element is inserted successfully
*/
@Override
public void insert(E e) {
if (root == null) {
root = createNewNode(e); // Create a new root
size++;
return true;
}
else {
return insert(e, root);
} else {
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
pre: root != null
* pre: root != null
*/
public boolean insert(E e, TreeNode<E> tree) {
if (e.compareTo(tree.element) == 0) {
return false; // Duplicate node not inserted
}
else if (e.compareTo(tree.element) < 0 && tree.left != null)
return insert(e, tree.left);
} else if (e.compareTo(tree.element) < 0 && tree.left != null)
return insert(e, tree.left);
else if (e.compareTo(tree.element) > 0 && tree.right != null)
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 {
if (e.compareTo(tree.element) < 0) {
tree.left = createNewNode(e);
}
else {
} else {
tree.right = createNewNode(e);
}
size++;
@@ -119,15 +116,20 @@ public class BST<E extends Comparable<E>> extends AbstractTree<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() {
inorder(root);
}
/** Inorder traversal from a subtree */
/**
* Inorder traversal from a subtree
*/
protected void inorder(TreeNode<E> root) {
if (root == null) return;
inorder(root.left);
@@ -135,12 +137,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
inorder(root.right);
}
@Override /** Postorder traversal from the root */
/**
* Post order traversal from the root
*/
@Override
public void postorder() {
postorder(root);
}
/** Postorder traversal from a subtree */
/**
* Post order traversal from a subtree
*/
protected void postorder(TreeNode<E> root) {
if (root == null) return;
postorder(root.left);
@@ -148,12 +155,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
System.out.print(root.element + " ");
}
@Override /** Preorder traversal from the root */
/**
* Preorder traversal from the root
*/
@Override
public void preorder() {
preorder(root);
}
/** Preorder traversal from a subtree */
/**
* Preorder traversal from a subtree
*/
protected void preorder(TreeNode<E> root) {
if (root == null) return;
System.out.print(root.element + " ");
@@ -161,8 +173,10 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
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>> {
protected E element;
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() {
return size;
}
/** Returns the root of the tree */
/**
* Returns the root of the tree
*/
public TreeNode<E> getRoot() {
return root;
}
/** Returns a path from the root leading to the specified element */
public java.util.ArrayList<TreeNode<E>> path(E e) {
java.util.ArrayList<TreeNode<E>> list =
new java.util.ArrayList<TreeNode<E>>();
/**
* Returns a path from the root leading to the specified element
*/
public ArrayList<TreeNode<E>> path(E e) {
ArrayList<TreeNode<E>> list =
new ArrayList<>();
TreeNode<E> current = root; // Start from the root
while (current != null) {
list.add(current); // Add the node to the list
if (e.compareTo(current.element) < 0) {
current = current.left;
}
else if (e.compareTo(current.element) > 0) {
} else if (e.compareTo(current.element) > 0) {
current = current.right;
}
else
} else
break;
}
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 false if the element is not in the tree */
public boolean delete(E e) {
* Return false if the element is not in the tree
*/
@Override
public void delete(E e) {
// Locate the node to be deleted and also locate its parent node
TreeNode<E> parent = null;
TreeNode<E> current = root;
@@ -215,32 +237,28 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
if (e.compareTo(current.element) < 0) {
parent = current;
current = current.left;
}
else if (e.compareTo(current.element) > 0) {
} else if (e.compareTo(current.element) > 0) {
parent = current;
current = current.right;
}
else
} else
break; // Element is in the tree pointed at by current
}
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
if (current.left == null) {
// Connect the parent with the right child of the current node
if (parent == null) {
root = current.right;
}
else {
} else {
if (e.compareTo(parent.element) < 0)
parent.left = current.right;
else
parent.right = current.right;
}
}
else {
} else {
// Case 2: The current node has a left child
// Locate the rightmost node in the left subtree of
// 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)
parentOfRightMost.right = rightMost.left;
else
// Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left;
// Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left;
}
size--;
return true; // Element deleted successfully
}
@Override /** Obtain an iterator. Use inorder. */
/**
* Obtain an iterator. Use inorder.
*/
@Override
public java.util.Iterator<E> iterator() {
return new InorderIterator();
}
@@ -275,41 +295,51 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
// Inner class InorderIterator
private class InorderIterator implements java.util.Iterator<E> {
// Store the elements in a list
private java.util.ArrayList<E> list =
new java.util.ArrayList<E>();
private final java.util.ArrayList<E> list =
new java.util.ArrayList<>();
private int current = 0; // Point to the current element in list
public InorderIterator() {
inorder(); // Traverse binary tree and store elements in list
}
/** Inorder traversal from the root*/
/**
* Inorder traversal from the root
*/
private void inorder() {
inorder(root);
}
/** Inorder traversal from a subtree */
/**
* Inorder traversal from a subtree
*/
private void inorder(TreeNode<E> root) {
if (root == null)return;
if (root == null) return;
inorder(root.left);
list.add(root.element);
inorder(root.right);
}
@Override /** More elements for traversing? */
/**
* More elements for traversing?
*/
@Override
public boolean hasNext() {
if (current < list.size())
return true;
return false;
return current < list.size();
}
@Override /** Get the current element and move to the next */
/**
* Get the current element and move to the next
*/
@Override
public E next() {
return list.get(current++);
}
@Override /** Remove the current element */
/**
* Remove the current element
*/
@Override
public void remove() {
delete(list.get(current)); // Delete the current element
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() {
root = null;
size = 0;
@@ -325,9 +357,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
@Override
public String toString() {
String res = "";
StringBuilder res = new StringBuilder();
for (E e : this) {
res += e.toString();
res.append(e.toString());
}
return "BST{" +
"root=" + root +
@@ -335,18 +367,4 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
", " + 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
* 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
* Return true if the element is deleted successfully */
public boolean delete(E e);
public void delete(E e);
/** Inorder traversal from the root*/
public void inorder();