commit 0748930c0fd24d13cf573431cca1e6207e54abde Author: Joel Beckmeyer Date: Fri Dec 8 14:47:48 2017 -0500 intial commit diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..5ea87b8 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project termproject. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..0e715d7 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..f5eec5a --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=05f8d30b +build.xml.script.CRC32=43cf3d72 +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=05f8d30b +nbproject/build-impl.xml.script.CRC32=109a6610 +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..df6b8d4 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,74 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/termproject.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=termproject.Termproject +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..9bb314d --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + termproject + + + + + + + + + diff --git a/src/termproject/Comparator.java b/src/termproject/Comparator.java new file mode 100644 index 0000000..de18510 --- /dev/null +++ b/src/termproject/Comparator.java @@ -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); +} \ No newline at end of file diff --git a/src/termproject/Dictionary.java b/src/termproject/Dictionary.java new file mode 100644 index 0000000..280126e --- /dev/null +++ b/src/termproject/Dictionary.java @@ -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; +} \ No newline at end of file diff --git a/src/termproject/ElementNotFoundException.java b/src/termproject/ElementNotFoundException.java new file mode 100644 index 0000000..9dde180 --- /dev/null +++ b/src/termproject/ElementNotFoundException.java @@ -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); + } +} \ No newline at end of file diff --git a/src/termproject/IntegerComparator.java b/src/termproject/IntegerComparator.java new file mode 100644 index 0000000..c70975b --- /dev/null +++ b/src/termproject/IntegerComparator.java @@ -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; + } + } +} \ No newline at end of file diff --git a/src/termproject/InvalidIntegerException.java b/src/termproject/InvalidIntegerException.java new file mode 100644 index 0000000..844809a --- /dev/null +++ b/src/termproject/InvalidIntegerException.java @@ -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); + } +} \ No newline at end of file diff --git a/src/termproject/Item.java b/src/termproject/Item.java new file mode 100644 index 0000000..691d414 --- /dev/null +++ b/src/termproject/Item.java @@ -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; + } +} \ No newline at end of file diff --git a/src/termproject/TFNode.java b/src/termproject/TFNode.java new file mode 100644 index 0000000..8c8a58e --- /dev/null +++ b/src/termproject/TFNode.java @@ -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; + } +} \ No newline at end of file diff --git a/src/termproject/TFNodeException.java b/src/termproject/TFNodeException.java new file mode 100644 index 0000000..b477ddc --- /dev/null +++ b/src/termproject/TFNodeException.java @@ -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); + } +} diff --git a/src/termproject/Termproject.java b/src/termproject/Termproject.java new file mode 100644 index 0000000..caeac46 --- /dev/null +++ b/src/termproject/Termproject.java @@ -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 + } + +} diff --git a/src/termproject/TwoFourTree.java b/src/termproject/TwoFourTree.java new file mode 100644 index 0000000..e1fb89f --- /dev/null +++ b/src/termproject/TwoFourTree.java @@ -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)); + } + + } +} diff --git a/src/termproject/TwoFourTreeException.java b/src/termproject/TwoFourTreeException.java new file mode 100644 index 0000000..fb11205 --- /dev/null +++ b/src/termproject/TwoFourTreeException.java @@ -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); + } +} \ No newline at end of file