Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2XB3FinalProject
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Schankula
2XB3FinalProject
Commits
459508d7
Commit
459508d7
authored
7 years ago
by
Haley Glavina
Browse files
Options
Downloads
Patches
Plain Diff
get method added to redBlackTree
parent
dc161a21
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/search/RedBlackTree.java
+51
-3
51 additions, 3 deletions
src/search/RedBlackTree.java
src/search/RedBlackTreeTest.java
+22
-0
22 additions, 0 deletions
src/search/RedBlackTreeTest.java
with
73 additions
and
3 deletions
src/search/RedBlackTree.java
+
51
−
3
View file @
459508d7
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
src/search/RedBlackTreeTest.java
+
22
−
0
View file @
459508d7
...
...
@@ -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)}.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment