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> {
|
public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the height of node t, or 0, if null.
|
* Return the height of node t, or 0, if null.
|
||||||
*/
|
*/
|
||||||
private int height(TreeNode t) {
|
private int height(TreeNode t) {
|
||||||
return t == null ? 0 : t.height;
|
return t == null ? 0 : t.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert into the tree; duplicates are ignored.
|
* Insert into the tree; duplicates are ignored.
|
||||||
* @param x the item to insert.
|
* @param x the item to insert.
|
||||||
*/
|
*/
|
||||||
public void add(T element) {
|
public void add(T element) {
|
||||||
root = insertAt(root, element);
|
root = insertAt(root, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TreeNode<T> balance(TreeNode<T> p) {
|
private TreeNode<T> balance(TreeNode<T> p) {
|
||||||
if (p == null) return null;
|
if (p == null) return null;
|
||||||
if (height(p.left) - height(p.right) == 2) {
|
if (height(p.left) - height(p.right) == 2) {
|
||||||
if (height(p.left.left) > height(p.left.right)) {
|
if (height(p.left.left) > height(p.left.right)) {
|
||||||
// to be done
|
// to be done
|
||||||
} else {
|
} else {
|
||||||
// to be done
|
// to be done
|
||||||
}
|
}
|
||||||
} else if (height(p.right) - height(p.left) == 2) {
|
} else if (height(p.right) - height(p.left) == 2) {
|
||||||
if (height(p.right.right) > height(p.right.left)) {
|
if (height(p.right.right) > height(p.right.left)) {
|
||||||
// to be done
|
// to be done
|
||||||
} else {
|
} else {
|
||||||
// to be done
|
// to be done
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.height = Math.max(height(p.left), height(p.right)) + 1;
|
p.height = Math.max(height(p.left), height(p.right)) + 1;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal method to insert into a subtree.
|
* Internal method to insert into a subtree.
|
||||||
* @param x the item to insert.
|
* @param x the item to insert.
|
||||||
* @param t the node that roots the tree.
|
* @param t the node that roots the tree.
|
||||||
* @return the new root.
|
* @return the new root.
|
||||||
*/
|
*/
|
||||||
private TreeNode insertAt(TreeNode p, T element) {
|
private TreeNode insertAt(TreeNode p, T element) {
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
p = new TreeNode<T>(element);
|
p = new TreeNode<T>(element);
|
||||||
return p;
|
return p;
|
||||||
} else {
|
} else {
|
||||||
int c = element.compareTo((T) p.element);
|
int c = element.compareTo((T) p.element);
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
p.count++;
|
p.count++;
|
||||||
} else if (c < 0) {
|
} else if (c < 0) {
|
||||||
p.left = insertAt(p.left, element);
|
p.left = insertAt(p.left, element);
|
||||||
} else if (c > 0) {
|
} else if (c > 0) {
|
||||||
p.right = insertAt(p.right, element);
|
p.right = insertAt(p.right, element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return balance(p);
|
return balance(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
// find node to replace
|
// find node to replace
|
||||||
// find node to replace
|
// find node to replace
|
||||||
// private TreeNode<T> rep;
|
// private TreeNode<T> rep;
|
||||||
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
||||||
if (node.right != null) {
|
if (node.right != null) {
|
||||||
node.right = findRepAt(node.right,rep);
|
node.right = findRepAt(node.right,rep);
|
||||||
} else {
|
} else {
|
||||||
rep.element = node.element;
|
rep.element = node.element;
|
||||||
rep.count = node.count;
|
rep.count = node.count;
|
||||||
rep.height = node.height;
|
rep.height = node.height;
|
||||||
node = node.left;
|
node = node.left;
|
||||||
}
|
}
|
||||||
// to be done
|
// to be done
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove node
|
// remove node
|
||||||
private TreeNode<T> removeAt(TreeNode<T> node, T x, TreeNode<T> removed) {
|
private TreeNode<T> removeAt(TreeNode<T> node, T x, TreeNode<T> removed) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
if (x.compareTo(node.element) == 0) {
|
if (x.compareTo(node.element) == 0) {
|
||||||
// found
|
// found
|
||||||
removed.element = node.element;
|
removed.element = node.element;
|
||||||
if (node.count > 1) {
|
if (node.count > 1) {
|
||||||
node.count--;
|
node.count--;
|
||||||
return node;
|
return node;
|
||||||
} else if (node.left == null) {
|
} else if (node.left == null) {
|
||||||
node = node.right;
|
node = node.right;
|
||||||
} else if (node.right == null) {
|
} else if (node.right == null) {
|
||||||
node = node.left;
|
node = node.left;
|
||||||
} else {
|
} else {
|
||||||
node.left = findRepAt(node.left,node);
|
node.left = findRepAt(node.left,node);
|
||||||
}
|
}
|
||||||
} else if (x.compareTo(node.element) < 0) {
|
} else if (x.compareTo(node.element) < 0) {
|
||||||
// search left
|
// search left
|
||||||
node.left = removeAt(node.left, x, removed);
|
node.left = removeAt(node.left, x, removed);
|
||||||
} else {
|
} else {
|
||||||
// search right
|
// search right
|
||||||
node.right = removeAt(node.right, x, removed);
|
node.right = removeAt(node.right, x, removed);
|
||||||
}
|
}
|
||||||
// to be done
|
// to be done
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove from the tree. Nothing is done if x is not found.
|
* Remove from the tree. Nothing is done if x is not found.
|
||||||
* @param x the item to remove.
|
* @param x the item to remove.
|
||||||
*/
|
*/
|
||||||
public T remove(T x) {
|
public T remove(T x) {
|
||||||
TreeNode<T> removed = new TreeNode<T>(null);
|
TreeNode<T> removed = new TreeNode<T>(null);
|
||||||
root = removeAt(root, x, removed);
|
root = removeAt(root, x, removed);
|
||||||
return removed.element;
|
return removed.element;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Traversal<T> traversal() {
|
public Traversal<T> traversal() {
|
||||||
return new AVLTreeTraversal<T>(root);
|
return new AVLTreeTraversal<T>(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate binary tree node with left child.
|
* Rotate binary tree node with left child.
|
||||||
* For AVL trees, this is a single rotation for case 1.
|
* For AVL trees, this is a single rotation for case 1.
|
||||||
* Update heights, then return new root.
|
* Update heights, then return new root.
|
||||||
*/
|
*/
|
||||||
private TreeNode rotateR(TreeNode k2) {
|
private TreeNode rotateR(TreeNode k2) {
|
||||||
TreeNode k1 = k2.left;
|
TreeNode k1 = k2.left;
|
||||||
k2.left = k1.right;
|
k2.left = k1.right;
|
||||||
k1.right = k2;
|
k1.right = k2;
|
||||||
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
|
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
|
||||||
k1.height = Math.max(height(k1.left), k2.height) + 1;
|
k1.height = Math.max(height(k1.left), k2.height) + 1;
|
||||||
return k1;
|
return k1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate binary tree node with right child.
|
* Rotate binary tree node with right child.
|
||||||
* For AVL trees, this is a single rotation for case 4.
|
* For AVL trees, this is a single rotation for case 4.
|
||||||
* Update heights, then return new root.
|
* Update heights, then return new root.
|
||||||
*/
|
*/
|
||||||
private TreeNode rotateL(TreeNode k1) {
|
private TreeNode rotateL(TreeNode k1) {
|
||||||
TreeNode k2 = k1.right;
|
TreeNode k2 = k1.right;
|
||||||
k1.right = k2.left;
|
k1.right = k2.left;
|
||||||
k2.left = k1;
|
k2.left = k1;
|
||||||
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
|
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
|
||||||
k2.height = Math.max(height(k2.right), k1.height) + 1;
|
k2.height = Math.max(height(k2.right), k1.height) + 1;
|
||||||
return k2;
|
return k2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Double rotate binary tree node: first left child
|
* Double rotate binary tree node: first left child
|
||||||
* with its right child; then node k3 with new left child.
|
* with its right child; then node k3 with new left child.
|
||||||
* For AVL trees, this is a double rotation for case 2.
|
* For AVL trees, this is a double rotation for case 2.
|
||||||
* Update heights, then return new root.
|
* Update heights, then return new root.
|
||||||
*/
|
*/
|
||||||
private TreeNode rotateLR(TreeNode k3) {
|
private TreeNode rotateLR(TreeNode k3) {
|
||||||
k3.left = rotateL(k3.left);
|
k3.left = rotateL(k3.left);
|
||||||
return rotateR(k3);
|
return rotateR(k3);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Double rotate binary tree node: first right child
|
* Double rotate binary tree node: first right child
|
||||||
* with its left child; then node k1 with new right child.
|
* with its left child; then node k1 with new right child.
|
||||||
* For AVL trees, this is a double rotation for case 3.
|
* For AVL trees, this is a double rotation for case 3.
|
||||||
* Update heights, then return new root.
|
* Update heights, then return new root.
|
||||||
*/
|
*/
|
||||||
private TreeNode rotateRL(TreeNode k1) {
|
private TreeNode rotateRL(TreeNode k1) {
|
||||||
k1.right = rotateR(k1.right);
|
k1.right = rotateR(k1.right);
|
||||||
return rotateL(k1);
|
return rotateL(k1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,68 +1,68 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class AVLTreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
public class AVLTreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||||
|
|
||||||
private TreeNode<T> root;
|
private TreeNode<T> root;
|
||||||
|
|
||||||
public AVLTreeTraversal(TreeNode<T> root) {
|
public AVLTreeTraversal(TreeNode<T> root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
inorder(node.left, vis);
|
inorder(node.left, vis);
|
||||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||||
inorder(node.right, vis);
|
inorder(node.right, vis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void inorder(Visitor<T> vis) {
|
public void inorder(Visitor<T> vis) {
|
||||||
inorder(root, vis);
|
inorder(root, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||||
preorder(node.left, vis);
|
preorder(node.left, vis);
|
||||||
preorder(node.right, vis);
|
preorder(node.right, vis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void preorder(Visitor<T> vis) {
|
public void preorder(Visitor<T> vis) {
|
||||||
preorder(root, vis);
|
preorder(root, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
postorder(node.left, vis);
|
postorder(node.left, vis);
|
||||||
postorder(node.right, vis);
|
postorder(node.right, vis);
|
||||||
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
for (int i=0; i < node.count; i++) vis.visit(node.element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void postorder(Visitor<T> vis) {
|
public void postorder(Visitor<T> vis) {
|
||||||
postorder(root, vis);
|
postorder(root, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
||||||
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
q.offer(node);
|
q.offer(node);
|
||||||
}
|
}
|
||||||
while (!q.isEmpty()) {
|
while (!q.isEmpty()) {
|
||||||
node = q.poll();
|
node = q.poll();
|
||||||
for (int i=0; i < node.count; i++) visitor.visit(node.element);
|
for (int i=0; i < node.count; i++) visitor.visit(node.element);
|
||||||
if (node.left != null) {
|
if (node.left != null) {
|
||||||
q.offer(node.left);
|
q.offer(node.left);
|
||||||
}
|
}
|
||||||
if (node.right != null) {
|
if (node.right != null) {
|
||||||
q.offer(node.right);
|
q.offer(node.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void levelorder(Visitor<T> vis) {
|
public void levelorder(Visitor<T> vis) {
|
||||||
levelorder(root,vis);
|
levelorder(root,vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,66 +1,66 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
|
|
||||||
public class Competitor implements Comparable<Competitor> {
|
public class Competitor implements Comparable<Competitor> {
|
||||||
private String name;
|
private String name;
|
||||||
private String country;
|
private String country;
|
||||||
private long time;
|
private long time;
|
||||||
private int jg;
|
private int jg;
|
||||||
private int startNr;
|
private int startNr;
|
||||||
private int rank;
|
private int rank;
|
||||||
|
|
||||||
public Competitor(int startNr, String name, int jg, String country, String time) throws ParseException {
|
public Competitor(int startNr, String name, int jg, String country, String time) throws ParseException {
|
||||||
this.startNr = startNr;
|
this.startNr = startNr;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.jg = jg;
|
this.jg = jg;
|
||||||
this.country = country;
|
this.country = country;
|
||||||
this.time = parseTime(time);
|
this.time = parseTime(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRank(int rank) {
|
public void setRank(int rank) {
|
||||||
this.rank = rank;
|
this.rank = rank;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTime(long time) {
|
public void setTime(long time) {
|
||||||
this.time = time;
|
this.time = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTime() {
|
public long getTime() {
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getJg() {
|
public int getJg() {
|
||||||
return jg;
|
return jg;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long parseTime(String s) throws ParseException {
|
private static long parseTime(String s) throws ParseException {
|
||||||
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
|
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
|
||||||
Date date = sdf.parse(s);
|
Date date = sdf.parse(s);
|
||||||
return date.getTime();
|
return date.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
|
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("Rang: ");
|
sb.append("Rang: ");
|
||||||
sb.append(rank);
|
sb.append(rank);
|
||||||
sb.append(" StartNr: ");
|
sb.append(" StartNr: ");
|
||||||
sb.append(startNr);
|
sb.append(startNr);
|
||||||
sb.append(" Name: ");
|
sb.append(" Name: ");
|
||||||
sb.append(name);
|
sb.append(name);
|
||||||
sb.append(" JG: ");
|
sb.append(" JG: ");
|
||||||
sb.append(Integer.toString(jg));
|
sb.append(Integer.toString(jg));
|
||||||
sb.append(" Zeit: ");
|
sb.append(" Zeit: ");
|
||||||
sb.append(df.format(new Date(time)));
|
sb.append(df.format(new Date(time)));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Competitor o) {
|
public int compareTo(Competitor o) {
|
||||||
return (int) (this.getTime() - o.getTime());
|
return (int) (this.getTime() - o.getTime());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
public class MyRankingVisitor implements Visitor<Competitor> {
|
public class MyRankingVisitor implements Visitor<Competitor> {
|
||||||
private StringBuilder buf;
|
private StringBuilder buf;
|
||||||
private int rank=1;
|
private int rank=1;
|
||||||
|
|
||||||
public MyRankingVisitor(StringBuilder buf) {
|
public MyRankingVisitor(StringBuilder buf) {
|
||||||
this.buf = buf;
|
this.buf = buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(Competitor o) {
|
public void visit(Competitor o) {
|
||||||
o.setRank(rank++);
|
o.setRank(rank++);
|
||||||
buf.append(o.toString()+'\n');
|
buf.append(o.toString()+'\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,37 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
|
|
||||||
public class RankingTreeServer implements CommandExecutor {
|
public class RankingTreeServer implements CommandExecutor {
|
||||||
|
|
||||||
private final static int STARTNR = 0;
|
private final static int STARTNR = 0;
|
||||||
private final static int NAME = 1;
|
private final static int NAME = 1;
|
||||||
private final static int JG = 2;
|
private final static int JG = 2;
|
||||||
private final static int COUNTRY = 3;
|
private final static int COUNTRY = 3;
|
||||||
private final static int TIME = 4;
|
private final static int TIME = 4;
|
||||||
Tree<Competitor> tree = new AVLSearchTree<>();
|
Tree<Competitor> tree = new AVLSearchTree<>();
|
||||||
|
|
||||||
public void load(Tree<Competitor> tree, String list) throws Exception {
|
public void load(Tree<Competitor> tree, String list) throws Exception {
|
||||||
String[] lines = list.split("\n");
|
String[] lines = list.split("\n");
|
||||||
for (int i = 0; i < lines.length; i++) {
|
for (int i = 0; i < lines.length; i++) {
|
||||||
String[] items = lines[i].split(";");
|
String[] items = lines[i].split(";");
|
||||||
Competitor c = new Competitor(Integer.parseInt(items[STARTNR]), // startNr
|
Competitor c = new Competitor(Integer.parseInt(items[STARTNR]), // startNr
|
||||||
items[NAME], // name
|
items[NAME], // name
|
||||||
Integer.parseInt(items[JG]), // jg
|
Integer.parseInt(items[JG]), // jg
|
||||||
items[COUNTRY], // country
|
items[COUNTRY], // country
|
||||||
items[TIME]); // time
|
items[TIME]); // time
|
||||||
tree.add(c);
|
tree.add(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String execute(String command) throws Exception {
|
public String execute(String command) throws Exception {
|
||||||
// list of all Competitors
|
// list of all Competitors
|
||||||
|
|
||||||
//read values from String
|
//read values from String
|
||||||
load(tree,command);
|
load(tree,command);
|
||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
Traversal<Competitor> trav = tree.traversal();
|
Traversal<Competitor> trav = tree.traversal();
|
||||||
Visitor<Competitor> vis = new MyRankingVisitor(buf);
|
Visitor<Competitor> vis = new MyRankingVisitor(buf);
|
||||||
trav.inorder(vis);
|
trav.inorder(vis);
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,144 +1,144 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
||||||
protected TreeNode<T> root;
|
protected TreeNode<T> root;
|
||||||
|
|
||||||
private TreeNode<T> insertAt(TreeNode<T> node, T x) {
|
private TreeNode<T> insertAt(TreeNode<T> node, T x) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
return new TreeNode<T>(x);
|
return new TreeNode<T>(x);
|
||||||
} else {
|
} else {
|
||||||
if (x.compareTo(node.element) <= 0) {
|
if (x.compareTo(node.element) <= 0) {
|
||||||
node.left = insertAt(node.left, x);
|
node.left = insertAt(node.left, x);
|
||||||
} else {
|
} else {
|
||||||
node.right = insertAt(node.right, x);
|
node.right = insertAt(node.right, x);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(T x) {
|
public void add(T x) {
|
||||||
root = insertAt(root, x);
|
root = insertAt(root, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
// find node to replace
|
// find node to replace
|
||||||
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
|
||||||
if (node.right != null) {
|
if (node.right != null) {
|
||||||
node.right = findRepAt(node.right,rep);
|
node.right = findRepAt(node.right,rep);
|
||||||
} else {
|
} else {
|
||||||
rep.element = node.element;
|
rep.element = node.element;
|
||||||
node = node.left;
|
node = node.left;
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove node
|
// remove node
|
||||||
private TreeNode<T> removeAt(TreeNode<T> node, T x,TreeNode<T> removed ) {
|
private TreeNode<T> removeAt(TreeNode<T> node, T x,TreeNode<T> removed ) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
if (x.compareTo(node.element) == 0) {
|
if (x.compareTo(node.element) == 0) {
|
||||||
// found
|
// found
|
||||||
removed.element = node.element;
|
removed.element = node.element;
|
||||||
if (node.left == null) {
|
if (node.left == null) {
|
||||||
node = node.right;
|
node = node.right;
|
||||||
} else if (node.right == null) {
|
} else if (node.right == null) {
|
||||||
node = node.left;
|
node = node.left;
|
||||||
} else {
|
} else {
|
||||||
node.left = findRepAt(node.left,node);
|
node.left = findRepAt(node.left,node);
|
||||||
}
|
}
|
||||||
} else if (x.compareTo(node.element) < 0) {
|
} else if (x.compareTo(node.element) < 0) {
|
||||||
// search left
|
// search left
|
||||||
node.left = removeAt(node.left, x, removed);
|
node.left = removeAt(node.left, x, removed);
|
||||||
} else {
|
} else {
|
||||||
// search right
|
// search right
|
||||||
node.right = removeAt(node.right, x, removed);
|
node.right = removeAt(node.right, x, removed);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T remove(T x) {
|
public T remove(T x) {
|
||||||
TreeNode<T> removed = new TreeNode<T>(null);
|
TreeNode<T> removed = new TreeNode<T>(null);
|
||||||
root = removeAt(root, x, removed);
|
root = removeAt(root, x, removed);
|
||||||
return removed.element;
|
return removed.element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return root == null;
|
return root == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Traversal<T> traversal() {
|
public Traversal<T> traversal() {
|
||||||
return new TreeTraversal<T>(root);
|
return new TreeTraversal<T>(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int calcHeight(TreeNode<T> node) {
|
protected int calcHeight(TreeNode<T> node) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return 1 + Math.max(calcHeight(node.left),calcHeight(node.right));
|
return 1 + Math.max(calcHeight(node.left),calcHeight(node.right));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int height() {
|
public int height() {
|
||||||
return calcHeight(root);
|
return calcHeight(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int calcSize(TreeNode p) {
|
protected int calcSize(TreeNode p) {
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return p.count + calcSize(p.left) + calcSize(p.right);
|
return p.count + calcSize(p.left) + calcSize(p.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return calcSize(root);
|
return calcSize(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean balanced(TreeNode<T> node) {
|
private boolean balanced(TreeNode<T> node) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return Math.abs(calcHeight(node.left) - calcHeight(node.right)) < 2
|
return Math.abs(calcHeight(node.left) - calcHeight(node.right)) < 2
|
||||||
&& balanced(node.left) && balanced(node.right);
|
&& balanced(node.left) && balanced(node.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean balanced() {
|
public boolean balanced() {
|
||||||
return balanced(root);
|
return balanced(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
// only for testing and debugging: show the structure of the tree
|
// only for testing and debugging: show the structure of the tree
|
||||||
public String printTree() {
|
public String printTree() {
|
||||||
StringBuilder out = new StringBuilder();
|
StringBuilder out = new StringBuilder();
|
||||||
if (root.right != null) {
|
if (root.right != null) {
|
||||||
printTree(root.right,out, true, "");
|
printTree(root.right,out, true, "");
|
||||||
}
|
}
|
||||||
out.append(root.element+"\n");
|
out.append(root.element+"\n");
|
||||||
if (root.left != null) {
|
if (root.left != null) {
|
||||||
printTree(root.left,out, false, "");
|
printTree(root.left,out, false, "");
|
||||||
}
|
}
|
||||||
return out.toString();
|
return out.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void printTree(TreeNode node, StringBuilder out, boolean isRight, String indent) {
|
private void printTree(TreeNode node, StringBuilder out, boolean isRight, String indent) {
|
||||||
if (node.right != null) {
|
if (node.right != null) {
|
||||||
printTree(node.right, out, true,
|
printTree(node.right, out, true,
|
||||||
indent + (isRight ? " " : " | "));
|
indent + (isRight ? " " : " | "));
|
||||||
}
|
}
|
||||||
out.append(indent);
|
out.append(indent);
|
||||||
if (isRight) {
|
if (isRight) {
|
||||||
out.append(" /");
|
out.append(" /");
|
||||||
} else {
|
} else {
|
||||||
out.append(" \\");
|
out.append(" \\");
|
||||||
}
|
}
|
||||||
out.append("----- ");
|
out.append("----- ");
|
||||||
out.append(node.element+"\n");
|
out.append(node.element+"\n");
|
||||||
if (node.left != null) {
|
if (node.left != null) {
|
||||||
printTree(node.left, out, false,
|
printTree(node.left, out, false,
|
||||||
indent + (isRight ? " | " : " "));
|
indent + (isRight ? " | " : " "));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
/* interface of Traversal ADT */
|
/* interface of Traversal ADT */
|
||||||
public interface Traversal<T extends Comparable<T>> {
|
public interface Traversal<T extends Comparable<T>> {
|
||||||
/* traverse elements of tree in preorder */
|
/* traverse elements of tree in preorder */
|
||||||
public void preorder(Visitor<T> vistor);
|
public void preorder(Visitor<T> vistor);
|
||||||
/* traverse elements of tree in inorder */
|
/* traverse elements of tree in inorder */
|
||||||
public void inorder(Visitor<T> vistor);
|
public void inorder(Visitor<T> vistor);
|
||||||
/* traverse elements of tree in postorder */
|
/* traverse elements of tree in postorder */
|
||||||
public void postorder(Visitor<T> vistor);
|
public void postorder(Visitor<T> vistor);
|
||||||
/* traverse elements of tree in levelorder */
|
/* traverse elements of tree in levelorder */
|
||||||
public void levelorder(Visitor<T> vistor);
|
public void levelorder(Visitor<T> vistor);
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/* interface of Tree ADT */
|
/* interface of Tree ADT */
|
||||||
public interface Tree<T extends Comparable<T>> {
|
public interface Tree<T extends Comparable<T>> {
|
||||||
/* add an element to the tree */
|
/* add an element to the tree */
|
||||||
void add(T o);
|
void add(T o);
|
||||||
/* remove an element; returns the element if found else return null */
|
/* remove an element; returns the element if found else return null */
|
||||||
T remove(T o);
|
T remove(T o);
|
||||||
/* test if tree is empty */
|
/* test if tree is empty */
|
||||||
boolean isEmpty();
|
boolean isEmpty();
|
||||||
/* returns instance of class that implements traversal interface */
|
/* returns instance of class that implements traversal interface */
|
||||||
Traversal<T> traversal();
|
Traversal<T> traversal();
|
||||||
/* number of elements */
|
/* number of elements */
|
||||||
int size();
|
int size();
|
||||||
/* height of the tree */
|
/* height of the tree */
|
||||||
int height();
|
int height();
|
||||||
/* is the tree balanced */
|
/* is the tree balanced */
|
||||||
boolean balanced();
|
boolean balanced();
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
class TreeNode<T extends Comparable<T>> {
|
class TreeNode<T extends Comparable<T>> {
|
||||||
T element;
|
T element;
|
||||||
TreeNode left, right;
|
TreeNode left, right;
|
||||||
int height;
|
int height;
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
TreeNode(T element){
|
TreeNode(T element){
|
||||||
this.element = element;
|
this.element = element;
|
||||||
this.count = 1;
|
this.count = 1;
|
||||||
this.height = 1;
|
this.height = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeNode(T element, TreeNode left, TreeNode right){
|
TreeNode(T element, TreeNode left, TreeNode right){
|
||||||
this(element); this.left = left; this.right = right;
|
this(element); this.left = left; this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
T getValue(){return element;}
|
T getValue(){return element;}
|
||||||
}
|
}
|
@ -1,68 +1,68 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
|
||||||
|
|
||||||
private TreeNode<T> root;
|
private TreeNode<T> root;
|
||||||
|
|
||||||
public TreeTraversal(TreeNode<T> root) {
|
public TreeTraversal(TreeNode<T> root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
private void inorder(TreeNode<T> node, Visitor<T> vis) {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
inorder(node.left, vis);
|
inorder(node.left, vis);
|
||||||
vis.visit(node.element);
|
vis.visit(node.element);
|
||||||
inorder(node.right, vis);
|
inorder(node.right, vis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void inorder(Visitor<T> vis) {
|
public void inorder(Visitor<T> vis) {
|
||||||
inorder(root, vis);
|
inorder(root, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
private void preorder(TreeNode<T> node, Visitor<T> vis) {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
vis.visit(node.element);
|
vis.visit(node.element);
|
||||||
preorder(node.left, vis);
|
preorder(node.left, vis);
|
||||||
preorder(node.right, vis);
|
preorder(node.right, vis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void preorder(Visitor<T> vis) {
|
public void preorder(Visitor<T> vis) {
|
||||||
preorder(root, vis);
|
preorder(root, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
private void postorder(TreeNode<T> node, Visitor<T> vis) {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
postorder(node.left, vis);
|
postorder(node.left, vis);
|
||||||
postorder(node.right, vis);
|
postorder(node.right, vis);
|
||||||
vis.visit(node.element);
|
vis.visit(node.element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void postorder(Visitor<T> vis) {
|
public void postorder(Visitor<T> vis) {
|
||||||
postorder(root, vis);
|
postorder(root, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
void levelorder(TreeNode<T> node, Visitor<T> visitor) {
|
||||||
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
q.add(node);
|
q.add(node);
|
||||||
}
|
}
|
||||||
while (!q.isEmpty()) {
|
while (!q.isEmpty()) {
|
||||||
node = q.remove();
|
node = q.remove();
|
||||||
visitor.visit(node.element);
|
visitor.visit(node.element);
|
||||||
if (node.left != null) {
|
if (node.left != null) {
|
||||||
q.add(node.left);
|
q.add(node.left);
|
||||||
}
|
}
|
||||||
if (node.right != null) {
|
if (node.right != null) {
|
||||||
q.add(node.right);
|
q.add(node.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void levelorder(Visitor<T> vis) {
|
public void levelorder(Visitor<T> vis) {
|
||||||
levelorder(root,vis);
|
levelorder(root,vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/* interface of visitor ADT */
|
/* interface of visitor ADT */
|
||||||
public interface Visitor<T extends Comparable<T>> {
|
public interface Visitor<T extends Comparable<T>> {
|
||||||
/* called for each element in the tree */
|
/* called for each element in the tree */
|
||||||
public void visit(T obj);
|
public void visit(T obj);
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,130 +1,130 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class AVLSearchTreeTest {
|
public class AVLSearchTreeTest {
|
||||||
Tree<String> tree;
|
Tree<String> tree;
|
||||||
|
|
||||||
private void init(Tree<String> tree) {
|
private void init(Tree<String> tree) {
|
||||||
tree.add("E");
|
tree.add("E");
|
||||||
tree.add("F");
|
tree.add("F");
|
||||||
tree.add("G");
|
tree.add("G");
|
||||||
tree.add("H");
|
tree.add("H");
|
||||||
tree.add("J");
|
tree.add("J");
|
||||||
tree.add("A");
|
tree.add("A");
|
||||||
tree.add("B");
|
tree.add("B");
|
||||||
tree.add("C");
|
tree.add("C");
|
||||||
tree.add("D");
|
tree.add("D");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
tree = new AVLSearchTree<String>();
|
tree = new AVLSearchTree<String>();
|
||||||
init(tree);
|
init(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInorder() {
|
public void testInorder() {
|
||||||
Visitor<String> v = new MyVisitor<String>();
|
Visitor<String> v = new MyVisitor<String>();
|
||||||
tree.traversal().inorder(v);
|
tree.traversal().inorder(v);
|
||||||
assertEquals("inorder", "ABCDEFGHJ", v.toString());
|
assertEquals("inorder", "ABCDEFGHJ", v.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPreorder() {
|
public void testPreorder() {
|
||||||
Visitor<String> v = new MyVisitor<String>();
|
Visitor<String> v = new MyVisitor<String>();
|
||||||
tree.traversal().preorder(v);
|
tree.traversal().preorder(v);
|
||||||
assertEquals("preorder", "FBADCEHGJ", v.toString());
|
assertEquals("preorder", "FBADCEHGJ", v.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPostorder() {
|
public void testPostorder() {
|
||||||
Visitor<String> v = new MyVisitor<String>();
|
Visitor<String> v = new MyVisitor<String>();
|
||||||
tree.traversal().postorder(v);
|
tree.traversal().postorder(v);
|
||||||
assertEquals("postorder", "ACEDBGJHF", v.toString());
|
assertEquals("postorder", "ACEDBGJHF", v.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLevelörder() {
|
public void testLevelörder() {
|
||||||
Visitor<String> v = new MyVisitor<String>();
|
Visitor<String> v = new MyVisitor<String>();
|
||||||
tree.traversal().levelorder(v);
|
tree.traversal().levelorder(v);
|
||||||
assertEquals("levelorder", "FBHADGJCE", v.toString());
|
assertEquals("levelorder", "FBHADGJCE", v.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeight() {
|
public void testHeight() {
|
||||||
assertEquals(4,tree.height());
|
assertEquals(4,tree.height());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBalanced() {
|
public void testBalanced() {
|
||||||
assertTrue(tree.balanced());
|
assertTrue(tree.balanced());
|
||||||
Tree<String> tree2 = new SortedBinaryTree<String> ();
|
Tree<String> tree2 = new SortedBinaryTree<String> ();
|
||||||
tree2.add("A");
|
tree2.add("A");
|
||||||
tree2.add("B");
|
tree2.add("B");
|
||||||
tree2.add("C");
|
tree2.add("C");
|
||||||
assertFalse(tree2.balanced());
|
assertFalse(tree2.balanced());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemove() {
|
public void testRemove() {
|
||||||
tree = new AVLSearchTree<String>();
|
tree = new AVLSearchTree<String>();
|
||||||
init(tree);
|
init(tree);
|
||||||
tree.remove("F");
|
tree.remove("F");
|
||||||
tree.remove("H");
|
tree.remove("H");
|
||||||
tree.remove("J");
|
tree.remove("J");
|
||||||
Visitor<String> v = new MyVisitor<String>();
|
Visitor<String> v = new MyVisitor<String>();
|
||||||
tree.traversal().inorder(v);
|
tree.traversal().inorder(v);
|
||||||
assertEquals("remove", "ABCDEG", v.toString());
|
assertEquals("remove", "ABCDEG", v.toString());
|
||||||
v = new MyVisitor<String>();
|
v = new MyVisitor<String>();
|
||||||
tree.traversal().levelorder(v);
|
tree.traversal().levelorder(v);
|
||||||
assertEquals("remove", "DBEACG", v.toString());
|
assertEquals("remove", "DBEACG", v.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMixed() {
|
public void testMixed() {
|
||||||
tree = new AVLSearchTree<String>();
|
tree = new AVLSearchTree<String>();
|
||||||
List<String> list = new LinkedList<>();
|
List<String> list = new LinkedList<>();
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
Character c = (char) ('A' + (Math.random() * 26));
|
Character c = (char) ('A' + (Math.random() * 26));
|
||||||
int op = (int) (Math.random() * 2);
|
int op = (int) (Math.random() * 2);
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case 0:
|
case 0:
|
||||||
list.add(c.toString());
|
list.add(c.toString());
|
||||||
tree.add(c.toString());
|
tree.add(c.toString());
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
list.remove(c.toString());
|
list.remove(c.toString());
|
||||||
tree.remove(c.toString());
|
tree.remove(c.toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertTrue(tree.balanced());
|
assertTrue(tree.balanced());
|
||||||
assertEquals(tree.size(),list.size());
|
assertEquals(tree.size(),list.size());
|
||||||
Collections.sort(list);
|
Collections.sort(list);
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
for (String s : list) {b.append(s);};
|
for (String s : list) {b.append(s);};
|
||||||
Visitor<String> v = new MyVisitor<String>();
|
Visitor<String> v = new MyVisitor<String>();
|
||||||
tree.traversal().inorder(v);
|
tree.traversal().inorder(v);
|
||||||
assertEquals("mixed",b.toString(), v.toString());
|
assertEquals("mixed",b.toString(), v.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyVisitor<T> implements Visitor<T> {
|
class MyVisitor<T> implements Visitor<T> {
|
||||||
StringBuilder output;
|
StringBuilder output;
|
||||||
|
|
||||||
MyVisitor() {
|
MyVisitor() {
|
||||||
output = new StringBuilder();
|
output = new StringBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(T s) {
|
public void visit(T s) {
|
||||||
output.append(s);
|
output.append(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user