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
f017b687
Commit
f017b687
authored
7 years ago
by
Haley Glavina
Browse files
Options
Downloads
Patches
Plain Diff
RbTree works
parent
963820c3
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/search/RedBlackTree.java
+17
-15
17 additions, 15 deletions
src/search/RedBlackTree.java
with
17 additions
and
15 deletions
src/search/RedBlackTree.java
+
17
−
15
View file @
f017b687
...
...
@@ -16,12 +16,12 @@ public class RedBlackTree<Key, Value> {
RedBlackTree
<
Integer
,
Integer
[]>
myTree
=
new
RedBlackTree
<
Integer
,
Integer
[]>(
fld
,
b1
);
for
(
int
i
=
0
;
i
<
x
.
length
;
i
++){
myTree
.
put
(
x
[
i
]);
System
.
out
.
println
(
x
[
i
][
0
]);
System
.
out
.
println
(
" root: "
+
myTree
.
root
()
.
key
())
;
if
(
myTree
.
root
().
left
()
!=
null
)
System
.
out
.
println
(
" left: "
+
myTree
.
root
().
left
().
key
());
if
(
myTree
.
root
().
right
()
!=
null
)
System
.
out
.
println
(
"right: "
+
myTree
.
root
().
right
().
key
()
);
}
Node
h
=
myTree
.
root
();
System
.
out
.
println
(
h
.
key
());
while
(
h
.
left
()
!=
null
)
{
System
.
out
.
println
(
h
.
left
().
key
());
h
=
h
.
left
(
);
}
}
...
...
@@ -50,8 +50,8 @@ public class RedBlackTree<Key, Value> {
* @param val - A new record
*/
public
void
put
(
Value
val
){
Node
<
Key
,
Value
>
newNode
=
new
Node
(
field
.
field
(
val
),
val
,
0
,
true
);
put
(
root
,
newNode
);
Node
<
Key
,
Value
>
newNode
=
new
Node
<
Key
,
Value
>
(
field
.
field
(
val
),
val
,
1
,
true
);
root
=
put
(
root
,
newNode
);
}
/**
...
...
@@ -63,35 +63,37 @@ public class RedBlackTree<Key, Value> {
* @return
*/
private
Node
<
Key
,
Value
>
put
(
Node
<
Key
,
Value
>
h
,
Node
<
Key
,
Value
>
newNode
){
System
.
out
.
println
(
"Newnode key: "
+
newNode
.
key
());
// Placing the first node in a tree
if
(
root
==
null
)
{
root
=
newNode
;
root
.
color
(
false
);
return
root
;
}
// Place new element in the tree
int
cmp
=
compare
.
compare
(
newNode
.
key
(),
h
.
key
());
if
(
cmp
<
0
&
(
h
.
left
()
==
null
))
if
(
cmp
<
0
&
&
(
h
.
left
()
==
null
))
h
.
left
(
newNode
);
else
if
(
cmp
<
0
)
h
.
left
(
put
(
h
.
left
(),
newNode
));
else
if
(
cmp
>
0
&
(
h
.
right
()
==
null
))
else
if
(
cmp
>
0
&
&
(
h
.
right
()
==
null
))
h
.
right
(
newNode
);
else
if
(
cmp
>
0
)
h
.
right
(
put
(
h
.
right
(),
newNode
));
else
h
=
newNode
;
System
.
out
.
println
(
"h.right: "
+
h
.
right
().
key
());
//System.out.println("h.right: " + h.right().key());
// Rearrange the tree to maintain balance
//
if(
n
> 2){
if
(
h
.
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
);
//
}
}
// Increment how many nodes are in a subtree
if
((
h
.
left
()
!=
null
)
&&
(
h
.
right
()
!=
null
))
...
...
@@ -123,7 +125,7 @@ public class RedBlackTree<Key, Value> {
* @return New root of the rotated segment
*/
public
Node
<
Key
,
Value
>
rotateLeft
(
Node
<
Key
,
Value
>
h
){
//
System.out.println("Rotate left!");
System
.
out
.
println
(
"Rotate left!"
);
Node
<
Key
,
Value
>
x
=
h
.
right
();
h
.
right
(
x
.
left
());
x
.
left
(
h
);
...
...
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