Commit 4734045c authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Added nodeID counter for easier reference in rotation.

parent 79c5856e
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
package edu.bu.ec504.hw2;

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

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

    /**
@@ -46,16 +48,20 @@ public class BST<keyType extends Comparable<keyType> > {

    // METHODS

    // ... getters
    final Integer getNodeId() { return nodeId; }
    final keyType getKey() { return key; }

    /**
     * Compute how to transform this BST into the BST <code>otherTree</code> using rotations.
     * 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</code>
     * @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 <first> into <second.
     *         must be performed to transform this object into {@code otherTree}
     */
    public ArrayList<BstRotation<keyType> > transformTo(BST<keyType> otherTree) {
    public ArrayList<BstRotation> transformTo(BST<keyType> otherTree) {
        throw new UnsupportedOperationException("This method should be properly implemented in a subclass.");
    }

@@ -98,7 +104,7 @@ public class BST<keyType extends Comparable<keyType> > {

    /**
     * Return the BST rooted at this node as a human-readable string,
     * indented by <code>depth</code> characters
     * indented by {@code depth} characters
     *
     * @param prefix The current prefix for the subtree being printed
     */
@@ -106,7 +112,7 @@ public class BST<keyType extends Comparable<keyType> > {
        String result = "";

        // output the key
        result += key + "\n";
        result += key + "(ID "+nodeId+")\n";

        // recurse
        result += prefix + "->" + (left == null ? "\n" : left.toString(prefix.concat("  |")));
@@ -119,5 +125,6 @@ public class BST<keyType extends Comparable<keyType> > {
    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

    private Integer nodeId;         // the current node ID
    private static Integer nodeIdCounter=0; // a counter for node IDs
}
+9 −11
Original line number Diff line number Diff line
package edu.bu.ec504.hw2;

import java.util.Iterator;

/**
     * Represents one rotation (ZIG or ZAG) around one key in a Binary Search Tree
     */
    public class BstRotation<keyType> {
    public class BstRotation {
        public enum RotationType {ZIG, ZAG}

        public BstRotation(Iterator<keyType> rootOfRot, RotationType rotType) {
            RotRoot = rootOfRot;
            myRotType = rotType;
        public BstRotation(Integer myRotRootID, RotationType myRotType) {
            rotRootID = myRotRootID;
            rotType = myRotType;
        }

        /**
         * @return A human-readable description of the rotation
         */
        public String print() {
        public String toString() {
            String result = "";
            switch (myRotType) {
            switch (rotType) {
                case ZIG:
                    result = "ZIG";
                    break;
@@ -26,10 +24,10 @@ import java.util.Iterator;
                    result = "ZAG";
                    break;
            }
            result += " on " + RotRoot;
            result += " on node ID "+ rotRootID;
            return result;
        }

        private final Iterator<keyType> RotRoot;   // the root of the rotation being performed
        private final RotationType myRotType;      // the type of rotation being performed
        private final Integer rotRootID;           // the ID of the root of the rotation being performed
        private final RotationType rotType;      // the type of rotation being performed
    }
+2 −0
Original line number Diff line number Diff line
@@ -13,5 +13,7 @@ class Main {

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

    }
}