uebung_03: implement Task 3 (BST) and Task 4 (IntHashSet)
Task 3 adds IntBinarySearchTree with iterative add/contains and a test class covering empty trees, duplicates, and degenerate ascending and descending insertion orders. Task 4 adds IntHashSet backed by an IntLinkedList bucket array with a 0.7 load factor, Math.floorMod-based hashing for negative-int safety, doubling resize that rehashes via a private addWithoutResize helper, and a test class covering negatives, Integer.MIN_VALUE, forced collisions on bucket 0, and 100-element inserts spanning three resizes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
54
uebung_03/src/IntBinarySearchTree.java
Normal file
54
uebung_03/src/IntBinarySearchTree.java
Normal file
@@ -0,0 +1,54 @@
|
||||
public class IntBinarySearchTree {
|
||||
IntTreeNode root;
|
||||
|
||||
public IntBinarySearchTree(){
|
||||
}
|
||||
|
||||
public void add(int value){
|
||||
if (root == null){
|
||||
root = new IntTreeNode(value);
|
||||
return;
|
||||
}
|
||||
|
||||
IntTreeNode currentRelativRoot = root;
|
||||
while (true) {
|
||||
if(value == currentRelativRoot.value){
|
||||
return;
|
||||
}
|
||||
if (value < currentRelativRoot.value) {
|
||||
if (currentRelativRoot.left == null) {
|
||||
currentRelativRoot.left = new IntTreeNode(value);
|
||||
return;
|
||||
} else currentRelativRoot = currentRelativRoot.left;
|
||||
|
||||
} else {
|
||||
if (currentRelativRoot.right == null) {
|
||||
currentRelativRoot.right = new IntTreeNode(value);
|
||||
return;
|
||||
} else currentRelativRoot = currentRelativRoot.right;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
public boolean contains(int value){
|
||||
if (root == null) return false;
|
||||
IntTreeNode currentRelativRoot = root;
|
||||
while (true){
|
||||
if(value == currentRelativRoot.value){
|
||||
return true;
|
||||
}
|
||||
if (value < currentRelativRoot.value) {
|
||||
if (currentRelativRoot.left == null) {
|
||||
return false;
|
||||
} else currentRelativRoot = currentRelativRoot.left;
|
||||
|
||||
} else {
|
||||
if (currentRelativRoot.right == null) {
|
||||
return false;
|
||||
} else currentRelativRoot = currentRelativRoot.right;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user