Compare commits
4 commits
0a4895c538
...
ac7372738c
Author | SHA1 | Date | |
---|---|---|---|
ac7372738c | |||
532788980e | |||
7a8e7a973d | |||
63da19757e |
2 changed files with 38 additions and 17 deletions
|
@ -3,13 +3,20 @@ package ch.nuth.zhaw.exbox;
|
|||
/**
|
||||
* Implements an AVL tree.
|
||||
* Note that all "matching" is based on the compareTo method.
|
||||
*
|
||||
* @author Mark Allen Weiss
|
||||
* Generic K.Rege
|
||||
*/
|
||||
public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T> {
|
||||
private boolean balanced(TreeNode<T> node) {
|
||||
// TODO Implement (6.4)
|
||||
if (node == null) {
|
||||
return true;
|
||||
} else if (balanced(node.left) && balanced(node.right)) {
|
||||
return Math.abs(calcHeight(node.left) - calcHeight(node.right)) < 2;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean balanced() {
|
||||
|
@ -19,7 +26,7 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
|
|||
@Override
|
||||
protected int calcSize(TreeNode<T> p) {
|
||||
// TODO Implement (6.2)
|
||||
return super.calcSize(p);
|
||||
return super.calcSize(p) + p.values.size() - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,6 +38,7 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
|
|||
|
||||
/**
|
||||
* Insert into the tree; duplicates are ignored.
|
||||
*
|
||||
* @param element the item to insert.
|
||||
*/
|
||||
public void add(T element) {
|
||||
|
@ -63,6 +71,7 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
|
|||
|
||||
/**
|
||||
* Internal method to insert into a subtree.
|
||||
*
|
||||
* @param element the item to insert.
|
||||
* @param p the node that roots the tree.
|
||||
* @return the new root.
|
||||
|
@ -88,6 +97,7 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
|
|||
|
||||
// find node to replace
|
||||
private TreeNode<T> rep;
|
||||
|
||||
private TreeNode<T> findRepAt(TreeNode<T> node) {
|
||||
if (node.right != null) {
|
||||
node.right = findRepAt(node.right);
|
||||
|
@ -129,13 +139,15 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
|
|||
// search right
|
||||
node.right = removeAt(node.right, x);
|
||||
}
|
||||
|
||||
// TODO Implement (6.5)
|
||||
return node;
|
||||
return balance(node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove from the tree. Nothing is done if x is not found.
|
||||
*
|
||||
* @param x the item to remove.
|
||||
*/
|
||||
public T remove(T x) {
|
||||
|
|
|
@ -74,14 +74,19 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
|||
}
|
||||
|
||||
protected int calcHeight(TreeNode<T> node) {
|
||||
int result = 0;
|
||||
if (node == null) {
|
||||
return 0;
|
||||
} else {
|
||||
int result = 1;
|
||||
|
||||
if (node.left != null || node.right != null) {
|
||||
result = Stream.of(node.left, node.right).filter(
|
||||
(innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max().getAsInt();
|
||||
result += Stream.of(node.left, node.right).filter(
|
||||
(innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max()
|
||||
.getAsInt();
|
||||
}
|
||||
|
||||
return result + 1;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public int height() {
|
||||
|
@ -89,7 +94,10 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
|||
}
|
||||
|
||||
protected int calcSize(TreeNode<T> p) {
|
||||
int result = 1;
|
||||
int result = 0;
|
||||
|
||||
if (p != null) {
|
||||
result++;
|
||||
|
||||
if (p.left != null) {
|
||||
result += calcSize(p.left);
|
||||
|
@ -98,6 +106,7 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
|||
if (p.right != null) {
|
||||
result += calcSize(p.right);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue