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
ca56cee4
Commit
ca56cee4
authored
7 years ago
by
Haley Glavina
Browse files
Options
Downloads
Patches
Plain Diff
Object implementation of RedBlackTree
parent
df4e54fd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bin/search/RedBlackTree.class
+0
-0
0 additions, 0 deletions
bin/search/RedBlackTree.class
src/search/Field.java
+15
-0
15 additions, 0 deletions
src/search/Field.java
src/search/RedBlackTree.java
+59
-68
59 additions, 68 deletions
src/search/RedBlackTree.java
with
74 additions
and
68 deletions
bin/search/RedBlackTree.class
+
0
−
0
View file @
ca56cee4
No preview for this file type
This diff is collapsed.
Click to expand it.
src/search/Field.java
0 → 100644
+
15
−
0
View file @
ca56cee4
package
search
;
public
class
Field
{
// Access desired field for comparison (the field to be used as a key for each record)
public
static
Comparable
field
(
Comparable
fieldT
,
Comparable
[]
record
)
{
String
type
=
fieldT
.
getClass
().
getSimpleName
();
if
(
type
==
"Integer"
)
return
record
[
0
];
if
(
type
==
"String"
)
return
record
[
1
];
return
null
;
}
}
This diff is collapsed.
Click to expand it.
src/search/RedBlackTree.java
+
59
−
68
View file @
ca56cee4
package
search
;
public
class
RedBlackTree
<
Key
,
Value
>
{
private
static
Node
root
;
// Root of the tree
public
class
RedBlackTree
<
Key
,
Value
,
T
>
{
private
Node
root
;
// Root of the tree
private
GeneralCompare
compareType
;
private
Comparable
<
T
>
fieldType
;
public
static
void
main
(
String
[]
args
)
{
GeneralCompare
<
Integer
>
b1
;
b1
=
(
a1
,
a2
)
->
(
Integer
)
a1
-
(
Integer
)
a2
;
Integer
[]
x
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
};
RedBlackTree
myTree
=
RedBlackTree
(
x
[
0
],
x
[
0
],
b1
);
for
(
int
i
=
0
;
i
<
x
.
length
;
i
++){
put
(
x
[
i
],
x
,
b1
);
}
...
...
@@ -18,48 +21,75 @@ public class RedBlackTree<Key, Value> {
*/
}
private
static
<
T
>
int
size
(
Node
h
){
if
(
h
==
null
)
return
0
;
else
return
h
.
n
();
// RedBlackTree Constructor
public
RedBlackTree
<
Key
,
Value
,
T
>
RedBlackTree
(
Comparable
<
T
>
fieldT
,
Comparable
<
T
>[]
record
,
GeneralCompare
gc
)
{
root
.
val
(
record
);
compareType
=
gc
;
fieldType
=
fieldT
;
root
.
color
(
false
);
root
.
key
(
Field
.
field
(
fieldType
,
record
));
root
.
left
(
null
);
root
.
right
(
null
);
return
this
;
}
//Get root of the tree
// Get root of a tree
public
Node
root
()
{
return
this
.
root
();
}
public
static
<
T
>
void
put
(
Comparable
<
T
>
key
,
Comparable
<
T
>[]
val
,
GeneralCompare
gc
){
root
=
put
(
root
,
key
,
val
,
gc
);
root
.
color
(
false
);
System
.
out
.
println
(
root
.
key
());
/*
if (((int) key) == 9) {
System.out.println(root.key());
while (root.right() != null) {
System.out.println(root.right().key());
}
}*/
public
void
put
(
Comparable
<
T
>[]
val
){
put
(
root
,
Field
.
field
(
fieldType
,
val
),
val
,
this
.
compareType
);
}
private
static
boolean
isRed
(
Node
x
){
private
Node
put
(
Node
h
,
Comparable
<
T
>
key
,
Comparable
<
T
>[]
val
,
GeneralCompare
<
T
>
gc
){
if
(
h
==
null
)
return
new
Node
(
key
,
val
,
1
,
true
);
int
n
=
h
.
n
();
Node
newNode
=
new
Node
(
key
,
val
,
n
,
true
);
int
cmp
=
gc
.
compare
(
key
,
h
.
key
());
if
(
cmp
<
0
)
h
.
left
(
put
(
h
.
left
(),
Field
.
field
(
fieldType
,
val
),
val
,
gc
));
else
if
(
cmp
>
0
)
h
.
right
(
put
(
h
.
right
(),
Field
.
field
(
fieldType
,
val
),
val
,
gc
));
else
if
(
n
>
2
){
if
(
isRed
(
h
.
right
())
&&
!
isRed
(
h
.
left
()))
h
=
rotateLeft
(
h
);
if
(
isRed
(
h
.
left
())
&&
isRed
(
h
.
left
().
left
()))
h
=
rotateRight
(
h
);
if
(
isRed
(
h
.
left
())
&&
isRed
(
h
.
right
()))
flipColors
(
h
);
}
h
.
n
(
h
.
left
().
n
()
+
h
.
right
().
n
()
+
1
);
return
h
;
}
// Check if the link to a node's parent is red
private
boolean
isRed
(
Node
x
){
if
(
x
==
null
)
return
false
;
return
true
;
}
public
static
Node
rotateLeft
(
Node
h
){
public
Node
rotateLeft
(
Node
h
){
Node
x
=
h
.
right
();
h
.
right
(
x
.
left
());
x
.
left
(
h
);
x
.
color
(
h
.
color
());
h
.
color
(
true
);
x
.
n
(
h
.
n
());
h
.
n
(
1
+
size
(
h
.
left
())
+
size
(
h
.
right
()));
h
.
n
(
1
+
h
.
left
()
.
n
(
)
+
h
.
right
()
.
n
(
));
return
x
;
}
public
static
Node
rotateRight
(
Node
h
){
Node
x
=
h
.
left
();
h
.
left
(
x
.
right
());
...
...
@@ -67,55 +97,16 @@ public class RedBlackTree<Key, Value> {
x
.
color
(
h
.
color
());
h
.
color
(
true
);
x
.
n
(
h
.
n
());
h
.
n
(
1
+
size
(
h
.
left
())
+
size
(
h
.
right
()));
h
.
n
(
1
+
h
.
left
()
.
n
(
)
+
h
.
right
()
.
n
(
));
return
x
;
}
private
static
void
flipColors
(
Node
h
){
private
void
flipColors
(
Node
h
){
if
(
h
.
left
()
!=
null
&&
h
.
right
()
!=
null
){
h
.
left
().
color
(
false
);
h
.
right
().
color
(
false
);
h
.
color
(
true
);
}
}
private
static
<
T
>
Node
put
(
Node
h
,
Comparable
<
T
>
key
,
Comparable
<
T
>[]
val
,
GeneralCompare
<
T
>
gc
){
if
(
h
==
null
)
return
new
Node
(
key
,
val
,
1
,
true
);
int
n
=
size
(
h
);
Node
newNode
=
new
Node
(
key
,
val
,
n
,
true
);
int
cmp
=
gc
.
compare
(
key
,
h
.
key
());
if
(
cmp
<
0
)
{
while
(
cmp
<
0
)
{
h
=
h
.
left
();
cmp
=
gc
.
compare
(
key
,
h
.
key
());
}
h
.
left
(
newNode
);
}
if
(
cmp
>
0
)
{
while
(
cmp
>
0
)
{
h
=
h
.
right
();
cmp
=
gc
.
compare
(
key
,
h
.
key
());
}
h
.
right
(
newNode
);
}
else
h
.
val
(
val
);
if
(
n
>
2
){
if
(
isRed
(
h
.
right
())
&&
!
isRed
(
h
.
left
()))
h
=
rotateLeft
(
h
);
if
(
isRed
(
h
.
left
())
&&
isRed
(
h
.
left
().
left
()))
h
=
rotateRight
(
h
);
if
(
isRed
(
h
.
left
())
&&
isRed
(
h
.
right
()))
flipColors
(
h
);
}
h
.
n
(
size
(
h
.
left
())
+
size
(
h
.
right
())
+
1
);
return
h
;
}
}
}
\ No newline at end of file
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