From 749429b171439b77e024b3dca7f3d59079d78e7d Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Sun, 7 Jun 2020 17:10:36 +0200 Subject: [PATCH] clean bst --- .../src/netwerkprog/game/client/MainGame.java | 2 +- core/src/netwerkprog/game/util/tree/BST.java | 252 ++++++++++-------- core/src/netwerkprog/game/util/tree/Tree.java | 4 +- 3 files changed, 138 insertions(+), 120 deletions(-) diff --git a/core/src/netwerkprog/game/client/MainGame.java b/core/src/netwerkprog/game/client/MainGame.java index d15ce83..0714be3 100644 --- a/core/src/netwerkprog/game/client/MainGame.java +++ b/core/src/netwerkprog/game/client/MainGame.java @@ -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) { diff --git a/core/src/netwerkprog/game/util/tree/BST.java b/core/src/netwerkprog/game/util/tree/BST.java index b8549dd..45f1bb0 100644 --- a/core/src/netwerkprog/game/util/tree/BST.java +++ b/core/src/netwerkprog/game/util/tree/BST.java @@ -1,115 +1,112 @@ package netwerkprog.game.util.tree; +import java.util.ArrayList; + public class BST> extends AbstractTree { protected TreeNode 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 node ) { - // Schrijf hier jouw code... + public int sum(TreeNode 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 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 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 tree) - { + private boolean search(E e, TreeNode 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 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> extends AbstractTree { protected TreeNode createNewNode(E e) { - return new TreeNode(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 root) { if (root == null) return; inorder(root.left); @@ -135,12 +137,17 @@ public class BST> extends AbstractTree { 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 root) { if (root == null) return; postorder(root.left); @@ -148,12 +155,17 @@ public class BST> extends AbstractTree { 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 root) { if (root == null) return; System.out.print(root.element + " "); @@ -161,8 +173,10 @@ public class BST> extends AbstractTree { 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> { protected E element; protected TreeNode left; @@ -173,41 +187,49 @@ public class BST> extends AbstractTree { } } - @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 getRoot() { return root; } - /** Returns a path from the root leading to the specified element */ - public java.util.ArrayList> path(E e) { - java.util.ArrayList> list = - new java.util.ArrayList>(); + /** + * Returns a path from the root leading to the specified element + */ + public ArrayList> path(E e) { + ArrayList> list = + new ArrayList<>(); TreeNode 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 parent = null; TreeNode current = root; @@ -215,32 +237,28 @@ public class BST> extends AbstractTree { 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> extends AbstractTree { 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 iterator() { return new InorderIterator(); } @@ -275,41 +295,51 @@ public class BST> extends AbstractTree { // Inner class InorderIterator private class InorderIterator implements java.util.Iterator { // Store the elements in a list - private java.util.ArrayList list = - new java.util.ArrayList(); + private final java.util.ArrayList 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 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> extends AbstractTree { } } - /** 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> extends AbstractTree { @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> extends AbstractTree { ", " + 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; - // } - // } diff --git a/core/src/netwerkprog/game/util/tree/Tree.java b/core/src/netwerkprog/game/util/tree/Tree.java index 60c982d..1e8c9ad 100644 --- a/core/src/netwerkprog/game/util/tree/Tree.java +++ b/core/src/netwerkprog/game/util/tree/Tree.java @@ -6,11 +6,11 @@ public interface Tree extends Iterable { /** 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();