Loading src/edu/bu/ec504/hw2/Main.java +7 −5 Original line number Diff line number Diff line package edu.bu.ec504.hw2; import edu.bu.ec504.hw2.bst.BST; import edu.bu.ec504.hw2.bst.BSTRotation; import java.util.ArrayList; import java.util.Arrays; Loading @@ -14,12 +16,12 @@ class Main { BST<Integer>.searchRecord intResult; System.out.println("Your int BST is:\n"+intTest); System.out.println("... rightChild subtree:\n"+intTest.getRightChild()); intResult = intTest.contains(0); System.out.println("... 0 is in your BST? " + intResult.searchResult+" in "+intResult.searchCount+" steps;\n "+intResult.lastSeen); intResult = intTest.contains(6); System.out.println("... 6 is in your BST? " + intResult.searchResult+" in "+intResult.searchCount+" steps;\n "+intResult.lastSeen); intResult = intTest.extendedContains(0); System.out.println("... 0 is in your BST? " + intResult.getSearchResult()+" in "+intResult.getSearchCount()+" steps;\n "+intResult.getLastSeen()); intResult = intTest.extendedContains(6); System.out.println("... 6 is in your BST? " + intResult.getSearchResult()+" in "+intResult.getSearchCount()+" steps;\n "+intResult.getLastSeen()); System.out.println("Your string BST is:\n"+strTest); System.out.println("One rotation: "+new BstRotation(3, BstRotation.RotationType.ZAG)); System.out.println("One rotation: "+new BSTRotation(3, BSTRotation.RotationType.ZAG)); } } src/edu/bu/ec504/hw2/BST.java→src/edu/bu/ec504/hw2/bst/BST.java +57 −21 Original line number Diff line number Diff line package edu.bu.ec504.hw2; package edu.bu.ec504.hw2.bst; /** * Implements a simple (not necessarily balanced) Binary Search Tree. Loading @@ -9,26 +9,45 @@ public class BST<keyType extends Comparable<keyType> > { // SUBCLASSES /** * Represents the result of a {@link #contains(Comparable)} call. * Represents the result of a {@link #extendedContains(Comparable)} call. */ final public class searchRecord { /** * Constructs a search record * @param theSearchResult - true iff the search was successful * @param theSearchCount - the number of nodes visited in the search * @param theLastSeen - if the search succeeds, this is the node containing the searched key; * - if the search failed, this contains the last node before falling * off the tree during the search. Otherwise, it is null. */ searchRecord(Boolean theSearchResult, Integer theSearchCount, BST<keyType> theLastSeen) { searchRecord(Boolean theSearchResult, BST<keyType> theLastSeen) { searchResult=theSearchResult; searchCount=theSearchCount; this.lastSeen =theLastSeen; } /** * @return The result produced by the search the search represented by this record. * The result is true iff the search was successful. */ public boolean getSearchResult() { return searchResult; } /** * @return The number of nodes visited during the search represented by this record. */ public Integer getSearchCount() { return searchCount; } /** * @return a pointer to the last node seen in the search represented by this record. */ public BST<keyType> getLastSeen() { return lastSeen; } final Boolean searchResult; // true iff the search was successful public Integer searchCount; // the number of nodes visited in the search Integer searchCount=0; // the number of nodes visited in the search final BST<keyType> lastSeen; } Loading Loading @@ -86,21 +105,21 @@ public class BST<keyType extends Comparable<keyType> > { * @return {@link searchRecord} statistics related to the search. If the BST * is null, returns a null searchRecord */ final searchRecord searchHelper(keyType searchKey) { protected searchRecord searchHelper(keyType searchKey) { searchRecord tmpRecord; if (key == null) // the BST is null return null; else if (searchKey.compareTo(key) == 0) // i.e., searchKey == key tmpRecord = new searchRecord(true, 0, this); tmpRecord = new searchRecord(true, this); else if (searchKey.compareTo(key) > 0) { // i.e., searchKey > key --- look at the rightChild if (rightChild == null) // no subtree tmpRecord = new searchRecord(false, 0, this); tmpRecord = new searchRecord(false, this); else tmpRecord = rightChild.searchHelper(searchKey); } else // i.e., searchKey<=key --- look at the leftChild if (leftChild == null) // no subtree tmpRecord = new searchRecord(false, 0, this); tmpRecord = new searchRecord(false, this); else tmpRecord = leftChild.searchHelper(searchKey); Loading @@ -109,6 +128,33 @@ public class BST<keyType extends Comparable<keyType> > { return tmpRecord; } // ... final methods that you cannot modify /** * Provides a search record with extended information about hte search for <code>theKey</code>. * @param theKey The key for which to search. * @return a <Boolean,Integer> pair: * * the Boolean is true iff the BST rooted at the current object contains <code>theKey</code>; * * the Integer returns the number of nodes visited in this subtree during the search for <code>theKey</code>. */ final public searchRecord extendedContains(keyType theKey) { return searchHelper(theKey); } /** * A generic version of {@link #extendedContains(Comparable)} that matches the {@link java.util.Set} interface. * Checks whether object obj exists in this data structure. * @param obj The object to check for containment in the data structure. * @return true iff the object is in the data structure * @throws ClassCastException if <code>obj</code> is not of type {@link keyType}. */ @SuppressWarnings("unchecked") final public boolean contains(Object obj) { if (obj==null) return false; keyType theKey = (keyType) obj; return extendedContains(theKey).searchResult; } /** * Inserts the new number as a key into the binary search tree * Loading @@ -131,16 +177,6 @@ public class BST<keyType extends Comparable<keyType> > { } } /** * @param theKey The key for which to search. * @return a <Boolean,Integer> pair: * * the Boolean is true iff the BST rooted at the current object contains <code>theKey</code>; * * the Integer returns the number of nodes visited in this subtree during the search for <code>theKey</code>. */ final public searchRecord contains(keyType theKey) { return searchHelper(theKey); } // ... informational /** * Return the BST rooted at this node as a human-readable string Loading src/edu/bu/ec504/hw2/BstRotation.java→src/edu/bu/ec504/hw2/bst/BSTRotation.java +3 −3 Original line number Diff line number Diff line package edu.bu.ec504.hw2; package edu.bu.ec504.hw2.bst; /** * Represents one rotation (ZIG or ZAG) around one key in a Binary Search Tree */ public class BstRotation { public class BSTRotation { public enum RotationType {ZIG, ZAG} public BstRotation(Integer myRotKey, RotationType myRotType) { public BSTRotation(Integer myRotKey, RotationType myRotType) { rotKey = myRotKey; rotType = myRotType; } Loading src/edu/bu/ec504/hw2/bst/BSTSet.java 0 → 100644 +108 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw2.bst; import java.util.Collection; import java.util.Iterator; import java.util.Set; /** * An extension of BST that supports * @param <keyType> */ public class BSTSet<keyType extends Comparable<keyType>> extends BST<keyType> implements Set<keyType> { /** * @inheritDoc */ @Override public int size() { return 0; } /** * @inheritDoc */ @Override public boolean isEmpty() { return false; } /** * @inheritDoc */ @Override public Iterator<keyType> iterator() { return null; } /** * @inheritDoc */ @Override public Object[] toArray() { return new Object[0]; } /** * @inheritDoc */ @Override public <T> T[] toArray(T[] a) { return null; } /** * @inheritDoc */ @Override public boolean add(keyType keyType) { return false; } /** * @inheritDoc */ @Override public boolean remove(Object o) { return false; } /** * @inheritDoc */ @Override public boolean containsAll(Collection<?> c) { return false; } /** * @inheritDoc */ @Override public boolean addAll(Collection<? extends keyType> c) { return false; } /** * @inheritDoc */ @Override public boolean retainAll(Collection<?> c) { return false; } /** * @inheritDoc */ @Override public boolean removeAll(Collection<?> c) { return false; } /** * @inheritDoc */ @Override public void clear() { } } src/edu/bu/ec504/hw2/bst/ExtendedBSTSet.java 0 → 100644 +57 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw2.bst; public class ExtendedBSTSet<keyType extends Comparable<keyType>> extends BSTSet<keyType> { // METHODS /** * @return The parent of the current node. */ public ExtendedBSTSet<keyType> getParent() { return parent; } /** * Perform the rotation specified in <code>op</code> on the current tree. * @param op A rotational operation to complete. * @return true iff the rotation was successful. */ public boolean rotate(BSTRotation op) { return false; } /** * @inheritDoc */ @Override protected searchRecord searchHelper(keyType searchKey) { return super.searchHelper(searchKey); } /** * @inheritDoc */ @Override public void insert(keyType newKey) { super.insert(newKey); } /** * @inheritDoc */ @Override public String toString() { return super.toString(); } /** * @inheritDoc */ @Override String toString(String prefix) { return super.toString(prefix); } // FIELDS protected ExtendedBSTSet<keyType> parent; // the parent of this node, or null if this node has no parent (e.g. it is the global root) } Loading
src/edu/bu/ec504/hw2/Main.java +7 −5 Original line number Diff line number Diff line package edu.bu.ec504.hw2; import edu.bu.ec504.hw2.bst.BST; import edu.bu.ec504.hw2.bst.BSTRotation; import java.util.ArrayList; import java.util.Arrays; Loading @@ -14,12 +16,12 @@ class Main { BST<Integer>.searchRecord intResult; System.out.println("Your int BST is:\n"+intTest); System.out.println("... rightChild subtree:\n"+intTest.getRightChild()); intResult = intTest.contains(0); System.out.println("... 0 is in your BST? " + intResult.searchResult+" in "+intResult.searchCount+" steps;\n "+intResult.lastSeen); intResult = intTest.contains(6); System.out.println("... 6 is in your BST? " + intResult.searchResult+" in "+intResult.searchCount+" steps;\n "+intResult.lastSeen); intResult = intTest.extendedContains(0); System.out.println("... 0 is in your BST? " + intResult.getSearchResult()+" in "+intResult.getSearchCount()+" steps;\n "+intResult.getLastSeen()); intResult = intTest.extendedContains(6); System.out.println("... 6 is in your BST? " + intResult.getSearchResult()+" in "+intResult.getSearchCount()+" steps;\n "+intResult.getLastSeen()); System.out.println("Your string BST is:\n"+strTest); System.out.println("One rotation: "+new BstRotation(3, BstRotation.RotationType.ZAG)); System.out.println("One rotation: "+new BSTRotation(3, BSTRotation.RotationType.ZAG)); } }
src/edu/bu/ec504/hw2/BST.java→src/edu/bu/ec504/hw2/bst/BST.java +57 −21 Original line number Diff line number Diff line package edu.bu.ec504.hw2; package edu.bu.ec504.hw2.bst; /** * Implements a simple (not necessarily balanced) Binary Search Tree. Loading @@ -9,26 +9,45 @@ public class BST<keyType extends Comparable<keyType> > { // SUBCLASSES /** * Represents the result of a {@link #contains(Comparable)} call. * Represents the result of a {@link #extendedContains(Comparable)} call. */ final public class searchRecord { /** * Constructs a search record * @param theSearchResult - true iff the search was successful * @param theSearchCount - the number of nodes visited in the search * @param theLastSeen - if the search succeeds, this is the node containing the searched key; * - if the search failed, this contains the last node before falling * off the tree during the search. Otherwise, it is null. */ searchRecord(Boolean theSearchResult, Integer theSearchCount, BST<keyType> theLastSeen) { searchRecord(Boolean theSearchResult, BST<keyType> theLastSeen) { searchResult=theSearchResult; searchCount=theSearchCount; this.lastSeen =theLastSeen; } /** * @return The result produced by the search the search represented by this record. * The result is true iff the search was successful. */ public boolean getSearchResult() { return searchResult; } /** * @return The number of nodes visited during the search represented by this record. */ public Integer getSearchCount() { return searchCount; } /** * @return a pointer to the last node seen in the search represented by this record. */ public BST<keyType> getLastSeen() { return lastSeen; } final Boolean searchResult; // true iff the search was successful public Integer searchCount; // the number of nodes visited in the search Integer searchCount=0; // the number of nodes visited in the search final BST<keyType> lastSeen; } Loading Loading @@ -86,21 +105,21 @@ public class BST<keyType extends Comparable<keyType> > { * @return {@link searchRecord} statistics related to the search. If the BST * is null, returns a null searchRecord */ final searchRecord searchHelper(keyType searchKey) { protected searchRecord searchHelper(keyType searchKey) { searchRecord tmpRecord; if (key == null) // the BST is null return null; else if (searchKey.compareTo(key) == 0) // i.e., searchKey == key tmpRecord = new searchRecord(true, 0, this); tmpRecord = new searchRecord(true, this); else if (searchKey.compareTo(key) > 0) { // i.e., searchKey > key --- look at the rightChild if (rightChild == null) // no subtree tmpRecord = new searchRecord(false, 0, this); tmpRecord = new searchRecord(false, this); else tmpRecord = rightChild.searchHelper(searchKey); } else // i.e., searchKey<=key --- look at the leftChild if (leftChild == null) // no subtree tmpRecord = new searchRecord(false, 0, this); tmpRecord = new searchRecord(false, this); else tmpRecord = leftChild.searchHelper(searchKey); Loading @@ -109,6 +128,33 @@ public class BST<keyType extends Comparable<keyType> > { return tmpRecord; } // ... final methods that you cannot modify /** * Provides a search record with extended information about hte search for <code>theKey</code>. * @param theKey The key for which to search. * @return a <Boolean,Integer> pair: * * the Boolean is true iff the BST rooted at the current object contains <code>theKey</code>; * * the Integer returns the number of nodes visited in this subtree during the search for <code>theKey</code>. */ final public searchRecord extendedContains(keyType theKey) { return searchHelper(theKey); } /** * A generic version of {@link #extendedContains(Comparable)} that matches the {@link java.util.Set} interface. * Checks whether object obj exists in this data structure. * @param obj The object to check for containment in the data structure. * @return true iff the object is in the data structure * @throws ClassCastException if <code>obj</code> is not of type {@link keyType}. */ @SuppressWarnings("unchecked") final public boolean contains(Object obj) { if (obj==null) return false; keyType theKey = (keyType) obj; return extendedContains(theKey).searchResult; } /** * Inserts the new number as a key into the binary search tree * Loading @@ -131,16 +177,6 @@ public class BST<keyType extends Comparable<keyType> > { } } /** * @param theKey The key for which to search. * @return a <Boolean,Integer> pair: * * the Boolean is true iff the BST rooted at the current object contains <code>theKey</code>; * * the Integer returns the number of nodes visited in this subtree during the search for <code>theKey</code>. */ final public searchRecord contains(keyType theKey) { return searchHelper(theKey); } // ... informational /** * Return the BST rooted at this node as a human-readable string Loading
src/edu/bu/ec504/hw2/BstRotation.java→src/edu/bu/ec504/hw2/bst/BSTRotation.java +3 −3 Original line number Diff line number Diff line package edu.bu.ec504.hw2; package edu.bu.ec504.hw2.bst; /** * Represents one rotation (ZIG or ZAG) around one key in a Binary Search Tree */ public class BstRotation { public class BSTRotation { public enum RotationType {ZIG, ZAG} public BstRotation(Integer myRotKey, RotationType myRotType) { public BSTRotation(Integer myRotKey, RotationType myRotType) { rotKey = myRotKey; rotType = myRotType; } Loading
src/edu/bu/ec504/hw2/bst/BSTSet.java 0 → 100644 +108 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw2.bst; import java.util.Collection; import java.util.Iterator; import java.util.Set; /** * An extension of BST that supports * @param <keyType> */ public class BSTSet<keyType extends Comparable<keyType>> extends BST<keyType> implements Set<keyType> { /** * @inheritDoc */ @Override public int size() { return 0; } /** * @inheritDoc */ @Override public boolean isEmpty() { return false; } /** * @inheritDoc */ @Override public Iterator<keyType> iterator() { return null; } /** * @inheritDoc */ @Override public Object[] toArray() { return new Object[0]; } /** * @inheritDoc */ @Override public <T> T[] toArray(T[] a) { return null; } /** * @inheritDoc */ @Override public boolean add(keyType keyType) { return false; } /** * @inheritDoc */ @Override public boolean remove(Object o) { return false; } /** * @inheritDoc */ @Override public boolean containsAll(Collection<?> c) { return false; } /** * @inheritDoc */ @Override public boolean addAll(Collection<? extends keyType> c) { return false; } /** * @inheritDoc */ @Override public boolean retainAll(Collection<?> c) { return false; } /** * @inheritDoc */ @Override public boolean removeAll(Collection<?> c) { return false; } /** * @inheritDoc */ @Override public void clear() { } }
src/edu/bu/ec504/hw2/bst/ExtendedBSTSet.java 0 → 100644 +57 −0 Original line number Diff line number Diff line package edu.bu.ec504.hw2.bst; public class ExtendedBSTSet<keyType extends Comparable<keyType>> extends BSTSet<keyType> { // METHODS /** * @return The parent of the current node. */ public ExtendedBSTSet<keyType> getParent() { return parent; } /** * Perform the rotation specified in <code>op</code> on the current tree. * @param op A rotational operation to complete. * @return true iff the rotation was successful. */ public boolean rotate(BSTRotation op) { return false; } /** * @inheritDoc */ @Override protected searchRecord searchHelper(keyType searchKey) { return super.searchHelper(searchKey); } /** * @inheritDoc */ @Override public void insert(keyType newKey) { super.insert(newKey); } /** * @inheritDoc */ @Override public String toString() { return super.toString(); } /** * @inheritDoc */ @Override String toString(String prefix) { return super.toString(prefix); } // FIELDS protected ExtendedBSTSet<keyType> parent; // the parent of this node, or null if this node has no parent (e.g. it is the global root) }