Commit 65f283f8 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

initial version

parent 0059ab7e
Loading
Loading
Loading
Loading

.idea/encodings.xml

0 → 100644
+4 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
 No newline at end of file
+34 −27
Original line number Diff line number Diff line
package edu.bu.ec504.hw2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Implements a simple (not necessarily balanced) Binary Search Tree.
@@ -17,8 +16,8 @@ public class BST<keyType extends Comparable<keyType> > {
     */
    public BST() {
        key = null;
        left = null;
        right = null;
        leftChild = null;
        rightChild = null;
        nodeId=nodeIdCounter++;
    }

@@ -51,13 +50,13 @@ public class BST<keyType extends Comparable<keyType> > {
    // ... getters
    public final int getNodeId() { return nodeId; }
    public final keyType getKey() { return key; }
    public BST<keyType> getLeft() { return left; }
    public BST<keyType> getRight() { return right; }
    public BST<keyType> getLeftChild() { return leftChild; }
    public BST<keyType> getRightChild() { return rightChild; }

    // ... setters
    public void setNodeID(int newId) { nodeId=newId; }
    public void setLeft(BST<keyType> newLeft) { left=newLeft; }
    public void setRight(BST<keyType> newRight) { right=newRight; }
    public void setLeftChild(BST<keyType> newLeft) { leftChild =newLeft; }
    public void setRightChild(BST<keyType> newRight) { rightChild =newRight; }
    public static void resetNodeIDCounter() { nodeIdCounter=0; }

    /**
@@ -66,20 +65,28 @@ public class BST<keyType extends Comparable<keyType> > {
     * @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 An ArrayList of rotations indicating which rotations around which nodes
     *         must be performed to transform this object into {@code otherTree}
     * @return A list of rotations indicating which rotations around which nodes
     *         must be performed to transform this object into {@code otherTree}.
     */
    public ArrayList<BstRotation> transformTo(BST<keyType> otherTree) {
    public List<BstRotation> rotateTo(BST<keyType> otherTree) {
        throw new UnsupportedOperationException("This method should be properly implemented in a subclass.");
    }

    /**
     * Static version of {@link #transformTo(BST)}.
     *
     * @return Rotations needed to transform this tree into a
     */
    public List<BstRotation> balanceTree() {
        throw new UnsupportedOperationException("This method should be properly implemented in a subclass.");
    }

    /**
     * Static version of {@link #rotateTo(BST)}.
     * Only transforms a BST to another BST of the same key type.
     */
    public static <oneKeyType extends Comparable<oneKeyType> > ArrayList
    transform(BST<oneKeyType> firstTree, BST<oneKeyType> secondTree) {
        return firstTree.transformTo(secondTree);
    public static <theKeyType extends Comparable<theKeyType> > List
    transform(BST<theKeyType> firstTree, BST<theKeyType> secondTree) {
        return firstTree.rotateTo(secondTree);
    }

    /**
@@ -91,16 +98,16 @@ public class BST<keyType extends Comparable<keyType> > {
        if (key == null) // insert here
            key = newKey;
        else if (newKey.compareTo(key) > 0) { // i.e. newKey > key
            // insert to the right
            if (right == null)
                right = new BST<>(newKey);
            // insert to the rightChild
            if (rightChild == null)
                rightChild = new BST<>(newKey);
            else
                right.insert(newKey);
        } else // num<=key; insert to the left
            if (left == null)
                left = new BST<>(newKey);
                rightChild.insert(newKey);
        } else // num<=key; insert to the leftChild
            if (leftChild == null)
                leftChild = new BST<>(newKey);
            else
                left.insert(newKey);
                leftChild.insert(newKey);
    }

    /**
@@ -123,16 +130,16 @@ public class BST<keyType extends Comparable<keyType> > {
        result += key + "(ID "+nodeId+")\n";

        // recurse
        result += prefix + "->" + (left == null ? "\n" : left.toString(prefix.concat("  |")));
        result += prefix + "->" + (right == null ? "\n": right.toString(prefix.replace("  |","   ").concat("  |")));
        result += prefix + "->" + (leftChild == null ? "\n" : leftChild.toString(prefix.concat("  |")));
        result += prefix + "->" + (rightChild == null ? "\n": rightChild.toString(prefix.replace("  |","   ").concat("  |")));
        return result;
    }


    // FIELDS
    protected keyType key; // the key stored by this node
    protected BST<keyType> left;    // the left child of this node
    protected BST<keyType> right;   // the right child of 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
}
+5 −5
Original line number Diff line number Diff line
@@ -15,17 +15,17 @@ package edu.bu.ec504.hw2;
         * @return A human-readable description of the rotation
         */
        public String toString() {
            String result = "";
            StringBuilder result = new StringBuilder();
            switch (rotType) {
                case ZIG:
                    result = "ZIG";
                    result.append("ZIG");
                    break;
                case ZAG:
                    result = "ZAG";
                    result.append("ZAG");
                    break;
            }
            result += " on node ID "+ rotRootID;
            return result;
            result.append(" on node ID ").append(rotRootID);
            return result.toString();
        }

        private final Integer rotRootID;           // the ID of the root of the rotation being performed
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ class Main {
        BST<String> strTest = new BST<>(strElements);

        System.out.println("Your int BST is:\n"+intTest);
        System.out.println("... right subtree:\n"+intTest.getRight());
        System.out.println("... rightChild subtree:\n"+intTest.getRightChild());
        System.out.println("Your string BST is:\n"+strTest);
        System.out.println("One rotation: "+new BstRotation(3, BstRotation.RotationType.ZAG));