some old college stuff
This commit is contained in:
parent
0a8075974c
commit
f44e642b3a
BIN
ss2010/gdi2/exercise/GDI_H_12_6.pdf
Normal file
BIN
ss2010/gdi2/exercise/GDI_H_12_6.pdf
Normal file
Binary file not shown.
BIN
ss2010/gdi2/exercise/uebung-12.pdf
Normal file
BIN
ss2010/gdi2/exercise/uebung-12.pdf
Normal file
Binary file not shown.
BIN
ss2010/gdi2/java/P2.7z
Normal file
BIN
ss2010/gdi2/java/P2.7z
Normal file
Binary file not shown.
BIN
ss2010/gdi2/java/Project4.7z
Normal file
BIN
ss2010/gdi2/java/Project4.7z
Normal file
Binary file not shown.
BIN
ss2010/gdi2/java/Project4.zip
Normal file
BIN
ss2010/gdi2/java/Project4.zip
Normal file
Binary file not shown.
7
ss2010/gdi2/java/Project4/.classpath
Normal file
7
ss2010/gdi2/java/Project4/.classpath
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
ss2010/gdi2/java/Project4/.project
Normal file
17
ss2010/gdi2/java/Project4/.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Project4</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@ -0,0 +1,12 @@
|
||||
#Thu Jun 24 12:30:46 CEST 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
201
ss2010/gdi2/java/Project4/src/GDI2BTree.java
Normal file
201
ss2010/gdi2/java/Project4/src/GDI2BTree.java
Normal file
@ -0,0 +1,201 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class represents the GDI2 B-Tree
|
||||
*/
|
||||
|
||||
public class GDI2BTree
|
||||
{
|
||||
private GDI2BTreeNode root; // the root of the B-Tree
|
||||
private int order; // the order (=k) of the B-Tree
|
||||
|
||||
// TODO: Specify other private data, variables
|
||||
// TODO: Implement other necessary methods, e.g. a method to traverse the B-Tree
|
||||
|
||||
/**
|
||||
* This method inserts into the B-Tree the given key/entry (String)
|
||||
* @param entry the value to add into the tree
|
||||
* @return true - if successful, false otherwise
|
||||
*/
|
||||
public boolean addData(String entry) {
|
||||
if(root == null){
|
||||
return false;
|
||||
}
|
||||
|
||||
GDI2BTreeNode newroot = root.add(entry,order);
|
||||
|
||||
if(newroot == null)
|
||||
{
|
||||
return false;
|
||||
} else {
|
||||
root = newroot;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method deletes from the B-Tree the given key/entry (String)
|
||||
* @param entry the value to delete from the tree
|
||||
* @return true - if successful, false otherwise
|
||||
*/
|
||||
public boolean delData(String entry) {
|
||||
if(root == null){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(root.del(entry,order) == null){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given entry/key can be found in the B-Tree
|
||||
* @param entry The entry/key which is to be found
|
||||
* @return the B-Tree node containing the given key/entry (String), otherwise null
|
||||
*/
|
||||
public GDI2BTreeNode findData(String entry) {
|
||||
if(root == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
return root.find(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the B-Tree is empty
|
||||
* @return true - if B-Tree empty, otherwise false
|
||||
*/
|
||||
public boolean isBTreeEmpty(){
|
||||
return !(root == null || root.hasKeys());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method prints the B-Tree
|
||||
*/
|
||||
public void printBTree()
|
||||
{
|
||||
if(isBTreeEmpty())
|
||||
{
|
||||
System.out.println("B-Tree is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println( "GDI2 B-Tree: order = " +
|
||||
this.getOrder() +
|
||||
", keys = " +
|
||||
root.countKeys() +
|
||||
", height = " +
|
||||
root.getTotalDepth()+1);
|
||||
System.out.println("------------------------------------------------");
|
||||
|
||||
ArrayList<GDI2BTreeNode> list = new ArrayList<GDI2BTreeNode>();
|
||||
list.add(root);
|
||||
int lastdepth = 0;
|
||||
|
||||
while(!list.isEmpty())
|
||||
{
|
||||
if(list.get(0).getDepth() > lastdepth)
|
||||
{
|
||||
System.out.println("\n\n");
|
||||
}
|
||||
|
||||
String element = list.get(0).toString();
|
||||
|
||||
//order - number of data = how many times should '#' be inserted
|
||||
for(int len = order - ((element.length()+1) / 2); len > 0; len--)
|
||||
{
|
||||
element += ",#";
|
||||
}
|
||||
|
||||
element = "[" + element + "]";
|
||||
|
||||
System.out.println(element + " ");
|
||||
list.addAll(list.get(0).getChildren());
|
||||
list.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method helps to check the correctness
|
||||
* of the B-Tree after adding/deleting data.
|
||||
*
|
||||
* You can and should extend/modify this method
|
||||
* to check the correctness of your B-Tree implementation.
|
||||
* You are allowed to change the signature
|
||||
* of this methods according to your needs.
|
||||
*
|
||||
* We will use a similar method
|
||||
* correctnessCheckPrivate(...) to test your implementation.
|
||||
*/
|
||||
public void correctnessCheckPublic()
|
||||
{
|
||||
// OPTIONAL TODO: extend/modify this method
|
||||
|
||||
// the following checks are only some examples
|
||||
|
||||
// check if the root has data
|
||||
if (getRoot().hasKeys())
|
||||
{
|
||||
// check if the number of children is correct (root not being a leaf)
|
||||
if (!getRoot().isLeaf() && (getRoot().getData().size() + 1 < getRoot().getChildren().size()))
|
||||
{
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.BTREE,
|
||||
"Too many children.");
|
||||
}
|
||||
// check if the number of keys is conform to the given order
|
||||
if (getRoot().getData().size() > 2 * getOrder()){
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.BTREE,
|
||||
"Too many keys - the given order is violated.");
|
||||
}
|
||||
}
|
||||
// check if the root has children
|
||||
if (getRoot().hasChildren())
|
||||
{
|
||||
// check if root is a leaf
|
||||
if (getRoot().isLeaf()){
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.BTREE,
|
||||
"It should not be possible for a leaf to have children.");
|
||||
}
|
||||
// check if the correct number of children is provided
|
||||
if (getRoot().getData().size() >= getRoot().getChildren().size()){
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.BTREE,
|
||||
"Too few children.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param root the root to set
|
||||
*/
|
||||
public void setRoot(GDI2BTreeNode root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the root
|
||||
*/
|
||||
public GDI2BTreeNode getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param order the order to set
|
||||
*/
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the order
|
||||
*/
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
}
|
||||
128
ss2010/gdi2/java/Project4/src/GDI2BTreeDataReader.java
Normal file
128
ss2010/gdi2/java/Project4/src/GDI2BTreeDataReader.java
Normal file
@ -0,0 +1,128 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* GDDI2 B-Tree data reader
|
||||
*/
|
||||
|
||||
public class GDI2BTreeDataReader
|
||||
{
|
||||
private BufferedReader reader;
|
||||
|
||||
/**
|
||||
* Sets the reader for the GDI2 B-Tree data file
|
||||
*
|
||||
* @param dataFile the file where the B-Tree data is given
|
||||
* @throws GDI2BTreeFileFormatException
|
||||
*/
|
||||
public GDI2BTreeDataReader(String dataFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
setReader(new BufferedReader(new FileReader(dataFile)));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"IOException raised while reading the file.");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This method reads the next line from the file
|
||||
*
|
||||
* @return a string or null if the end of the stream is reached
|
||||
* @throws GDI2BTreeFileFormatException
|
||||
*/
|
||||
public String[] getNextLine()
|
||||
{
|
||||
// TODO: extend this method with further checks
|
||||
String line = null;
|
||||
try
|
||||
{
|
||||
line = reader.readLine();
|
||||
if (line == null)
|
||||
return null;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"IOException raised while reading the file.");
|
||||
}
|
||||
line = line.trim();
|
||||
String[] commandLine = line.split(" ");
|
||||
// checking if the line has the correct format (<command> <key>)
|
||||
if (commandLine.length != 2)
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"Wrong command/key line!");
|
||||
// checking the commands
|
||||
if (!(commandLine[0].equals("add") || commandLine[0].equals("del")))
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"Unknown command!");
|
||||
return commandLine;
|
||||
}
|
||||
/**
|
||||
* Reads the order from the file
|
||||
* @return the order for the B-Tree
|
||||
* @throws GDI2BTreeFileFormatException
|
||||
*/
|
||||
public int readOrder()
|
||||
{
|
||||
String line = "";
|
||||
int order = -1;
|
||||
try
|
||||
{
|
||||
line = reader.readLine();
|
||||
line = line.trim();
|
||||
// get the order of the B-Tree
|
||||
order = Integer.parseInt(line);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"IOException raised while reading file.");
|
||||
}
|
||||
catch (NumberFormatException nfe)
|
||||
{
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"NumberFormatException raised while reading order.");
|
||||
}
|
||||
return order;
|
||||
}
|
||||
/**
|
||||
* This method closes the data reader
|
||||
*
|
||||
* @throws GDI2BTreeFileFormatException
|
||||
*/
|
||||
public void closeGDI2BTreeDataReader()
|
||||
{
|
||||
try
|
||||
{
|
||||
getReader().close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new GDI2BTreeFileFormatException(
|
||||
GDI2BTreeFileFormatException.GDI2TypeOfException.FILE,
|
||||
"IOException raised while closing the file.");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param reader the reader to set
|
||||
*/
|
||||
public void setReader(BufferedReader reader) {
|
||||
this.reader = reader;
|
||||
}
|
||||
/**
|
||||
* @return the reader
|
||||
*/
|
||||
public BufferedReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
|
||||
|
||||
/**
|
||||
* GDI2 B-Tree and file format exception
|
||||
*/
|
||||
|
||||
public class GDI2BTreeFileFormatException extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
static public enum GDI2TypeOfException {
|
||||
/**
|
||||
* File format is incorrect
|
||||
*/
|
||||
FILE,
|
||||
|
||||
/**
|
||||
* An error regarding the B-Tree
|
||||
*/
|
||||
BTREE
|
||||
}
|
||||
/**
|
||||
* The error message
|
||||
*/
|
||||
protected String errorMessage;
|
||||
/**
|
||||
* The error type
|
||||
*/
|
||||
protected GDI2TypeOfException errorType;
|
||||
/**
|
||||
* Constructor
|
||||
* @param s The message string
|
||||
*/
|
||||
public GDI2BTreeFileFormatException(String s) {
|
||||
super(s);
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
* @param type The type of the error (FILE or BTREE)
|
||||
* @param message The error message
|
||||
*/
|
||||
public GDI2BTreeFileFormatException(GDI2TypeOfException type, String message)
|
||||
{
|
||||
errorMessage = message;
|
||||
errorType = type;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Throwable#getMessage()
|
||||
*/
|
||||
@Override
|
||||
public String getMessage()
|
||||
{
|
||||
return "GDI2 B-Tree Error: " + errorMessage;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Throwable#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String message = new String();
|
||||
if (errorType == GDI2TypeOfException.FILE)
|
||||
message = "FileFormatException";
|
||||
else if (errorType == GDI2TypeOfException.BTREE)
|
||||
message = "BTreeException";
|
||||
return "GDI2 B-Tree Error: " + message + " : " + errorMessage;
|
||||
}
|
||||
}
|
||||
15
ss2010/gdi2/java/Project4/src/GDI2BTreeMain.java
Normal file
15
ss2010/gdi2/java/Project4/src/GDI2BTreeMain.java
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* The Main class
|
||||
*/
|
||||
|
||||
public class GDI2BTreeMain
|
||||
{
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new GDI2BTree();
|
||||
// TODO: add the needed code to work with your implementation
|
||||
}
|
||||
}
|
||||
321
ss2010/gdi2/java/Project4/src/GDI2BTreeNode.java
Normal file
321
ss2010/gdi2/java/Project4/src/GDI2BTreeNode.java
Normal file
@ -0,0 +1,321 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class represents the node of the GDI2 B-Tree
|
||||
*/
|
||||
public class GDI2BTreeNode
|
||||
{
|
||||
private GDI2BTreeNode predecessor; // the parent node
|
||||
private List<String> data; // the list of keys (the data)
|
||||
private List<GDI2BTreeNode> children; // the list of children nodes
|
||||
private boolean leaf; // leaf or no leaf
|
||||
private int depth = 0; // Depth of this node
|
||||
|
||||
// TODO: implement the getter/setter methods, e.g., getPredecessor() and setPredecessor()
|
||||
|
||||
// TODO: Implement other necessary methods, e.g., a method to sort the keys
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param parent the predecessor node (if the current node is root, then parent = null)
|
||||
*/
|
||||
public GDI2BTreeNode(GDI2BTreeNode parent){
|
||||
this.setPredecessor(parent);
|
||||
data = new ArrayList<String>();
|
||||
children = new ArrayList<GDI2BTreeNode>();
|
||||
leaf = true;
|
||||
|
||||
if(parent == null)
|
||||
{
|
||||
setDepth(0);
|
||||
} else
|
||||
{
|
||||
setDepth(parent.getDepth() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void setDepth(int depth)
|
||||
{
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the node has keys/data
|
||||
* @return true - if the node has keys/data, otherwise false
|
||||
*/
|
||||
public boolean hasKeys()
|
||||
{
|
||||
return (data.size() > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the node has children
|
||||
* @return true - if the node has children, otherwise false
|
||||
*/
|
||||
public boolean hasChildren()
|
||||
{
|
||||
return (children.size() > 0);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result = "";
|
||||
|
||||
for(int i= 0; i< data.size(); i++)
|
||||
{
|
||||
result += data.get(i) + ",";
|
||||
}
|
||||
|
||||
//remove last comma
|
||||
return result.substring(0,result.length()-1);
|
||||
}
|
||||
|
||||
public int countKeys()
|
||||
{
|
||||
int count = data.size();
|
||||
|
||||
for(int i = 0; i< children.size(); i++)
|
||||
{
|
||||
count += children.get(i).countKeys();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public int getTotalDepth()
|
||||
{
|
||||
if(isLeaf())
|
||||
{
|
||||
return depth;
|
||||
}
|
||||
|
||||
return children.get(0).getTotalDepth();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the data
|
||||
*/
|
||||
public List<String> getData() {
|
||||
return data;
|
||||
}
|
||||
/**
|
||||
* @param data the data to set
|
||||
*/
|
||||
public void setData(List<String> data) {
|
||||
this.data = data;
|
||||
}
|
||||
/**
|
||||
* @return the children
|
||||
*/
|
||||
public List<GDI2BTreeNode> getChildren() {
|
||||
return children;
|
||||
}
|
||||
/**
|
||||
* @param children the children to set
|
||||
*/
|
||||
public void setChildren(List<GDI2BTreeNode> children) {
|
||||
this.children = children;
|
||||
}
|
||||
/**
|
||||
* Checks if the node is a leaf
|
||||
* @return true - if the node is a leaf, otherwise false
|
||||
*/
|
||||
public boolean isLeaf()
|
||||
{
|
||||
return this.leaf;
|
||||
}
|
||||
/**
|
||||
* @param leaf the leaf to set
|
||||
*/
|
||||
public void setLeaf(boolean leaf) {
|
||||
this.leaf = leaf;
|
||||
}
|
||||
|
||||
private void setPredecessor(GDI2BTreeNode parent) {
|
||||
this.predecessor = parent;
|
||||
}
|
||||
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
private GDI2BTreeNode split(String entry, int order, GDI2BTreeNode l, GDI2BTreeNode r){
|
||||
//insert sorted
|
||||
for(int i=0; i<data.size();i++)
|
||||
{
|
||||
int compared = data.get(i).compareTo(entry);
|
||||
|
||||
if(compared <= 0)
|
||||
{
|
||||
data.add(i, entry);
|
||||
children.add(i,l);
|
||||
children.add(i+1,r);
|
||||
leaf = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(data.size() <= 2*order)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean newroot = false;
|
||||
|
||||
if(predecessor == null)
|
||||
{
|
||||
predecessor = new GDI2BTreeNode(null);
|
||||
|
||||
newroot = true;
|
||||
}
|
||||
|
||||
GDI2BTreeNode left = new GDI2BTreeNode(predecessor);
|
||||
GDI2BTreeNode right = new GDI2BTreeNode(predecessor);
|
||||
|
||||
left.setData(data.subList(0, order-1));
|
||||
right.setData(data.subList(order+1, order-1));
|
||||
left.setChildren(children.subList(0, order-1));
|
||||
right.setChildren(children.subList(order+1, order-1));
|
||||
|
||||
predecessor.split(data.get(order), order, left, right);
|
||||
|
||||
if(newroot)
|
||||
{
|
||||
return predecessor;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public GDI2BTreeNode add(String entry, int order) {
|
||||
|
||||
//add to this node?
|
||||
if(data.size() < 2*order)
|
||||
{
|
||||
//insert sorted
|
||||
for(int i=0; i<data.size();i++)
|
||||
{
|
||||
int compared = data.get(i).compareTo(entry);
|
||||
|
||||
//entry already in list?
|
||||
if(compared == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if(compared < 0)
|
||||
{
|
||||
data.add(i, entry);
|
||||
children.add(i,null);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
data.add(entry);
|
||||
children.add(data.size()-1,null);
|
||||
return this;
|
||||
} else {//add to children
|
||||
if(isLeaf()){
|
||||
return split(entry, order,null,null);
|
||||
} else {
|
||||
|
||||
for(int i= 0; i< data.size(); i++){
|
||||
|
||||
int compared = data.get(i).compareTo(entry);
|
||||
|
||||
if(compared == 0){
|
||||
return null;
|
||||
}
|
||||
|
||||
if(compared < 0){
|
||||
if(children.size() > i) {
|
||||
return children.get(i).add(entry,order);
|
||||
}
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public GDI2BTreeNode find(String entry) {
|
||||
//TODO binary search
|
||||
|
||||
for(int i= 0; i< data.size(); i++)
|
||||
{
|
||||
int compared = data.get(i).compareTo(entry);
|
||||
|
||||
if(compared == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if(compared < 0) {
|
||||
if(children.size() > i)
|
||||
{
|
||||
return children.get(i).find(entry);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//last child
|
||||
if( children.size() == data.size()+1){
|
||||
return children.get(data.size()+1).find(entry);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public GDI2BTreeNode del(String entry,int order) {
|
||||
|
||||
GDI2BTreeNode node = find(entry);
|
||||
|
||||
if(node == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
for(int i= 0; i < node.getData().size(); i++){
|
||||
if(node.getData().get(i).compareTo(entry) == 0)
|
||||
{
|
||||
node.getData().remove(i);
|
||||
//TODO AJUST CHILDREN
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(data.size() >= order)
|
||||
{
|
||||
//TODO falsch - nicht nur in den blättern löschen
|
||||
return null;
|
||||
}
|
||||
|
||||
//unterlauf
|
||||
/**
|
||||
* predecessor.getMyLeft()
|
||||
*
|
||||
* if myleft == null
|
||||
* predecessor.getMyRight()
|
||||
*
|
||||
* zähle schlüssen in data und dem gefundenen
|
||||
* ausgleichsschlüssel falls >= 2*order alles ok
|
||||
* -> schlüssel verschieben und aufteilen.
|
||||
*
|
||||
* -> Andernfalls: knoten löschen und aufteilen
|
||||
*
|
||||
* UNTERSCHEIDE: Blatt/Innerer Knoten?!
|
||||
*
|
||||
*/
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
5
ss2010/gdi2/java/Project4/test_0
Normal file
5
ss2010/gdi2/java/Project4/test_0
Normal file
@ -0,0 +1,5 @@
|
||||
3 4
|
||||
add 1 3
|
||||
add 2
|
||||
add 3
|
||||
del 3
|
||||
5
ss2010/gdi2/java/Project4/test_00
Normal file
5
ss2010/gdi2/java/Project4/test_00
Normal file
@ -0,0 +1,5 @@
|
||||
3
|
||||
add 1 3
|
||||
add 2
|
||||
add 3
|
||||
del 3
|
||||
5
ss2010/gdi2/java/Project4/test_01
Normal file
5
ss2010/gdi2/java/Project4/test_01
Normal file
@ -0,0 +1,5 @@
|
||||
order
|
||||
add 1
|
||||
add 2
|
||||
add 3
|
||||
del 3
|
||||
6
ss2010/gdi2/java/Project4/test_02
Normal file
6
ss2010/gdi2/java/Project4/test_02
Normal file
@ -0,0 +1,6 @@
|
||||
3
|
||||
add 1
|
||||
add 2
|
||||
add 3
|
||||
del 3
|
||||
mov 5
|
||||
5
ss2010/gdi2/java/Project4/test_03
Normal file
5
ss2010/gdi2/java/Project4/test_03
Normal file
@ -0,0 +1,5 @@
|
||||
1
|
||||
add 1
|
||||
add 2
|
||||
add 3
|
||||
del 3
|
||||
7
ss2010/gdi2/java/Project4/test_04
Normal file
7
ss2010/gdi2/java/Project4/test_04
Normal file
@ -0,0 +1,7 @@
|
||||
2
|
||||
add G
|
||||
add D
|
||||
add I
|
||||
del G
|
||||
del D
|
||||
add CS
|
||||
90
ss2010/gdi2/java/Project4/tests/GDI2BTreeTest.java
Normal file
90
ss2010/gdi2/java/Project4/tests/GDI2BTreeTest.java
Normal file
@ -0,0 +1,90 @@
|
||||
import java.util.ArrayList;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Test Cases
|
||||
*/
|
||||
|
||||
public class GDI2BTreeTest
|
||||
{
|
||||
/*
|
||||
* Write your own tests to thoroughly check your implementation.
|
||||
* Passing the following tests does not mean that your implementation is 100% correct.
|
||||
*
|
||||
*/
|
||||
@Test(expected=GDI2BTreeFileFormatException.class)
|
||||
public void treeTest0()
|
||||
{
|
||||
// read data
|
||||
GDI2BTreeDataReader dataReader = new GDI2BTreeDataReader("test_03");
|
||||
// create tree and root
|
||||
GDI2BTree tree = new GDI2BTree();
|
||||
GDI2BTreeNode root = new GDI2BTreeNode(null);
|
||||
// initialise root
|
||||
root.setData(new ArrayList<String>());
|
||||
root.setChildren(new ArrayList<GDI2BTreeNode>());
|
||||
// read order
|
||||
int order = dataReader.readOrder();
|
||||
// initialise tree
|
||||
tree.setOrder(order);
|
||||
tree.setRoot(root);
|
||||
|
||||
String[] commandKey;
|
||||
while ((commandKey = dataReader.getNextLine()) != null)
|
||||
{
|
||||
if (commandKey[0].equals("add"))
|
||||
{
|
||||
root.getData().add(commandKey[1]); // simulating addData()
|
||||
System.out.println("Add: " + commandKey[1]);
|
||||
tree.correctnessCheckPublic(); // check the correctness of the B-Tree
|
||||
}
|
||||
else if (commandKey[0].equals("del"))
|
||||
{
|
||||
System.out.println("Del: " + commandKey[1]);
|
||||
}
|
||||
}
|
||||
dataReader.closeGDI2BTreeDataReader(); // close the stream
|
||||
}
|
||||
@Test(expected=GDI2BTreeFileFormatException.class)
|
||||
public void treeTest1()
|
||||
{
|
||||
// read data
|
||||
GDI2BTreeDataReader dataReader = new GDI2BTreeDataReader("test_04");
|
||||
// create tree and root
|
||||
GDI2BTree tree = new GDI2BTree();
|
||||
GDI2BTreeNode root = new GDI2BTreeNode(null);
|
||||
// initialise root
|
||||
root.setData(new ArrayList<String>());
|
||||
root.setChildren(new ArrayList<GDI2BTreeNode>());
|
||||
// read order
|
||||
int order = dataReader.readOrder();
|
||||
// initialise tree
|
||||
tree.setOrder(order);
|
||||
tree.setRoot(root);
|
||||
|
||||
String[] commandKey;
|
||||
while ((commandKey = dataReader.getNextLine()) != null)
|
||||
{
|
||||
if (commandKey[0].equals("add"))
|
||||
{
|
||||
root.getData().add(commandKey[1]); // simulating addData()
|
||||
System.out.println("Add: " + commandKey[1]);
|
||||
tree.correctnessCheckPublic(); // check the correctness of the B-Tree
|
||||
}
|
||||
else if (commandKey[0].equals("del"))
|
||||
{
|
||||
System.out.println("Del: " + commandKey[1]);
|
||||
}
|
||||
}
|
||||
dataReader.closeGDI2BTreeDataReader(); // close the stream
|
||||
// until here everything should go well
|
||||
|
||||
// adding incorrectly a child node
|
||||
GDI2BTreeNode child = new GDI2BTreeNode(root);
|
||||
root.getChildren().add(child);
|
||||
// the leaf variable is not set to false
|
||||
// hasChildren() has to be correctly implemented to raise the exception here
|
||||
tree.correctnessCheckPublic(); // check the correctness of the B-Tree
|
||||
}
|
||||
}
|
||||
32510
ws2010/se/EiSE-Skript.pdf
Normal file
32510
ws2010/se/EiSE-Skript.pdf
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user