Loading .idea/.gitignore 0 → 100644 +2 −0 Original line number Diff line number Diff line # Default ignored files /workspace.xml src/edu/bu/ec504/hw2/BST.java +74 −45 Original line number Diff line number Diff line package edu.bu.ec504.hw2; import java.util.List; /** * Implements a simple (not necessarily balanced) Binary Search Tree. * * @param <keyType> The type of the keys stored in this Binary Search Tree. */ public class BST<keyType extends Comparable<keyType> > { // SUBCLASSES /** * Represents the result of a {@link #contains(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 theLastSeen) { searchResult=theSearchResult; searchCount=theSearchCount; this.lastSeen =theLastSeen; } final Boolean searchResult; // true iff the search was successful public Integer searchCount=0; // the number of nodes visited in the search final BST lastSeen; } // CONSTRUCTORS Loading @@ -18,7 +41,6 @@ public class BST<keyType extends Comparable<keyType> > { key = null; leftChild = null; rightChild = null; nodeId=nodeIdCounter++; } /** Loading Loading @@ -48,68 +70,77 @@ public class BST<keyType extends Comparable<keyType> > { // METHODS // ... getters public final int getNodeId() { return nodeId; } public final keyType getKey() { return key; } public BST<keyType> getLeftChild() { return leftChild; } public BST<keyType> getRightChild() { return rightChild; } // ... setters public void setNodeID(int newId) { nodeId=newId; } public void setLeftChild(BST<keyType> newLeft) { leftChild =newLeft; } public void setRightChild(BST<keyType> newRight) { rightChild =newRight; } public static void resetNodeIDCounter() { nodeIdCounter=0; } /** * Compute how to transform this BST into the BST {@code otherTree} using rotations. * * @param otherTree The into which we seek to transform the current tree. * @expects The keys of this object are {@link #equals(Object)}} to the keys of {@code otherTree} * in a one-to-one correspondence. * @return A list of rotations indicating which rotations around which nodes * must be performed to transform this object into {@code otherTree}. */ public List<BstRotation> rotateTo(BST<keyType> otherTree) { throw new UnsupportedOperationException("This method should be properly implemented in a subclass."); } // ... operators /** * * @return Rotations needed to transform this tree into a * Search for <code>newKey</code> and report a {@link searchRecord} of the result. * @param searchKey The key for which to search. * @return {@link searchRecord} statistics related to the search. If the BST * is null, returns a null searchRecord */ public List<BstRotation> balanceTree() { throw new UnsupportedOperationException("This method should be properly implemented in a subclass."); } final 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); 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); 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); else tmpRecord = leftChild.searchHelper(searchKey); /** * Static version of {@link #rotateTo(BST)}. * Only transforms a BST to another BST of the same key type. */ public static <theKeyType extends Comparable<theKeyType> > List transform(BST<theKeyType> firstTree, BST<theKeyType> secondTree) { return firstTree.rotateTo(secondTree); tmpRecord.searchCount++; // for this node return tmpRecord; } /** * Inserts the new number as a key into the binary search tree * * @param newKey The new key to be inserted * @param newKey The new key to be inserted. */ public void insert(keyType newKey) { if (key == null) // insert here searchRecord theRecord = searchHelper(newKey); if (theRecord == null) // insert here key = newKey; else if (newKey.compareTo(key) > 0) { // i.e. newKey > key // insert to the rightChild if (rightChild == null) rightChild = new BST<>(newKey); else if (theRecord.searchResult) // the item is already in the tree throw new IllegalStateException(newKey + " is already in this data structure"); else { // put into the appropriate child BST<keyType> lastSeen = theRecord.lastSeen; if (newKey.compareTo(lastSeen.key) > 0) // i.e. newKey > key lastSeen.rightChild = new BST<>(newKey); else rightChild.insert(newKey); } else // num<=key; insert to the leftChild if (leftChild == null) leftChild = new BST<>(newKey); else leftChild.insert(newKey); lastSeen.leftChild = new BST<>(newKey); } } /** * @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 @@ -127,7 +158,7 @@ public class BST<keyType extends Comparable<keyType> > { String result = ""; // output the key result += key + "(ID "+nodeId+")\n"; result += key + "\n"; // recurse result += prefix + "->" + (leftChild == null ? "\n" : leftChild.toString(prefix.concat(" |"))); Loading @@ -140,6 +171,4 @@ public class BST<keyType extends Comparable<keyType> > { protected keyType key; // the key stored by this node protected BST<keyType> leftChild; // the left child of this node protected BST<keyType> rightChild; // the right child of this node protected Integer nodeId; // the current node ID private static Integer nodeIdCounter=0; // a counter for node IDs } src/edu/bu/ec504/hw2/Main.java +5 −0 Original line number Diff line number Diff line Loading @@ -11,8 +11,13 @@ class Main { BST<Integer> intTest = new BST<>(intElements); BST<String> strTest = new BST<>(strElements); BST.searchRecord result; System.out.println("Your int BST is:\n"+intTest); System.out.println("... rightChild subtree:\n"+intTest.getRightChild()); result = intTest.contains(0); System.out.println("... 0 is in your BST? " + result.searchResult+" in "+result.searchCount+" steps;\n "+result.lastSeen); result = intTest.contains(6); System.out.println("... 6 is in your BST? " + result.searchResult+" in "+result.searchCount+" steps;\n "+result.lastSeen); System.out.println("Your string BST is:\n"+strTest); System.out.println("One rotation: "+new BstRotation(3, BstRotation.RotationType.ZAG)); Loading Loading
.idea/.gitignore 0 → 100644 +2 −0 Original line number Diff line number Diff line # Default ignored files /workspace.xml
src/edu/bu/ec504/hw2/BST.java +74 −45 Original line number Diff line number Diff line package edu.bu.ec504.hw2; import java.util.List; /** * Implements a simple (not necessarily balanced) Binary Search Tree. * * @param <keyType> The type of the keys stored in this Binary Search Tree. */ public class BST<keyType extends Comparable<keyType> > { // SUBCLASSES /** * Represents the result of a {@link #contains(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 theLastSeen) { searchResult=theSearchResult; searchCount=theSearchCount; this.lastSeen =theLastSeen; } final Boolean searchResult; // true iff the search was successful public Integer searchCount=0; // the number of nodes visited in the search final BST lastSeen; } // CONSTRUCTORS Loading @@ -18,7 +41,6 @@ public class BST<keyType extends Comparable<keyType> > { key = null; leftChild = null; rightChild = null; nodeId=nodeIdCounter++; } /** Loading Loading @@ -48,68 +70,77 @@ public class BST<keyType extends Comparable<keyType> > { // METHODS // ... getters public final int getNodeId() { return nodeId; } public final keyType getKey() { return key; } public BST<keyType> getLeftChild() { return leftChild; } public BST<keyType> getRightChild() { return rightChild; } // ... setters public void setNodeID(int newId) { nodeId=newId; } public void setLeftChild(BST<keyType> newLeft) { leftChild =newLeft; } public void setRightChild(BST<keyType> newRight) { rightChild =newRight; } public static void resetNodeIDCounter() { nodeIdCounter=0; } /** * Compute how to transform this BST into the BST {@code otherTree} using rotations. * * @param otherTree The into which we seek to transform the current tree. * @expects The keys of this object are {@link #equals(Object)}} to the keys of {@code otherTree} * in a one-to-one correspondence. * @return A list of rotations indicating which rotations around which nodes * must be performed to transform this object into {@code otherTree}. */ public List<BstRotation> rotateTo(BST<keyType> otherTree) { throw new UnsupportedOperationException("This method should be properly implemented in a subclass."); } // ... operators /** * * @return Rotations needed to transform this tree into a * Search for <code>newKey</code> and report a {@link searchRecord} of the result. * @param searchKey The key for which to search. * @return {@link searchRecord} statistics related to the search. If the BST * is null, returns a null searchRecord */ public List<BstRotation> balanceTree() { throw new UnsupportedOperationException("This method should be properly implemented in a subclass."); } final 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); 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); 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); else tmpRecord = leftChild.searchHelper(searchKey); /** * Static version of {@link #rotateTo(BST)}. * Only transforms a BST to another BST of the same key type. */ public static <theKeyType extends Comparable<theKeyType> > List transform(BST<theKeyType> firstTree, BST<theKeyType> secondTree) { return firstTree.rotateTo(secondTree); tmpRecord.searchCount++; // for this node return tmpRecord; } /** * Inserts the new number as a key into the binary search tree * * @param newKey The new key to be inserted * @param newKey The new key to be inserted. */ public void insert(keyType newKey) { if (key == null) // insert here searchRecord theRecord = searchHelper(newKey); if (theRecord == null) // insert here key = newKey; else if (newKey.compareTo(key) > 0) { // i.e. newKey > key // insert to the rightChild if (rightChild == null) rightChild = new BST<>(newKey); else if (theRecord.searchResult) // the item is already in the tree throw new IllegalStateException(newKey + " is already in this data structure"); else { // put into the appropriate child BST<keyType> lastSeen = theRecord.lastSeen; if (newKey.compareTo(lastSeen.key) > 0) // i.e. newKey > key lastSeen.rightChild = new BST<>(newKey); else rightChild.insert(newKey); } else // num<=key; insert to the leftChild if (leftChild == null) leftChild = new BST<>(newKey); else leftChild.insert(newKey); lastSeen.leftChild = new BST<>(newKey); } } /** * @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 @@ -127,7 +158,7 @@ public class BST<keyType extends Comparable<keyType> > { String result = ""; // output the key result += key + "(ID "+nodeId+")\n"; result += key + "\n"; // recurse result += prefix + "->" + (leftChild == null ? "\n" : leftChild.toString(prefix.concat(" |"))); Loading @@ -140,6 +171,4 @@ public class BST<keyType extends Comparable<keyType> > { protected keyType key; // the key stored by this node protected BST<keyType> leftChild; // the left child of this node protected BST<keyType> rightChild; // the right child of this node protected Integer nodeId; // the current node ID private static Integer nodeIdCounter=0; // a counter for node IDs }
src/edu/bu/ec504/hw2/Main.java +5 −0 Original line number Diff line number Diff line Loading @@ -11,8 +11,13 @@ class Main { BST<Integer> intTest = new BST<>(intElements); BST<String> strTest = new BST<>(strElements); BST.searchRecord result; System.out.println("Your int BST is:\n"+intTest); System.out.println("... rightChild subtree:\n"+intTest.getRightChild()); result = intTest.contains(0); System.out.println("... 0 is in your BST? " + result.searchResult+" in "+result.searchCount+" steps;\n "+result.lastSeen); result = intTest.contains(6); System.out.println("... 6 is in your BST? " + result.searchResult+" in "+result.searchCount+" steps;\n "+result.lastSeen); System.out.println("Your string BST is:\n"+strTest); System.out.println("One rotation: "+new BstRotation(3, BstRotation.RotationType.ZAG)); Loading