intial commit

This commit is contained in:
Joel Beckmeyer
2017-12-08 14:47:48 -05:00
commit 0748930c0f
17 changed files with 2231 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package termproject;
/**
* Title: Project #7
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public interface Comparator {
public boolean isLessThan (Object obj1, Object obj2);
public boolean isLessThanOrEqualTo (Object obj1, Object obj2);
public boolean isGreaterThan (Object obj1, Object obj2);
public boolean isGreaterThanOrEqualTo (Object obj1, Object obj2);
public boolean isEqual (Object obj1, Object obj2);
public boolean isComparable (Object obj);
}

View File

@@ -0,0 +1,42 @@
package termproject;
/**
* Term Project 2-4 Trees
*
* @author Dr. Gallagher
* @version 1.0
* Created 2 Mar 2001
* Summary of Modifications:
*
* Description: Abstraction for Dictionary ADT. Works for either ordered
* or unordered dictionary
*/
public interface Dictionary {
public int size();
public boolean isEmpty();
/**
* Searches dictionary to determine if key is present
* @param key to be searched for
* @return object corresponding to key; null if not found
*/
public Object findElement (Object key);
/**
* Inserts provided element into the Dictionary
* @param key of object to be inserted
* @param element to be inserted
*/
public void insertElement (Object key, Object element);
/**
* Searches dictionary to determine if key is present, then
* removes and returns corresponding object
* @param key of data to be removed
* @return object corresponding to key
* @exception ElementNotFoundException if the key is not in dictionary
*/
public Object removeElement (Object key) throws ElementNotFoundException;
}

View File

@@ -0,0 +1,21 @@
package termproject;
/**
* Title: Term Project 2-4 Trees
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class ElementNotFoundException extends RuntimeException {
public ElementNotFoundException() {
super ("Problem with TwoFourTree");
}
public ElementNotFoundException(String errorMsg) {
super (errorMsg);
}
}

View File

@@ -0,0 +1,56 @@
package termproject;
/**
* Title: Project #7
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class IntegerComparator implements Comparator {
public IntegerComparator() {
}
public boolean isLessThan (Object obj1, Object obj2) {
Integer myInt1;
Integer myInt2;
try {
myInt1 = (Integer) obj1;
myInt2 = (Integer) obj2;
}
catch (ClassCastException exc) {
throw new InvalidIntegerException ("Object not an integer");
}
return ( myInt1.intValue() < myInt2.intValue() );
}
public boolean isLessThanOrEqualTo (Object obj1, Object obj2) {
return ( ! isLessThan (obj2, obj1) );
}
public boolean isGreaterThan (Object obj1, Object obj2) {
return ( isLessThan (obj2, obj1) );
}
public boolean isGreaterThanOrEqualTo (Object obj1, Object obj2) {
return ( ! isLessThan (obj1, obj2) );
}
public boolean isEqual (Object obj1, Object obj2) {
return ( (! isLessThan (obj1, obj2)) && (! isLessThan (obj2, obj1)) );
}
public boolean isComparable (Object obj) {
try {
Integer myInt = (Integer) obj;
return true;
}
catch (ClassCastException exc) {
return false;
}
}
}

View File

@@ -0,0 +1,17 @@
package termproject;
/**
* Title: Project #7
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class InvalidIntegerException extends RuntimeException {
public InvalidIntegerException(String errorMsg) {
super (errorMsg);
}
}

38
src/termproject/Item.java Normal file
View File

@@ -0,0 +1,38 @@
package termproject;
/**
* Basic storage element for storing a key and data
*
* @author Dr. Gallagher
* @version 1.0
* Created 2 Mar 2001
* Description: Stores 2 objects: a key and an element
*/
public class Item {
private Object itemKey;
private Object itemElement;
public Item() {
this (null, null);
}
public Item(Object key, Object element) {
itemKey = key;
itemElement = element;
}
public Object key() {
return itemKey;
}
public void setKey(Object key) {
itemKey = key;
}
public Object element() {
return itemElement;
}
public void setElement (Object element) {
itemElement = element;
}
}

136
src/termproject/TFNode.java Normal file
View File

