dos2unix
This commit is contained in:
parent
a531a3ab67
commit
603a4fabe3
103
06/gradlew.bat
vendored
103
06/gradlew.bat
vendored
@ -1,103 +0,0 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
@ -1,172 +1,172 @@
|
||||
public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T> {
|
||||
|
||||
/**
|
||||
* Return the height of node t, or 0, if null.
|
||||
*/
|
||||
private int height(TreeNode t) {
|
||||
return t == null ? 0 : t.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert into the tree; duplicates are ignored.
|
||||
* @param x the item to insert.
|
||||
*/
|
||||
public void add(T element) {
|
||||
root = insertAt(root, element);
|
||||
}
|
||||
|
||||
private TreeNode<T> balance(TreeNode<T> p) {
|
||||
if (p == null) return null;
|
||||
if (height(p.left) - height(p.right) == 2) {
|
||||
if (height(p.left.left) > height(p.left.right)) {
|
||||
// to be done
|
||||
} else {
|
||||
// to be done
|
||||
}
|
||||
} else if (height(p.right) - height(p.left) == 2) {
|
||||
if (height(p.right.right) > height(p.right.left)) {
|
||||
// to be done
|
||||
} else {
|
||||
// to be done
|
||||
}
|
||||
}
|
||||
p.height = Math.max(height(p.left), height(p.right)) + 1;
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to insert into a subtree.
|
||||
* @param x the item to insert.
|
||||
* @param t the node that roots the tree.
|
||||
* @return the new root.
|
||||
*/
|
||||
private TreeNode insertAt(TreeNode p, T element) {
|
||||
if (p == null) {
|
||||
p = new TreeNode<T>(element);
|
||||
return p;
|
||||
} else {
|
||||
int c = element.compareTo((T) p.element);
|
||||
if (c == 0) {
|
||||
p.count++;
|
||||
} else if (c < 0) {
|
||||
p.left = insertAt(p.left, element);
|
||||
} else if (c > 0) {
|
||||
p.right = insertAt(p.right, element);
|
||||
}
|
||||
}
|
||||
return balance(p);
|
||||
}
|
||||
|
||||
// find node to replace
|
||||
// find node to replace
|
||||
// private TreeNode<T> rep;
|
||||
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
||||
if (node.right != null) {
|
||||
node.right = findRepAt(node.right,rep);
|
||||
} else {
|
||||
rep.element = node.element;
|
||||
rep.count = node.count;
|
||||
rep.height = node.height;
|
||||
node = node.left;
|
||||
}
|
||||
// to be done
|
||||
return p;
|
||||
}
|
||||
|
||||
// remove node
|
||||
private TreeNode<T> removeAt(TreeNode<T> node, T x, TreeNode<T> removed) {
|
||||
if (node == null) {
|
||||
return null;
|
||||
} else {
|
||||
if (x.compareTo(node.element) == 0) {
|
||||
// found
|
||||
removed.element = node.element;
|
||||
if (node.count > 1) {
|
||||
node.count--;
|
||||
return node;
|
||||
} else if (node.left == null) {
|
||||
node = node.right;
|
||||
} else if (node.right == null) {
|
||||
node = node.left;
|
||||
} else {
|
||||
node.left = findRepAt(node.left,node);
|
||||
}
|
||||
} else if (x.compareTo(node.element) < 0) {
|
||||
// search left
|
||||
node.left = removeAt(node.left, x, removed);
|
||||
} else {
|
||||
// search right
|
||||
node.right = removeAt(node.right, x, removed);
|
||||
}
|
||||
// to be done
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove from the tree. Nothing is done if x is not found.
|
||||
* @param x the item to remove.
|
||||
*/
|
||||
public T remove(T x) {
|
||||
TreeNode<T> removed = new TreeNode<T>(null);
|
||||
root = removeAt(root, x, removed);
|
||||
return removed.element;
|
||||
}
|
||||
|
||||
public Traversal<T> traversal() {
|
||||
return new AVLTreeTraversal<T>(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate binary tree node with left child.
|
||||
* For AVL trees, this is a single rotation for case 1.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateR(TreeNode k2) {
|
||||
TreeNode k1 = k2.left;
|
||||
k2.left = k1.right;
|
||||
k1.right = k2;
|
||||
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
|
||||
k1.height = Math.max(height(k1.left), k2.height) + 1;
|
||||
return k1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate binary tree node with right child.
|
||||
* For AVL trees, this is a single rotation for case 4.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateL(TreeNode k1) {
|
||||
TreeNode k2 = k1.right;
|
||||
k1.right = k2.left;
|
||||
k2.left = k1;
|
||||
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
|
||||
k2.height = Math.max(height(k2.right), k1.height) + 1;
|
||||
return k2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Double rotate binary tree node: first left child
|
||||
* with its right child; then node k3 with new left child.
|
||||
* For AVL trees, this is a double rotation for case 2.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateLR(TreeNode k3) {
|
||||
k3.left = rotateL(k3.left);
|
||||
return rotateR(k3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Double rotate binary tree node: first right child
|
||||
* with its left child; then node k1 with new right child.
|
||||
* For AVL trees, this is a double rotation for case 3.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateRL(TreeNode k1) {
|
||||
k1.right = rotateR(k1.right);
|
||||
return rotateL(k1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T> {
|
||||
|
||||
/**
|
||||
* Return the height of node t, or 0, if null.
|
||||
*/
|
||||
private int height(TreeNode t) {
|
||||
return t == null ? 0 : t.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert into the tree; duplicates are ignored.
|
||||
* @param x the item to insert.
|
||||
*/
|
||||
public void add(T element) {
|
||||
root = insertAt(root, element);
|
||||
}
|
||||
|
||||
private TreeNode<T> balance(TreeNode<T> p) {
|
||||
if (p == null) return null;
|
||||
if (height(p.left) - height(p.right) == 2) {
|
||||
if (height(p.left.left) > height(p.left.right)) {
|
||||
// to be done
|
||||
} else {
|
||||
// to be done
|
||||
}
|
||||
} else if (height(p.right) - height(p.left) == 2) {
|
||||
if (height(p.right.right) > height(p.right.left)) {
|
||||
// to be done
|
||||
} else {
|
||||
// to be done
|
||||
}
|
||||
}
|
||||
p.height = Math.max(height(p.left), height(p.right)) + 1;
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to insert into a subtree.
|
||||
* @param x the item to insert.
|
||||
* @param t the node that roots the tree.
|
||||
* @return the new root.
|
||||
*/
|
||||
private TreeNode insertAt(TreeNode p, T element) {
|
||||
if (p == null) {
|
||||
p = new TreeNode<T>(element);
|
||||
return p;
|
||||
} else {
|
||||
int c = element.compareTo((T) p.element);
|
||||
if (c == 0) {
|
||||
p.count++;
|
||||
} else if (c < 0) {
|
||||
p.left = insertAt(p.left, element);
|
||||
} else if (c > 0) {
|
||||
p.right = insertAt(p.right, element);
|
||||
}
|
||||
}
|
||||
return balance(p);
|
||||
}
|
||||
|
||||
// find node to replace
|
||||
// find node to replace
|
||||
// private TreeNode<T> rep;
|
||||
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
||||
if (node.right != null) {
|
||||
node.right = findRepAt(node.right,rep);
|
||||
} else {
|
||||
rep.element = node.element;
|
||||
rep.count = node.count;
|
||||
rep.height = node.height;
|
||||
node = node.left;
|
||||
}
|
||||
// to be done
|
||||
return p;
|
||||
}
|
||||
|
||||
// remove node
|
||||
private TreeNode<T> removeAt(TreeNode<T> node, T x, TreeNode<T> removed) {
|
||||
if (node == null) {
|
||||
return null;
|
||||
} else {
|
||||
if (x.compareTo(node.element) == 0) {
|
||||
// found
|
||||
removed.element = node.element;
|
||||
if (node.count > 1) {
|
||||
node.count--;
|
||||
return node;
|
||||
} else if (node.left == null) {
|
||||
node = node.right;
|
||||
} else if (node.right == null) {
|
||||
node = node.left;
|
||||
} else {
|
||||
node.left = findRepAt(node.left,node);
|
||||
}
|
||||
} else if (x.compareTo(node.element) < 0) {
|
||||
// search left
|
||||
node.left = removeAt(node.left, x, removed);
|
||||
} else {
|
||||
// search right
|
||||
node.right = removeAt(node.right, x, removed);
|
||||
}
|
||||
// to be done
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove from the tree. Nothing is done if x is not found.
|
||||
* @param x the item to remove.
|
||||
*/
|
||||
public T remove(T x) {
|
||||
TreeNode<T> removed = new TreeNode<T>(null);
|
||||
root = removeAt(root, x, removed);
|
||||
return removed.element;
|
||||
}
|
||||
|
||||
public Traversal<T> traversal() {
|
||||
return new AVLTreeTraversal<T>(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate binary tree node with left child.
|
||||
* For AVL trees, this is a single rotation for case 1.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateR(TreeNode k2) {
|
||||
TreeNode k1 = k2.left;
|
||||
k2.left = k1.right;
|
||||
k1.right = k2;
|
||||
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
|
||||
k1.height = Math.max(height(k1.left), k2.height) + 1;
|
||||
return k1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate binary tree node with right child.
|
||||
* For AVL trees, this is a single rotation for case 4.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateL(TreeNode k1) {
|
||||
TreeNode k2 = k1.right;
|
||||
k1.right = k2.left;
|
||||
k2.left = k1;
|
||||
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
|
||||
k2.height = Math.max(height(k2.right), k1.height) + 1;
|
||||
return k2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Double rotate binary tree node: first left child
|
||||
* with its right child; then node k3 with new left child.
|
||||
* For AVL trees, this is a double rotation for case 2.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateLR(TreeNode k3) {
|
||||
k3.left = rotateL(k3.left);
|
||||
return rotateR(k3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Double rotate binary tree node: first right child
|
||||
* with its left child; then node k1 with new right child.
|
||||
* For AVL trees, this is a double rotation for case 3.
|
||||
* Update heights, then return new root.
|
||||
*/
|
||||
private TreeNode rotateRL(TreeNode k1) {
|
||||
k1.right = rotateR(k1.right);
|
||||
return rotateL(k1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,68 +1,68 @@
|
||||
import java.util.*;
|
||||
|
||||
public class AVLTreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||
|
||||
private TreeNode<T> root;
|
||||
|
||||
public AVLTreeTraversal(TreeNode<T> root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
inorder(node.left, vis);
|
||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||
inorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void inorder(Visitor<T> vis) {
|
||||
inorder(root, vis);
|
||||
}
|
||||
|
||||
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||
preorder(node.left, vis);
|
||||
preorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void preorder(Visitor<T> vis) {
|
||||
preorder(root, vis);
|
||||
}
|
||||
|
||||
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
postorder(node.left, vis);
|
||||
postorder(node.right, vis);
|
||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||
}
|
||||
}
|
||||
|
||||
public void postorder(Visitor<T> vis) {
|
||||
postorder(root, vis);
|
||||
}
|
||||
|
||||
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
||||
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
||||
if (node != null) {
|
||||
q.offer(node);
|
||||
}
|
||||
while (!q.isEmpty()) {
|
||||
node = q.poll();
|
||||
for (int i=0; i < node.count; i++) visitor.visit(node.element);
|
||||
if (node.left != null) {
|
||||
q.offer(node.left);
|
||||
}
|
||||
if (node.right != null) {
|
||||
q.offer(node.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void levelorder(Visitor<T> vis) {
|
||||
levelorder(root,vis);
|
||||
}
|
||||
|
||||
}
|
||||
import java.util.*;
|
||||
|
||||
public class AVLTreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||
|
||||
private TreeNode<T> root;
|
||||
|
||||
public AVLTreeTraversal(TreeNode<T> root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
inorder(node.left, vis);
|
||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||
inorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void inorder(Visitor<T> vis) {
|
||||
inorder(root, vis);
|
||||
}
|
||||
|
||||
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||
preorder(node.left, vis);
|
||||
preorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void preorder(Visitor<T> vis) {
|
||||
preorder(root, vis);
|
||||
}
|
||||
|
||||
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
postorder(node.left, vis);
|
||||
postorder(node.right, vis);
|
||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||
}
|
||||
}
|
||||
|
||||
public void postorder(Visitor<T> vis) {
|
||||
postorder(root, vis);
|
||||
}
|
||||
|
||||
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
||||
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
||||
if (node != null) {
|
||||
q.offer(node);
|
||||
}
|
||||
while (!q.isEmpty()) {
|
||||
node = q.poll();
|
||||
for (int i=0; i < node.count; i++) visitor.visit(node.element);
|
||||
if (node.left != null) {
|
||||
q.offer(node.left);
|
||||
}
|
||||
if (node.right != null) {
|
||||
q.offer(node.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void levelorder(Visitor<T> vis) {
|
||||
levelorder(root,vis);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,66 +1,66 @@
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
|
||||
public class Competitor implements Comparable<Competitor> {
|
||||
private String name;
|
||||
private String country;
|
||||
private long time;
|
||||
private int jg;
|
||||
private int startNr;
|
||||
private int rank;
|
||||
|
||||
public Competitor(int startNr, String name, int jg, String country, String time) throws ParseException {
|
||||
this.startNr = startNr;
|
||||
this.name = name;
|
||||
this.jg = jg;
|
||||
this.country = country;
|
||||
this.time = parseTime(time);
|
||||
}
|
||||
|
||||
public void setRank(int rank) {
|
||||
this.rank = rank;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getJg() {
|
||||
return jg;
|
||||
}
|
||||
|
||||
private static long parseTime(String s) throws ParseException {
|
||||
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
|
||||
Date date = sdf.parse(s);
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Rang: ");
|
||||
sb.append(rank);
|
||||
sb.append(" StartNr: ");
|
||||
sb.append(startNr);
|
||||
sb.append(" Name: ");
|
||||
sb.append(name);
|
||||
sb.append(" JG: ");
|
||||
sb.append(Integer.toString(jg));
|
||||
sb.append(" Zeit: ");
|
||||
sb.append(df.format(new Date(time)));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Competitor o) {
|
||||
return (int) (this.getTime() - o.getTime());
|
||||
}
|
||||
}
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
|
||||
public class Competitor implements Comparable<Competitor> {
|
||||
private String name;
|
||||
private String country;
|
||||
private long time;
|
||||
private int jg;
|
||||
private int startNr;
|
||||
private int rank;
|
||||
|
||||
public Competitor(int startNr, String name, int jg, String country, String time) throws ParseException {
|
||||
this.startNr = startNr;
|
||||
this.name = name;
|
||||
this.jg = jg;
|
||||
this.country = country;
|
||||
this.time = parseTime(time);
|
||||
}
|
||||
|
||||
public void setRank(int rank) {
|
||||
this.rank = rank;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getJg() {
|
||||
return jg;
|
||||
}
|
||||
|
||||
private static long parseTime(String s) throws ParseException {
|
||||
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
|
||||
Date date = sdf.parse(s);
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Rang: ");
|
||||
sb.append(rank);
|
||||
sb.append(" StartNr: ");
|
||||
sb.append(startNr);
|
||||
sb.append(" Name: ");
|
||||
sb.append(name);
|
||||
sb.append(" JG: ");
|
||||
sb.append(Integer.toString(jg));
|
||||
sb.append(" Zeit: ");
|
||||
sb.append(df.format(new Date(time)));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Competitor o) {
|
||||
return (int) (this.getTime() - o.getTime());
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
public class MyRankingVisitor implements Visitor<Competitor> {
|
||||
private StringBuilder buf;
|
||||
private int rank=1;
|
||||
|
||||
public MyRankingVisitor(StringBuilder buf) {
|
||||
this.buf = buf;
|
||||
}
|
||||
|
||||
public void visit(Competitor o) {
|
||||
o.setRank(rank++);
|
||||
buf.append(o.toString()+'\n');
|
||||
}
|
||||
}
|
||||
public class MyRankingVisitor implements Visitor<Competitor> {
|
||||
private StringBuilder buf;
|
||||
private int rank=1;
|
||||
|
||||
public MyRankingVisitor(StringBuilder buf) {
|
||||
this.buf = buf;
|
||||
}
|
||||
|
||||
public void visit(Competitor o) {
|
||||
o.setRank(rank++);
|
||||
buf.append(o.toString()+'\n');
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,37 @@
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
|
||||
public class RankingTreeServer implements CommandExecutor {
|
||||
|
||||
private final static int STARTNR = 0;
|
||||
private final static int NAME = 1;
|
||||
private final static int JG = 2;
|
||||
private final static int COUNTRY = 3;
|
||||
private final static int TIME = 4;
|
||||
Tree<Competitor> tree = new AVLSearchTree<>();
|
||||
|
||||
public void load(Tree<Competitor> tree, String list) throws Exception {
|
||||
String[] lines = list.split("\n");
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
String[] items = lines[i].split(";");
|
||||
Competitor c = new Competitor(Integer.parseInt(items[STARTNR]), // startNr
|
||||
items[NAME], // name
|
||||
Integer.parseInt(items[JG]), // jg
|
||||
items[COUNTRY], // country
|
||||
items[TIME]); // time
|
||||
tree.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
public String execute(String command) throws Exception {
|
||||
// list of all Competitors
|
||||
|
||||
//read values from String
|
||||
load(tree,command);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Traversal<Competitor> trav = tree.traversal();
|
||||
Visitor<Competitor> vis = new MyRankingVisitor(buf);
|
||||
trav.inorder(vis);
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
|
||||
public class RankingTreeServer implements CommandExecutor {
|
||||
|
||||
private final static int STARTNR = 0;
|
||||
private final static int NAME = 1;
|
||||
private final static int JG = 2;
|
||||
private final static int COUNTRY = 3;
|
||||
private final static int TIME = 4;
|
||||
Tree<Competitor> tree = new AVLSearchTree<>();
|
||||
|
||||
public void load(Tree<Competitor> tree, String list) throws Exception {
|
||||
String[] lines = list.split("\n");
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
String[] items = lines[i].split(";");
|
||||
Competitor c = new Competitor(Integer.parseInt(items[STARTNR]), // startNr
|
||||
items[NAME], // name
|
||||
Integer.parseInt(items[JG]), // jg
|
||||
items[COUNTRY], // country
|
||||
items[TIME]); // time
|
||||
tree.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
public String execute(String command) throws Exception {
|
||||
// list of all Competitors
|
||||
|
||||
//read values from String
|
||||
load(tree,command);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Traversal<Competitor> trav = tree.traversal();
|
||||
Visitor<Competitor> vis = new MyRankingVisitor(buf);
|
||||
trav.inorder(vis);
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
@ -1,144 +1,144 @@
|
||||
import java.util.*;
|
||||
|
||||
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
||||
protected TreeNode<T> root;
|
||||
|
||||
private TreeNode<T> insertAt(TreeNode<T> node, T x) {
|
||||
if (node == null) {
|
||||
return new TreeNode<T>(x);
|
||||
} else {
|
||||
if (x.compareTo(node.element) <= 0) {
|
||||
node.left = insertAt(node.left, x);
|
||||
} else {
|
||||
node.right = insertAt(node.right, x);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(T x) {
|
||||
root = insertAt(root, x);
|
||||
}
|
||||
|
||||
// find node to replace
|
||||
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
||||
if (node.right != null) {
|
||||
node.right = findRepAt(node.right,rep);
|
||||
} else {
|
||||
rep.element = node.element;
|
||||
node = node.left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// remove node
|
||||
private TreeNode<T> removeAt(TreeNode<T> node, T x,TreeNode<T> removed ) {
|
||||
if (node == null) {
|
||||
return null;
|
||||
} else {
|
||||
if (x.compareTo(node.element) == 0) {
|
||||
// found
|
||||
removed.element = node.element;
|
||||
if (node.left == null) {
|
||||
node = node.right;
|
||||
} else if (node.right == null) {
|
||||
node = node.left;
|
||||
} else {
|
||||
node.left = findRepAt(node.left,node);
|
||||
}
|
||||
} else if (x.compareTo(node.element) < 0) {
|
||||
// search left
|
||||
node.left = removeAt(node.left, x, removed);
|
||||
} else {
|
||||
// search right
|
||||
node.right = removeAt(node.right, x, removed);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public T remove(T x) {
|
||||
TreeNode<T> removed = new TreeNode<T>(null);
|
||||
root = removeAt(root, x, removed);
|
||||
return removed.element;
|
||||
}
|
||||
|
||||
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public Traversal<T> traversal() {
|
||||
return new TreeTraversal<T>(root);
|
||||
}
|
||||
|
||||
protected int calcHeight(TreeNode<T> node) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1 + Math.max(calcHeight(node.left),calcHeight(node.right));
|
||||
}
|
||||
}
|
||||
|
||||
public int height() {
|
||||
return calcHeight(root);
|
||||
}
|
||||
|
||||
protected int calcSize(TreeNode p) {
|
||||
if (p == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return p.count + calcSize(p.left) + calcSize(p.right);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return calcSize(root);
|
||||
}
|
||||
|
||||
private boolean balanced(TreeNode<T> node) {
|
||||
if (node == null) {
|
||||
return true;
|
||||
} else {
|
||||
return Math.abs(calcHeight(node.left) - calcHeight(node.right)) < 2
|
||||
&& balanced(node.left) && balanced(node.right);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean balanced() {
|
||||
return balanced(root);
|
||||
}
|
||||
|
||||
// only for testing and debugging: show the structure of the tree
|
||||
public String printTree() {
|
||||
StringBuilder out = new StringBuilder();
|
||||
if (root.right != null) {
|
||||
printTree(root.right,out, true, "");
|
||||
}
|
||||
out.append(root.element+"\n");
|
||||
if (root.left != null) {
|
||||
printTree(root.left,out, false, "");
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private void printTree(TreeNode node, StringBuilder out, boolean isRight, String indent) {
|
||||
if (node.right != null) {
|
||||
printTree(node.right, out, true,
|
||||
indent + (isRight ? " " : " | "));
|
||||
}
|
||||
out.append(indent);
|
||||
if (isRight) {
|
||||
out.append(" /");
|
||||
} else {
|
||||
out.append(" \\");
|
||||
}
|
||||
out.append("----- ");
|
||||
out.append(node.element+"\n");
|
||||
if (node.left != null) {
|
||||
printTree(node.left, out, false,
|
||||
indent + (isRight ? " | " : " "));
|
||||
}
|
||||
}
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
||||
protected TreeNode<T> root;
|
||||
|
||||
private TreeNode<T> insertAt(TreeNode<T> node, T x) {
|
||||
if (node == null) {
|
||||
return new TreeNode<T>(x);
|
||||
} else {
|
||||
if (x.compareTo(node.element) <= 0) {
|
||||
node.left = insertAt(node.left, x);
|
||||
} else {
|
||||
node.right = insertAt(node.right, x);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(T x) {
|
||||
root = insertAt(root, x);
|
||||
}
|
||||
|
||||
// find node to replace
|
||||
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
||||
if (node.right != null) {
|
||||
node.right = findRepAt(node.right,rep);
|
||||
} else {
|
||||
rep.element = node.element;
|
||||
node = node.left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// remove node
|
||||
private TreeNode<T> removeAt(TreeNode<T> node, T x,TreeNode<T> removed ) {
|
||||
if (node == null) {
|
||||
return null;
|
||||
} else {
|
||||
if (x.compareTo(node.element) == 0) {
|
||||
// found
|
||||
removed.element = node.element;
|
||||
if (node.left == null) {
|
||||
node = node.right;
|
||||
} else if (node.right == null) {
|
||||
node = node.left;
|
||||
} else {
|
||||
node.left = findRepAt(node.left,node);
|
||||
}
|
||||
} else if (x.compareTo(node.element) < 0) {
|
||||
// search left
|
||||
node.left = removeAt(node.left, x, removed);
|
||||
} else {
|
||||
// search right
|
||||
node.right = removeAt(node.right, x, removed);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public T remove(T x) {
|
||||
TreeNode<T> removed = new TreeNode<T>(null);
|
||||
root = removeAt(root, x, removed);
|
||||
return removed.element;
|
||||
}
|
||||
|
||||
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public Traversal<T> traversal() {
|
||||
return new TreeTraversal<T>(root);
|
||||
}
|
||||
|
||||
protected int calcHeight(TreeNode<T> node) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1 + Math.max(calcHeight(node.left),calcHeight(node.right));
|
||||
}
|
||||
}
|
||||
|
||||
public int height() {
|
||||
return calcHeight(root);
|
||||
}
|
||||
|
||||
protected int calcSize(TreeNode p) {
|
||||
if (p == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return p.count + calcSize(p.left) + calcSize(p.right);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return calcSize(root);
|
||||
}
|
||||
|
||||
private boolean balanced(TreeNode<T> node) {
|
||||
if (node == null) {
|
||||
return true;
|
||||
} else {
|
||||
return Math.abs(calcHeight(node.left) - calcHeight(node.right)) < 2
|
||||
&& balanced(node.left) && balanced(node.right);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean balanced() {
|
||||
return balanced(root);
|
||||
}
|
||||
|
||||
// only for testing and debugging: show the structure of the tree
|
||||
public String printTree() {
|
||||
StringBuilder out = new StringBuilder();
|
||||
if (root.right != null) {
|
||||
printTree(root.right,out, true, "");
|
||||
}
|
||||
out.append(root.element+"\n");
|
||||
if (root.left != null) {
|
||||
printTree(root.left,out, false, "");
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private void printTree(TreeNode node, StringBuilder out, boolean isRight, String indent) {
|
||||
if (node.right != null) {
|
||||
printTree(node.right, out, true,
|
||||
indent + (isRight ? " " : " | "));
|
||||
}
|
||||
out.append(indent);
|
||||
if (isRight) {
|
||||
out.append(" /");
|
||||
} else {
|
||||
out.append(" \\");
|
||||
}
|
||||
out.append("----- ");
|
||||
out.append(node.element+"\n");
|
||||
if (node.left != null) {
|
||||
printTree(node.left, out, false,
|
||||
indent + (isRight ? " | " : " "));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
/* interface of Traversal ADT */
|
||||
public interface Traversal<T extends Comparable<T>> {
|
||||
/* traverse elements of tree in preorder */
|
||||
public void preorder(Visitor<T> vistor);
|
||||
/* traverse elements of tree in inorder */
|
||||
public void inorder(Visitor<T> vistor);
|
||||
/* traverse elements of tree in postorder */
|
||||
public void postorder(Visitor<T> vistor);
|
||||
/* traverse elements of tree in levelorder */
|
||||
public void levelorder(Visitor<T> vistor);
|
||||
}
|
||||
/* interface of Traversal ADT */
|
||||
public interface Traversal<T extends Comparable<T>> {
|
||||
/* traverse elements of tree in preorder */
|
||||
public void preorder(Visitor<T> vistor);
|
||||
/* traverse elements of tree in inorder */
|
||||
public void inorder(Visitor<T> vistor);
|
||||
/* traverse elements of tree in postorder */
|
||||
public void postorder(Visitor<T> vistor);
|
||||
/* traverse elements of tree in levelorder */
|
||||
public void levelorder(Visitor<T> vistor);
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
/* interface of Tree ADT */
|
||||
public interface Tree<T extends Comparable<T>> {
|
||||
/* add an element to the tree */
|
||||
void add(T o);
|
||||
/* remove an element; returns the element if found else return null */
|
||||
T remove(T o);
|
||||
/* test if tree is empty */
|
||||
boolean isEmpty();
|
||||
/* returns instance of class that implements traversal interface */
|
||||
Traversal<T> traversal();
|
||||
/* number of elements */
|
||||
int size();
|
||||
/* height of the tree */
|
||||
int height();
|
||||
/* is the tree balanced */
|
||||
boolean balanced();
|
||||
}
|
||||
/* interface of Tree ADT */
|
||||
public interface Tree<T extends Comparable<T>> {
|
||||
/* add an element to the tree */
|
||||
void add(T o);
|
||||
/* remove an element; returns the element if found else return null */
|
||||
T remove(T o);
|
||||
/* test if tree is empty */
|
||||
boolean isEmpty();
|
||||
/* returns instance of class that implements traversal interface */
|
||||
Traversal<T> traversal();
|
||||
/* number of elements */
|
||||
int size();
|
||||
/* height of the tree */
|
||||
int height();
|
||||
/* is the tree balanced */
|
||||
boolean balanced();
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
class TreeNode<T extends Comparable<T>> {
|
||||
T element;
|
||||
TreeNode left, right;
|
||||
int height;
|
||||
int count;
|
||||
|
||||
TreeNode(T element){
|
||||
this.element = element;
|
||||
this.count = 1;
|
||||
this.height = 1;
|
||||
}
|
||||
|
||||
TreeNode(T element, TreeNode left, TreeNode right){
|
||||
this(element); this.left = left; this.right = right;
|
||||
}
|
||||
|
||||
T getValue(){return element;}
|
||||
class TreeNode<T extends Comparable<T>> {
|
||||
T element;
|
||||
TreeNode left, right;
|
||||
int height;
|
||||
int count;
|
||||
|
||||
TreeNode(T element){
|
||||
this.element = element;
|
||||
this.count = 1;
|
||||
this.height = 1;
|
||||
}
|
||||
|
||||
TreeNode(T element, TreeNode left, TreeNode right){
|
||||
this(element); this.left = left; this.right = right;
|
||||
}
|
||||
|
||||
T getValue(){return element;}
|
||||
}
|
@ -1,68 +1,68 @@
|
||||
import java.util.*;
|
||||
|
||||
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||
|
||||
private TreeNode<T> root;
|
||||
|
||||
public TreeTraversal(TreeNode<T> root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
inorder(node.left, vis);
|
||||
vis.visit(node.element);
|
||||
inorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void inorder(Visitor<T> vis) {
|
||||
inorder(root, vis);
|
||||
}
|
||||
|
||||
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
vis.visit(node.element);
|
||||
preorder(node.left, vis);
|
||||
preorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void preorder(Visitor<T> vis) {
|
||||
preorder(root, vis);
|
||||
}
|
||||
|
||||
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
postorder(node.left, vis);
|
||||
postorder(node.right, vis);
|
||||
vis.visit(node.element);
|
||||
}
|
||||
}
|
||||
|
||||
public void postorder(Visitor<T> vis) {
|
||||
postorder(root, vis);
|
||||
}
|
||||
|
||||
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
||||
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
||||
if (node != null) {
|
||||
q.add(node);
|
||||
}
|
||||
while (!q.isEmpty()) {
|
||||
node = q.remove();
|
||||
visitor.visit(node.element);
|
||||
if (node.left != null) {
|
||||
q.add(node.left);
|
||||
}
|
||||
if (node.right != null) {
|
||||
q.add(node.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void levelorder(Visitor<T> vis) {
|
||||
levelorder(root,vis);
|
||||
}
|
||||
|
||||
}
|
||||
import java.util.*;
|
||||
|
||||
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||
|
||||
private TreeNode<T> root;
|
||||
|
||||
public TreeTraversal(TreeNode<T> root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
inorder(node.left, vis);
|
||||
vis.visit(node.element);
|
||||
inorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void inorder(Visitor<T> vis) {
|
||||
inorder(root, vis);
|
||||
}
|
||||
|
||||
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
vis.visit(node.element);
|
||||
preorder(node.left, vis);
|
||||
preorder(node.right, vis);
|
||||
}
|
||||
}
|
||||
|
||||
public void preorder(Visitor<T> vis) {
|
||||
preorder(root, vis);
|
||||
}
|
||||
|
||||
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
||||
if (node != null) {
|
||||
postorder(node.left, vis);
|
||||
postorder(node.right, vis);
|
||||
vis.visit(node.element);
|
||||
}
|
||||
}
|
||||
|
||||
public void postorder(Visitor<T> vis) {
|
||||
postorder(root, vis);
|
||||
}
|
||||
|
||||
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
||||
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
||||
if (node != null) {
|
||||
q.add(node);
|
||||
}
|
||||
while (!q.isEmpty()) {
|
||||
node = q.remove();
|
||||
visitor.visit(node.element);
|
||||
if (node.left != null) {
|
||||
q.add(node.left);
|
||||
}
|
||||
if (node.right != null) {
|
||||
q.add(node.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void levelorder(Visitor<T> vis) {
|
||||
levelorder(root,vis);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/* interface of visitor ADT */
|
||||
public interface Visitor<T extends Comparable<T>> {
|
||||
/* called for each element in the tree */
|
||||
public void visit(T obj);
|
||||
}
|
||||
/* interface of visitor ADT */
|
||||
public interface Visitor<T extends Comparable<T>> {
|
||||
/* called for each element in the tree */
|
||||
public void visit(T obj);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,130 +1,130 @@
|
||||
import java.util.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AVLSearchTreeTest {
|
||||
Tree<String> tree;
|
||||
|
||||
private void init(Tree<String> tree) {
|
||||
tree.add("E");
|
||||
tree.add("F");
|
||||
tree.add("G");
|
||||
tree.add("H");
|
||||
tree.add("J");
|
||||
tree.add("A");
|
||||
tree.add("B");
|
||||
tree.add("C");
|
||||
tree.add("D");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tree = new AVLSearchTree<String>();
|
||||
init(tree);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInorder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().inorder(v);
|
||||
assertEquals("inorder", "ABCDEFGHJ", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreorder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().preorder(v);
|
||||
assertEquals("preorder", "FBADCEHGJ", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostorder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().postorder(v);
|
||||
assertEquals("postorder", "ACEDBGJHF", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLevelörder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().levelorder(v);
|
||||
assertEquals("levelorder", "FBHADGJCE", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeight() {
|
||||
assertEquals(4,tree.height());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBalanced() {
|
||||
assertTrue(tree.balanced());
|
||||
Tree<String> tree2 = new SortedBinaryTree<String> ();
|
||||
tree2.add("A");
|
||||
tree2.add("B");
|
||||
tree2.add("C");
|
||||
assertFalse(tree2.balanced());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
tree = new AVLSearchTree<String>();
|
||||
init(tree);
|
||||
tree.remove("F");
|
||||
tree.remove("H");
|
||||
tree.remove("J");
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().inorder(v);
|
||||
assertEquals("remove", "ABCDEG", v.toString());
|
||||
v = new MyVisitor<String>();
|
||||
tree.traversal().levelorder(v);
|
||||
assertEquals("remove", "DBEACG", v.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMixed() {
|
||||
tree = new AVLSearchTree<String>();
|
||||
List<String> list = new LinkedList<>();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Character c = (char) ('A' + (Math.random() * 26));
|
||||
int op = (int) (Math.random() * 2);
|
||||
switch (op) {
|
||||
case 0:
|
||||
list.add(c.toString());
|
||||
tree.add(c.toString());
|
||||
break;
|
||||
case 1:
|
||||
list.remove(c.toString());
|
||||
tree.remove(c.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(tree.balanced());
|
||||
assertEquals(tree.size(),list.size());
|
||||
Collections.sort(list);
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (String s : list) {b.append(s);};
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().inorder(v);
|
||||
assertEquals("mixed",b.toString(), v.toString());
|
||||
}
|
||||
}
|
||||
|
||||
class MyVisitor<T> implements Visitor<T> {
|
||||
StringBuilder output;
|
||||
|
||||
MyVisitor() {
|
||||
output = new StringBuilder();
|
||||
}
|
||||
|
||||
public void visit(T s) {
|
||||
output.append(s);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return output.toString();
|
||||
}
|
||||
import java.util.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AVLSearchTreeTest {
|
||||
Tree<String> tree;
|
||||
|
||||
private void init(Tree<String> tree) {
|
||||
tree.add("E");
|
||||
tree.add("F");
|
||||
tree.add("G");
|
||||
tree.add("H");
|
||||
tree.add("J");
|
||||
tree.add("A");
|
||||
tree.add("B");
|
||||
tree.add("C");
|
||||
tree.add("D");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tree = new AVLSearchTree<String>();
|
||||
init(tree);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInorder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().inorder(v);
|
||||
assertEquals("inorder", "ABCDEFGHJ", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreorder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().preorder(v);
|
||||
assertEquals("preorder", "FBADCEHGJ", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostorder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().postorder(v);
|
||||
assertEquals("postorder", "ACEDBGJHF", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLevelörder() {
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().levelorder(v);
|
||||
assertEquals("levelorder", "FBHADGJCE", v.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeight() {
|
||||
assertEquals(4,tree.height());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBalanced() {
|
||||
assertTrue(tree.balanced());
|
||||
Tree<String> tree2 = new SortedBinaryTree<String> ();
|
||||
tree2.add("A");
|
||||
tree2.add("B");
|
||||
tree2.add("C");
|
||||
assertFalse(tree2.balanced());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
tree = new AVLSearchTree<String>();
|
||||
init(tree);
|
||||
tree.remove("F");
|
||||
tree.remove("H");
|
||||
tree.remove("J");
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().inorder(v);
|
||||
assertEquals("remove", "ABCDEG", v.toString());
|
||||
v = new MyVisitor<String>();
|
||||
tree.traversal().levelorder(v);
|
||||
assertEquals("remove", "DBEACG", v.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMixed() {
|
||||
tree = new AVLSearchTree<String>();
|
||||
List<String> list = new LinkedList<>();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Character c = (char) ('A' + (Math.random() * 26));
|
||||
int op = (int) (Math.random() * 2);
|
||||
switch (op) {
|
||||
case 0:
|
||||
list.add(c.toString());
|
||||
tree.add(c.toString());
|
||||
break;
|
||||
case 1:
|
||||
list.remove(c.toString());
|
||||
tree.remove(c.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(tree.balanced());
|
||||
assertEquals(tree.size(),list.size());
|
||||
Collections.sort(list);
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (String s : list) {b.append(s);};
|
||||
Visitor<String> v = new MyVisitor<String>();
|
||||
tree.traversal().inorder(v);
|
||||
assertEquals("mixed",b.toString(), v.toString());
|
||||
}
|
||||
}
|
||||
|
||||
class MyVisitor<T> implements Visitor<T> {
|
||||
StringBuilder output;
|
||||
|
||||
MyVisitor() {
|
||||
output = new StringBuilder();
|
||||
}
|
||||
|
||||
public void visit(T s) {
|
||||
output.append(s);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return output.toString();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user