From ffe340f13f8f547b5748590c4b0b7e1877212ce0 Mon Sep 17 00:00:00 2001 From: Jonathan Damico Date: Wed, 22 Mar 2017 09:37:29 -0700 Subject: [PATCH 1/2] upload all my work --- BSTTester.java | 65 +++++++++++++++++++ MyBST.java | 169 +++++++++++++++++++++++++++++++++++++++++++++++++ TreeNode.java | 31 +++++++++ 3 files changed, 265 insertions(+) create mode 100644 BSTTester.java create mode 100644 MyBST.java create mode 100644 TreeNode.java diff --git a/BSTTester.java b/BSTTester.java new file mode 100644 index 0000000..356f38b --- /dev/null +++ b/BSTTester.java @@ -0,0 +1,65 @@ + +public class BSTTester { + + public static void main (String[] args) { + originalMain(args); + } + + public static void originalMain(String[] args) + { + String out = "Added: One trueContains: One true[One]Added: Two trueContains: Two true[One, Two]Added: Three trueContains: Three true[One, Three, Two]Added: Four trueContains: Four true[Four, One, Three, Two]Added: Five trueContains: Five true[Five, Four, One, Three, Two]Added: Six trueContains: Six true[Five, Four, One, Six, Three, Two]Added: Seven trueContains: Seven true[Five, Four, One, Seven, Six, Three, Two]Added: Eight trueContains: Eight true[Eight, Five, Four, One, Seven, Six, Three, Two]Added: Nine trueContains: Nine true[Eight, Five, Four, Nine, One, Seven, Six, Three, Two]Added: Ten trueContains: Ten true[Eight, Five, Four, Nine, One, Seven, Six, Ten, Three, Two]Removed: One true[Eight, Five, Four, Nine, Seven, Six, Ten, Three, Two]Removed: Two true[Eight, Five, Four, Nine, Seven, Six, Ten, Three]Removed: Three true[Eight, Five, Four, Nine, Seven, Six, Ten]Removed: Four true[Eight, Five, Nine, Seven, Six, Ten]Removed: Five true[Eight, Nine, Seven, Six, Ten]Removed: Six true[Eight, Nine, Seven, Ten]Removed: Seven true[Eight, Nine, Ten]Removed: Eight true[Nine, Ten]Removed: Nine true[Ten]Removed: Ten true[]"; + String[] words = {"One", "Two", "Three", "Four", "Five", + "Six", "Seven", "Eight", "Nine", "Ten"}; + StringBuilder sb = new StringBuilder(); + MyBST bst = new MyBST(); + + for (String word : words) + { + sb.append("Added: " + word + " " + bst.add(word)); + sb.append("Contains: " + word + " " + bst.contains(word)); + if(bst.add(word)) + sb.append("*** Added a duplicate value ***"); + sb.append(bst); + } + + + + for (String word : words) + { + + sb.append("Removed: " + word + " " + bst.remove(word)); + if(bst.remove(word)) + sb.append("*** Removed a non-existent value ***"); + sb.append(bst); + } + if(out.equals(sb.toString())) + System.out.println("YES"); + else + System.out.println("NO"); + } + + public static void goodMain() + { + Integer[] words = {4, 9, 8, 2, 1, 6, 5, 0, 3, 7}; + MyBST bst = new MyBST(); + + for (Integer word : words) + { + System.out.println("Added: " + word + " " + bst.add(word)); + System.out.println("Contains: " + word + " " + bst.contains(word)); + if(bst.add(word)) + System.out.println("*** Added a duplicate value ***"); + System.out.println(bst); + } + + + + for (Integer word : words) + { + System.out.println("Removed: " + word + " " + bst.remove(word)); + if(bst.remove(word)) + System.out.println("*** Removed a non-existent value ***"); + System.out.println(bst); + } + } +} diff --git a/MyBST.java b/MyBST.java new file mode 100644 index 0000000..1312b31 --- /dev/null +++ b/MyBST.java @@ -0,0 +1,169 @@ +// Implements a BST with TreeNode nodes + +import java.util.Stack; +import java.util.Iterator; +import java.util.NoSuchElementException; + +@SuppressWarnings("unchecked") +// Normally, TreeNode and MyTreeSet would be "generic" (type-specific) +// classes and hold Comparable objects, but this is beyond the scope of +// the Java Methods book. Without @SuppressWarnings, this class would give +// three "Unchecked cast" warnings. + +public class MyBST +{ + private TreeNode root; // holds the root of this BST + + // Constructor: creates an empty BST. + public MyBST() + { + root = null; + } + + // Returns true if this BST contains value; otherwise returns false. + public boolean contains(Object value) + { + return contains(root, value); + } + + // Adds value to this BST, unless this tree already holds value. + // Returns true if value has been added; otherwise returns false. + public boolean add(Comparable value) + { + if (contains(value)) + return false; + root = add(root, value); + return true; + } + + // Removes value from this BST. Returns true if value has been + // found and removed; otherwise returns false. + public boolean remove(Comparable value) + { + if (!contains(value)) + return false; + root = remove(root, value); + return true; + } + + // Returns a string representation of this BST. + public String toString() + { + String str = toString(root); + if (str.endsWith(", ")) + str = str.substring(0, str.length() - 2); + return "[" + str + "]"; + } + + //*************** Private helper methods: ********************* + + // Returns true if the BST rooted at node contains value; + // otherwise returns false (recursive version). + + private boolean contains(TreeNode node, Object value) + { + if (node == null) + return false; + else + { + int diff = ((Comparable)value).compareTo(node.getValue()); + if (diff == 0) + return true; + else if (diff < 0) + return contains(node.getLeft(), value); + else // if (diff > 0) + return contains(node.getRight(), value); + } + } + + /* + // Iterative version: + private boolean contains(TreeNode node, Object value) + { + while (node != null) + { + int diff = ((Comparable)value).compareTo(node.getValue()); + if (diff == 0) + return true; + else if (diff < 0) + node = node.getLeft(); + else // if (diff > 0) + node = node.getRight(); + } + return false; + } + */ + + // Adds value to the BST rooted at node. Returns the + // root of the new tree. + // Precondition: the tree rooted at node does not contain value. + private TreeNode add(TreeNode node, Comparable value) + { + if(node == null) + node = new TreeNode(value); + else if(node.getValue().compareTo(value)>0) + node.setLeft(add(node.getLeft(), value)); + else + node.setRight(add(node.getRight(), value)); + return node; + } + + // Removes value from the BST rooted at node. + // Returns the root of the new tree. + // Precondition: the tree rooted at node contains value. + private TreeNode remove(TreeNode node, Comparable value) { + if(value.equals(node.getValue())) { + return removeRoot(node); + } + else if(node.getValue().compareTo(value)>0) + node.setLeft(remove(node.getLeft(), value)); + else + node.setRight(remove(node.getRight(), value)); + return node; + } + + // Removes the root of the BST rooted at root + // replacing it with the smallest node from the right subtree. + // Returns the root of the new tree. + private TreeNode removeRoot(TreeNode root) { + TreeNode traverse = root; + if(traverse.getRight()!=null) { + TreeNode last = null; + traverse = traverse.getRight(); + while(traverse.getLeft()!=null) { + last = traverse; + traverse = traverse.getLeft(); + } + root.setValue(traverse.getValue()); + if(last==null) + root.setRight(traverse.getRight()); + else + last.setLeft(traverse.getLeft()); + return root; + } else if(root.getLeft()!=null) { + return root.getLeft(); + } else { + return null; + } + } + +// public boolean isBinaryTree(TreeNode root) { +// boolean ok = true; +// if(root.getLeft()!=null) +// if(node.getValue().compareTo(value)<0) +// ok = false; +// +// return ok; +// } + + // Returns a string representation of the tree rooted at node. + private String toString(TreeNode node) + { + if (node == null) + return ""; + else + return toString(node.getLeft()) + node.getValue() + ", " + + toString(node.getRight()); + } + +} diff --git a/TreeNode.java b/TreeNode.java new file mode 100644 index 0000000..fd8a5c0 --- /dev/null +++ b/TreeNode.java @@ -0,0 +1,31 @@ +public class TreeNode +{ + private Comparable value; + private TreeNode left; + private TreeNode right; + + // Constructors: + + public TreeNode(Comparable initValue) + { + value = initValue; + left = null; + right = null; + } + + public TreeNode(Comparable initValue, TreeNode initLeft, TreeNode initRight) + { + value = initValue; + left = initLeft; + right = initRight; + } + + // Methods: + + public Comparable getValue() { return value; } + public TreeNode getLeft() { return left; } + public TreeNode getRight() { return right; } + public void setValue(Comparable treeNode) { value = treeNode; } + public void setLeft(TreeNode theNewLeft) { left = theNewLeft; } + public void setRight(TreeNode theNewRight) { right = theNewRight; } +} From b69f3fb2b9eb2027cdba596c6bbaac56a30dfd3b Mon Sep 17 00:00:00 2001 From: Jonathan Damico Date: Wed, 22 Mar 2017 09:45:43 -0700 Subject: [PATCH 2/2] reup all work --- BSTTester.java => src/BSTTester.java | 0 src/MasterTester.java | 2 +- MyBST.java => src/MyBST.java | 0 TreeNode.java => src/TreeNode.java | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename BSTTester.java => src/BSTTester.java (100%) rename MyBST.java => src/MyBST.java (100%) rename TreeNode.java => src/TreeNode.java (100%) diff --git a/BSTTester.java b/src/BSTTester.java similarity index 100% rename from BSTTester.java rename to src/BSTTester.java diff --git a/src/MasterTester.java b/src/MasterTester.java index a7d923c..c1d0c23 100644 --- a/src/MasterTester.java +++ b/src/MasterTester.java @@ -29,7 +29,7 @@ public static void main (String args[]) throws Exception sb.append(bst); } - if(!(out.equals(sb.toString())) + if(!(out.equals(sb.toString()))) throw new IncorrectlyCompletedException(); } } diff --git a/MyBST.java b/src/MyBST.java similarity index 100% rename from MyBST.java rename to src/MyBST.java diff --git a/TreeNode.java b/src/TreeNode.java similarity index 100% rename from TreeNode.java rename to src/TreeNode.java