intial commit
This commit is contained in:
commit
0748930c0f
73
build.xml
Normal file
73
build.xml
Normal file
@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- You may freely edit this file. See commented blocks below for -->
|
||||
<!-- some examples of how to customize the build. -->
|
||||
<!-- (If you delete it and reopen the project it will be recreated.) -->
|
||||
<!-- By default, only the Clean and Build commands use this build script. -->
|
||||
<!-- Commands such as Run, Debug, and Test only use this build script if -->
|
||||
<!-- the Compile on Save feature is turned off for the project. -->
|
||||
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
|
||||
<!-- in the project's Project Properties dialog box.-->
|
||||
<project name="termproject" default="default" basedir=".">
|
||||
<description>Builds, tests, and runs the project termproject.</description>
|
||||
<import file="nbproject/build-impl.xml"/>
|
||||
<!--
|
||||
|
||||
There exist several targets which are by default empty and which can be
|
||||
used for execution of your tasks. These targets are usually executed
|
||||
before and after some main targets. They are:
|
||||
|
||||
-pre-init: called before initialization of project properties
|
||||
-post-init: called after initialization of project properties
|
||||
-pre-compile: called before javac compilation
|
||||
-post-compile: called after javac compilation
|
||||
-pre-compile-single: called before javac compilation of single file
|
||||
-post-compile-single: called after javac compilation of single file
|
||||
-pre-compile-test: called before javac compilation of JUnit tests
|
||||
-post-compile-test: called after javac compilation of JUnit tests
|
||||
-pre-compile-test-single: called before javac compilation of single JUnit test
|
||||
-post-compile-test-single: called after javac compilation of single JUunit test
|
||||
-pre-jar: called before JAR building
|
||||
-post-jar: called after JAR building
|
||||
-post-clean: called after cleaning build products
|
||||
|
||||
(Targets beginning with '-' are not intended to be called on their own.)
|
||||
|
||||
Example of inserting an obfuscator after compilation could look like this:
|
||||
|
||||
<target name="-post-compile">
|
||||
<obfuscate>
|
||||
<fileset dir="${build.classes.dir}"/>
|
||||
</obfuscate>
|
||||
</target>
|
||||
|
||||
For list of available properties check the imported
|
||||
nbproject/build-impl.xml file.
|
||||
|
||||
|
||||
Another way to customize the build is by overriding existing main targets.
|
||||
The targets of interest are:
|
||||
|
||||
-init-macrodef-javac: defines macro for javac compilation
|
||||
-init-macrodef-junit: defines macro for junit execution
|
||||
-init-macrodef-debug: defines macro for class debugging
|
||||
-init-macrodef-java: defines macro for class execution
|
||||
-do-jar: JAR building
|
||||
run: execution of project
|
||||
-javadoc-build: Javadoc generation
|
||||
test-report: JUnit report generation
|
||||
|
||||
An example of overriding the target for project execution could look like this:
|
||||
|
||||
<target name="run" depends="termproject-impl.jar">
|
||||
<exec dir="bin" executable="launcher.exe">
|
||||
<arg file="${dist.jar}"/>
|
||||
</exec>
|
||||
</target>
|
||||
|
||||
Notice that the overridden target depends on the jar target and not only on
|
||||
the compile target as the regular run target does. Again, for a list of available
|
||||
properties which you can use, check the target you are overriding in the
|
||||
nbproject/build-impl.xml file.
|
||||
|
||||
-->
|
||||
</project>
|
3
manifest.mf
Normal file
3
manifest.mf
Normal file
@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
1420
nbproject/build-impl.xml
Normal file
1420
nbproject/build-impl.xml
Normal file
File diff suppressed because it is too large
Load Diff
8
nbproject/genfiles.properties
Normal file
8
nbproject/genfiles.properties
Normal file
@ -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
|
74
nbproject/project.properties
Normal file
74
nbproject/project.properties
Normal file
@ -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
|
15
nbproject/project.xml
Normal file
15
nbproject/project.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||
<type>org.netbeans.modules.java.j2seproject</type>
|
||||
<configuration>
|
||||
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
|
||||
<name>termproject</name>
|
||||
<source-roots>
|
||||
<root id="src.dir"/>
|
||||
</source-roots>
|
||||
<test-roots>
|
||||
<root id="test.src.dir"/>
|
||||
</test-roots>
|
||||
</data>
|
||||
</configuration>
|
||||
</project>
|
25
src/termproject/Comparator.java
Normal file
25
src/termproject/Comparator.java
Normal 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);
|
||||
}
|
42
src/termproject/Dictionary.java
Normal file
42
src/termproject/Dictionary.java
Normal 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;
|
||||
}
|
21
src/termproject/ElementNotFoundException.java
Normal file
21
src/termproject/ElementNotFoundException.java
Normal 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);
|
||||
}
|
||||
}
|
56
src/termproject/IntegerComparator.java
Normal file
56
src/termproject/IntegerComparator.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
17
src/termproject/InvalidIntegerException.java
Normal file
17
src/termproject/InvalidIntegerException.java
Normal 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
38
src/termproject/Item.java
Normal 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
136
src/termproject/TFNode.java
Normal 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;
|
||||
}
|
||||
}
|
20
src/termproject/TFNodeException.java
Normal file
20
src/termproject/TFNodeException.java
Normal 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);
|
||||
}
|
||||
}
|
26
src/termproject/Termproject.java
Normal file
26
src/termproject/Termproject.java
Normal 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
|
||||
}
|
||||
|
||||
}
|
236
src/termproject/TwoFourTree.java
Normal file
236
src/termproject/TwoFourTree.java
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
21
src/termproject/TwoFourTreeException.java
Normal file
21
src/termproject/TwoFourTreeException.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user