Skip to content
Snippets Groups Projects
Commit 550cb61d authored by Haley Glavina's avatar Haley Glavina
Browse files

Fixed RedBlackTree bug for large integers

parent 459508d7
No related branches found
No related tags found
No related merge requests found
package search;
// The interface for a function that returns one field (piece of data) from a record
public interface Field<Key, Value> {
public Comparable<Key> field(Value a1);
public interface Field<Key extends Comparable<Key>, Value> {
public Key field(Value a1);
}
package search;
public interface GeneralCompare<T> {
public int compare(Comparable<T> a1, Comparable<T> a2);
public interface GeneralCompare<T extends Comparable<T>> {
public int compare(T a1, T a2);
}
package search;
// An abstract data type that represents a node in a Red Black Search Tree
public class Node<Key, Value>{
public class Node<Key extends Comparable<Key>, Value>{
private final Comparable<Key> key;
private final Key key;
private Value val;
private Node<Key, Value> left, right;
private int n;
......@@ -16,7 +16,7 @@ public class Node<Key, Value>{
* @param n How many nodes are in the subtree beneath this node (inclusive)
* @param color True represents a red connection between the current node and its parent, black represents false
*/
public Node(Comparable<Key> key, Value val, int n, boolean color){
public Node(Key key, Value val, int n, boolean color){
this.key = key;
this.val = val;
this.n = n;
......@@ -27,7 +27,7 @@ public class Node<Key, Value>{
* Getter for a node's key
* @return The node's key
*/
public Comparable<Key> key(){
public Key key(){
return this.key;
}
......
package search;
public class RedBlackTree<Key, Value> {
public class RedBlackTree<Key extends Comparable<Key>, Value> {
private Node<Key, Value> root; // Root of the tree
private GeneralCompare<Key> compare;
private Field<Key, Value> field;
// Main method only used for testing
/*
public static void main(String[] args) {
GeneralCompare<Integer> b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
......@@ -31,14 +31,15 @@ public class RedBlackTree<Key, Value> {
System.out.println("Root: " + myTree.root().key());
System.out.println("myTree.get(6).key(): " + (Integer) myTree.get((Comparable<Integer>) 6).key());
/*
Node h = myTree.root();
System.out.println(h.key());
while (h.right() != null) {
System.out.println(h.right().key());
h = h.right();
}
}
}*/
......@@ -58,7 +59,7 @@ public class RedBlackTree<Key, Value> {
* @param key Key pointing to the desired node
* @return A node containing who's key matches the input
*/
public Node<Key, Value> get(Comparable<Key> key) {
public Node<Key, Value> get(Key key) {
return get(this.root, key);
}
......@@ -68,11 +69,12 @@ public class RedBlackTree<Key, Value> {
* @param key Desired key to be searched for in a tree
* @return The node containing the key, returns null if the key is not found
*/
private Node<Key, Value> get(Node<Key, Value> node, Comparable<Key> key) {
if (node.key() == key)
private Node<Key, Value> get(Node<Key, Value> node, Key key) {
if (node.key().equals(key)) {
return node;
}
// If key is greater than current node, look right
if (this.compare.compare(node.key(), key) < 0)
if (this.compare.compare(node.key(), (Key) key) < 0)
if (node.right() != null)
return get(node.right(), key);
else
......
......@@ -67,14 +67,26 @@ public class RedBlackTreeTest {
for(int i = 0; i < 4; i++){
myTree.put(x[i]);
}
assert(myTree.get((Comparable<Integer>) 6) == null);
assert(myTree.get(6) == null);
// Add remaining nodes, expected get(6) result is {6, 6}
for(int i = 5; i < x.length; i++){
myTree.put(x[i]);
}
assert(myTree.get(6).key() == (Comparable<Integer>) 6);
assert(myTree.get(6).key() == 6);
// ==== TESTING WEIRD VALUES ==== //
Integer[][] y = {{100, 1}, {200000, 2}, {-3, 3}, {45, 4}, {-125, 2}};
RedBlackTree<Integer, Integer[]> newTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
for(int i = 0; i < 5; i++){
newTree.put(y[i]);
}
assert(newTree.get(6) == null);
assert(newTree.get(-3).key() == (-3));
assert(newTree.get(100).key() == 100);
assert(newTree.get(-125).key() == (-125));
assert(newTree.get(200000).key() == 200000);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment