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

get method added to redBlackTree

parent dc161a21
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ public class RedBlackTree<Key, Value> {
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;
......@@ -16,9 +16,22 @@ public class RedBlackTree<Key, Value> {
Integer[][] x = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}};
RedBlackTree<Integer, Integer[]> myTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
for(int i = 0; i < x.length; i++){
// Add first 5 nodes, expected get(6) result is null
for(int i = 0; i < 4; i++){
myTree.put(x[i]);
}
assert(myTree.get((Comparable<Integer>) 6) == null);
// Add remaining nodes, expected get(6) result is {6, 6}
for(int i = 5; i < x.length; i++){
//System.out.println(x[i][0]);
myTree.put(x[i]);
}
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) {
......@@ -26,7 +39,7 @@ public class RedBlackTree<Key, Value> {
h = h.right();
}
}
*/
......@@ -40,6 +53,41 @@ public class RedBlackTree<Key, Value> {
field = fld;
}
/**
* Wrapper method for retrieving the node that matches a desired key
* @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) {
return get(this.root, key);
}
/**
* Finds the node in a tree whose key matches a desired key (if the matching node exists)
* @param node Root of the subtree being searched
* @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)
return node;
// If key is greater than current node, look right
if (this.compare.compare(node.key(), key) < 0)
if (node.right() != null)
return get(node.right(), key);
else
return null;
// If key is smaller than current node, look left
else if (this.compare.compare(node.key(), key) > 0)
if (node.left() != null)
return get(node.left(), key);
else
return null;
// If node == null, key does not exist in the tree
return null;
}
/**
* Getter method for the root of a tree
* @return The root of a tree object
......
......@@ -54,6 +54,28 @@ public class RedBlackTreeTest {
}
assert((Integer) myTree.root().key() == 4);
}
/**
* Test method for {@link search.RedBlackTree#get(Node<Key, Value> node, Comparable<Key> key)}.
*/
@Test
public void testGet() {
Integer[][] x = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}};
RedBlackTree<Integer, Integer[]> myTree = new RedBlackTree<Integer, Integer[]>(fld, b1);
// Add first 5 nodes, expected get(6) result is null
for(int i = 0; i < 4; i++){
myTree.put(x[i]);
}
assert(myTree.get((Comparable<Integer>) 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);
}
/**
* Test method for {@link search.RedBlackTree#put(java.lang.Object)}.
......
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