@@ -0,0 +1,136 @@
package termproject;
/**
* Basic storage element for the 2-4 Tree
*
* @author Dr. Gallagher
* @version 1.0
* Created 2 Mar 2001
* Summary of Modifications
* 3 Dec 2009 - DMG - changed type for data stored in TFNode to Item
* and changed necessary methods to deal with Item instead of Object
* Description: The basic node for a 2-4 tree. Contains an array of Items,
* an array of references to children TFNodes, a pointer to a parent TFNode,
* and a count of how many Items are stored in the node.
*/
public class TFNode {
private static final int MAX_ITEMS = 3;
private int numItems = 0;
private TFNode nodeParent;
private TFNode[] nodeChildren;
// DMG 3 Dec 09 - changed type to Item
private Item[] nodeItems;
public TFNode() {
// make them one bigger than needed, so can handle oversize nodes
// during inserts
nodeChildren = new TFNode[MAX_ITEMS+2];
nodeItems = new Item[MAX_ITEMS+1];
}
public int getNumItems () {
return numItems;
}
public int getMaxItems() {
return MAX_ITEMS;
}
public TFNode getParent() {
return nodeParent;
}
public void setParent (TFNode parent) {
nodeParent = parent;
}
public Item getItem(int index) {
if ( (index < 0) || (index > (numItems-1) ) )
throw new TFNodeException();
return nodeItems[index];
}
// adds, but does not extend array; so it overwrites anything there
public void addItem (int index, Item data) {
// always add at end+1; check that you are within array
if ( (index < 0) || (index > numItems) || (index > MAX_ITEMS) )
throw new TFNodeException();
nodeItems[index] = data;
numItems++;
}
// this function inserts an item into the node, and adjusts into child
// pointers to add the proper corresponding pointer
public void insertItem (int index, Item data) {
if ( (index < 0) || (index > numItems) || (index > MAX_ITEMS) )
throw new TFNodeException();
// adjust Items
for (int ind=numItems; ind > index; ind--) {
nodeItems[ind] = nodeItems[ind-1];
}
// insert new data into hole made
nodeItems[index] = data;
// adjust children pointers; if inserting into index=1, we make
// pointers 1 and 2 to point to 1; this is because whoever called
// this function will fix one of them later; index 0 doesn't change;
// pointer 3 becomes pointer 2; pointer 4 becomes 3, etc.
for (int ind=numItems+1; ind > index; ind--) {
nodeChildren[ind] = nodeChildren[ind-1];
}
numItems++;
}
// this method removes item, and shrinks array
public Item removeItem (int index) {
if ( (index < 0) || (index > (numItems-1) ) )
throw new TFNodeException();
Item removedItem = nodeItems[index];
for (int ind=index; ind < numItems-1; ind++) {
nodeItems[ind] = nodeItems[ind+1];
}
nodeItems[numItems-1] = null;
// fix children pointers also
// typically, you wouldn't expect to do a removeItem unless
// children are null, because removal of an item will mess up the
// pointers; however, here we will simply delete the child to the
// left of the removed item; i.e., the child with same index
for (int ind=index; ind < numItems; ind++) {
nodeChildren[ind] = nodeChildren[ind+1];
}
nodeChildren[numItems] = null;
numItems--;
return removedItem;
}
// this method removes item, but does not shrink array
public Item deleteItem (int index) {
if ( (index < 0) || (index > (numItems-1) ) )
throw new TFNodeException();
Item removedItem = nodeItems[index];
nodeItems[index] = null;
numItems--;
return removedItem;
}
// replaces Item at index with newItem, returning the old Item
public Item replaceItem (int index, Item newItem) {
if ( (index < 0) || (index > (numItems-1) ) )
throw new TFNodeException();
Item returnItem = nodeItems[index];
nodeItems[index] = newItem;
return returnItem;
}
public TFNode getChild (int index) {
if ( (index < 0) || (index > (MAX_ITEMS+1)) )
throw new TFNodeException();
return nodeChildren[index];
}
public void setChild (int index, TFNode child) {
if ( (index < 0) || (index > (MAX_ITEMS+1)) )
throw new TFNodeException();
nodeChildren[index] = child;
}
}

View File

@@ -0,0 +1,20 @@
package termproject;
/**
* Title: Term Project 2-4 Trees
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class TFNodeException extends RuntimeException {
public TFNodeException() {
super ("Problem with TFNode");
}
public TFNodeException(String errorMsg) {
super (errorMsg);
}
}

View File

@@ -0,0 +1,26 @@
/*
* This class contains JUnit tests to run against the ArrayQueue class.
@author Joel Beckmeyer
@version 1.0
File: Filename.java
Created: YYYY-MM-DD
Description: This class contains tests for correct exception handling in
several different cases, circular array wrapping, dynamic resizing, correct
method returns, and correct (expected) queue behavior.
*/
package termproject;
/**
*
* @author joel
*/
public class Termproject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}

View File

@@ -0,0 +1,236 @@
package termproject;
/**
* Title: Term Project 2-4 Trees
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class TwoFourTree
implements Dictionary {
private Comparator treeComp;
private int size = 0;
private TFNode treeRoot = null;
public TwoFourTree(Comparator comp) {
treeComp = comp;
}
private TFNode root() {
return treeRoot;
}
private void setRoot(TFNode root) {
treeRoot = root;
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
/**
* Searches dictionary to determine if key is present
* @param key to be searched for
* @return object corresponding to key; null if not found
*/
public Object findElement(Object key) {
return null;
}
/**
* Inserts provided element into the Dictionary
* @param key of object to be inserted
* @param element to be inserted
*/
public void insertElement(Object key, Object element) {
}
/**
* Searches dictionary to determine if key is present, then
* removes and returns corresponding object
* @param key of data to be removed
* @return object corresponding to key
* @exception ElementNotFoundException if the key is not in dictionary
*/
public Object removeElement(Object key) throws ElementNotFoundException {
return null;
}
public static void main(String[] args) {
Comparator myComp = new IntegerComparator();
TwoFourTree myTree = new TwoFourTree(myComp);
Integer myInt1 = new Integer(47);
myTree.insertElement(myInt1, myInt1);
Integer myInt2 = new Integer(83);
myTree.insertElement(myInt2, myInt2);
Integer myInt3 = new Integer(22);
myTree.insertElement(myInt3, myInt3);
Integer myInt4 = new Integer(16);
myTree.insertElement(myInt4, myInt4);
Integer myInt5 = new Integer(49);
myTree.insertElement(myInt5, myInt5);
Integer myInt6 = new Integer(100);
myTree.insertElement(myInt6, myInt6);
Integer myInt7 = new Integer(38);
myTree.insertElement(myInt7, myInt7);
Integer myInt8 = new Integer(3);
myTree.insertElement(myInt8, myInt8);
Integer myInt9 = new Integer(53);
myTree.insertElement(myInt9, myInt9);
Integer myInt10 = new Integer(66);
myTree.insertElement(myInt10, myInt10);
Integer myInt11 = new Integer(19);
myTree.insertElement(myInt11, myInt11);
Integer myInt12 = new Integer(23);
myTree.insertElement(myInt12, myInt12);
Integer myInt13 = new Integer(24);
myTree.insertElement(myInt13, myInt13);
Integer myInt14 = new Integer(88);
myTree.insertElement(myInt14, myInt14);
Integer myInt15 = new Integer(1);
myTree.insertElement(myInt15, myInt15);
Integer myInt16 = new Integer(97);
myTree.insertElement(myInt16, myInt16);
Integer myInt17 = new Integer(94);
myTree.insertElement(myInt17, myInt17);
Integer myInt18 = new Integer(35);
myTree.insertElement(myInt18, myInt18);
Integer myInt19 = new Integer(51);
myTree.insertElement(myInt19, myInt19);
myTree.printAllElements();
System.out.println("done");
myTree = new TwoFourTree(myComp);
final int TEST_SIZE = 10000;
for (int i = 0; i < TEST_SIZE; i++) {
myTree.insertElement(new Integer(i), new Integer(i));
// myTree.printAllElements();
// myTree.checkTree();
}
System.out.println("removing");
for (int i = 0; i < TEST_SIZE; i++) {
int out = (Integer) myTree.removeElement(new Integer(i));
if (out != i) {
throw new TwoFourTreeException("main: wrong element removed");
}
if (i > TEST_SIZE - 15) {
myTree.printAllElements();
}
}
System.out.println("done");
}
public void printAllElements() {
int indent = 0;
if (root() == null) {
System.out.println("The tree is empty");
}
else {
printTree(root(), indent);
}
}
public void printTree(TFNode start, int indent) {
if (start == null) {
return;
}
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
printTFNode(start);
indent += 4;
int numChildren = start.getNumItems() + 1;
for (int i = 0; i < numChildren; i++) {
printTree(start.getChild(i), indent);
}
}
public void printTFNode(TFNode node) {
int numItems = node.getNumItems();
for (int i = 0; i < numItems; i++) {
System.out.print(((Item) node.getItem(i)).element() + " ");
}
System.out.println();
}
// checks if tree is properly hooked up, i.e., children point to parents
public void checkTree() {
checkTreeFromNode(treeRoot);
}
private void checkTreeFromNode(TFNode start) {
if (start == null) {
return;
}
if (start.getParent() != null) {
TFNode parent = start.getParent();
int childIndex = 0;
for (childIndex = 0; childIndex <= parent.getNumItems(); childIndex++) {
if (parent.getChild(childIndex) == start) {
break;
}
}
// if child wasn't found, print problem
if (childIndex > parent.getNumItems()) {
System.out.println("Child to parent confusion");
printTFNode(start);
}
}
if (start.getChild(0) != null) {
for (int childIndex = 0; childIndex <= start.getNumItems(); childIndex++) {
if (start.getChild(childIndex) == null) {
System.out.println("Mixed null and non-null children");
printTFNode(start);
}
else {
if (start.getChild(childIndex).getParent() != start) {
System.out.println("Parent to child confusion");
printTFNode(start);
}
for (int i = childIndex - 1; i >= 0; i--) {
if (start.getChild(i) == start.getChild(childIndex)) {
System.out.println("Duplicate children of node");
printTFNode(start);
}
}
}
}
}
int numChildren = start.getNumItems() + 1;
for (int childIndex = 0; childIndex < numChildren; childIndex++) {
checkTreeFromNode(start.getChild(childIndex));
}
}
}

View File

@@ -0,0 +1,21 @@
package termproject;
/**
* Title: Term Project 2-4 Trees
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
public class TwoFourTreeException extends RuntimeException {
public TwoFourTreeException() {
super ("Problem with TwoFourTree");
}
public TwoFourTreeException(String errorMsg) {
super (errorMsg);
}
}