Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/BSTTester.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/MasterTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
169 changes: 169 additions & 0 deletions src/MyBST.java
Original file line number Diff line number Diff line change
@@ -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<Object>)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<Object>)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());
}

}
31 changes: 31 additions & 0 deletions src/TreeNode.java
Original file line number Diff line number Diff line change
@@ -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; }
}