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
960b23c7
Commit
960b23c7
authored
7 years ago
by
Christopher Schankula
Browse files
Options
Downloads
Patches
Plain Diff
serialize and deserialize the KDTree
parent
b02f24ed
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
src/sandbox/Point.java
+7
-1
7 additions, 1 deletion
src/sandbox/Point.java
src/sort/GeneralCompare.java
+3
-1
3 additions, 1 deletion
src/sort/GeneralCompare.java
src/sort/KDT.java
+68
-12
68 additions, 12 deletions
src/sort/KDT.java
with
78 additions
and
14 deletions
src/sandbox/Point.java
+
7
−
1
View file @
960b23c7
package
sandbox
;
public
class
Point
implements
Comparable
<
Point
>
{
import
java.io.Serializable
;
public
class
Point
implements
Comparable
<
Point
>,
Serializable
{
/**
*
*/
private
static
final
long
serialVersionUID
=
5361956730616676054L
;
private
final
int
x
;
private
final
int
y
;
...
...
This diff is collapsed.
Click to expand it.
src/sort/GeneralCompare.java
+
3
−
1
View file @
960b23c7
package
sort
;
public
interface
GeneralCompare
<
T
>
{
import
java.io.Serializable
;
public
interface
GeneralCompare
<
T
>
extends
Serializable
{
/**
* Compare two Comparable elements based on an arbitrary ordering definition.
* @param a1 The first value to be compared.
...
...
This diff is collapsed.
Click to expand it.
src/sort/KDT.java
+
68
−
12
View file @
960b23c7
package
sort
;
import
sandbox.Point
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectOutputStream
;
import
java.io.Serializable
;
import
java.util.ArrayList
;
public
class
KDT
<
KeyVal
extends
Comparable
<
KeyVal
>>
{
Node
root
;
public
class
KDT
<
KeyVal
extends
Comparable
<
KeyVal
>>
implements
Serializable
{
/**
*
*/
private
static
final
long
serialVersionUID
=
8807259801436570835L
;
/**
*
*/
KDNode
root
;
ArrayList
<
GeneralCompare
<
KeyVal
>>
axes
;
public
static
void
main
(
String
[]
args
)
{
...
...
@@ -26,7 +40,8 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
Point
[]
pts
=
{
p1
,
p2
,
p3
,
p4
,
p5
,
p6
};
KDT
<
Point
>
kdt
=
new
KDT
<
Point
>(
axes
,
pts
);
//KDT<Point> kdt = new KDT<Point>(axes, pts);
KDT
<
Point
>
kdt
=
new
KDT
<
Point
>(
"kdtree.ser"
);
System
.
out
.
println
(
kdt
.
size
());
System
.
out
.
println
(
kdt
.
height
());
...
...
@@ -49,25 +64,66 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
}
System
.
out
.
println
(
kdt
.
toString
());
try
{
FileOutputStream
fileOut
=
new
FileOutputStream
(
"kdtree.ser"
);
ObjectOutputStream
out
=
new
ObjectOutputStream
(
fileOut
);
out
.
writeObject
(
kdt
);
out
.
close
();
fileOut
.
close
();
System
.
out
.
printf
(
"Serialized data is saved in /tmp/kdtree.ser"
);
}
catch
(
IOException
i
)
{
i
.
printStackTrace
();
}
}
private
class
Node
{
private
class
KDNode
implements
Serializable
{
/**
*
*/
private
static
final
long
serialVersionUID
=
-
664511393872278542L
;
/**
*
*/
private
KeyVal
keyval
;
private
Node
left
,
right
;
private
KD
Node
left
,
right
;
private
int
n
;
public
Node
(
KeyVal
keyval
,
int
n
)
{
public
KD
Node
(
KeyVal
keyval
,
int
n
)
{
this
.
keyval
=
keyval
;
this
.
n
=
n
;
}
}
/**
* Load a kd-tree from a serialized file.
* @param fn
*/
public
KDT
(
String
fn
)
{
KDT
<
KeyVal
>
kdt
=
null
;
try
{
FileInputStream
fileIn
=
new
FileInputStream
(
fn
);
ObjectInputStream
in
=
new
ObjectInputStream
(
fileIn
);
kdt
=
(
KDT
<
KeyVal
>)
in
.
readObject
();
in
.
close
();
fileIn
.
close
();
}
catch
(
IOException
i
)
{
i
.
printStackTrace
();
}
catch
(
ClassNotFoundException
c
)
{
System
.
out
.
println
(
"Employee class not found"
);
c
.
printStackTrace
();
}
this
.
root
=
kdt
.
root
;
this
.
axes
=
kdt
.
axes
;
}
public
KDT
(
ArrayList
<
GeneralCompare
<
KeyVal
>>
axes
,
Comparable
<
KeyVal
>[]
keyvals
)
{
this
.
axes
=
axes
;
root
=
buildTree
(
keyvals
,
0
,
keyvals
.
length
-
1
,
0
);
}
private
Node
buildTree
(
Comparable
<
KeyVal
>[]
keyvals
,
int
lo
,
int
hi
,
int
depth
)
{
private
KD
Node
buildTree
(
Comparable
<
KeyVal
>[]
keyvals
,
int
lo
,
int
hi
,
int
depth
)
{
if
(
lo
>
hi
)
return
null
;
int
axis
=
depth
%
getK
();
...
...
@@ -76,7 +132,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
KeyVal
median
=
(
KeyVal
)
keyvals
[
mid
];
//TODO: fix size
Node
newNode
=
new
Node
(
median
,
0
);
KD
Node
newNode
=
new
KD
Node
(
median
,
0
);
newNode
.
left
=
buildTree
(
keyvals
,
lo
,
mid
-
1
,
depth
+
1
);
newNode
.
right
=
buildTree
(
keyvals
,
mid
+
1
,
hi
,
depth
+
1
);
...
...
@@ -90,7 +146,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return
result
;
}
private
void
rangeSearch
(
Node
x
,
ArrayList
<
GeneralRange
<
KeyVal
>>
range
,
ArrayList
<
KeyVal
>
result
,
int
depth
)
{
private
void
rangeSearch
(
KD
Node
x
,
ArrayList
<
GeneralRange
<
KeyVal
>>
range
,
ArrayList
<
KeyVal
>
result
,
int
depth
)
{
if
(
x
==
null
)
return
;
int
axis
=
depth
%
getK
();
GeneralRange
<
KeyVal
>
rg
=
range
.
get
(
axis
);
...
...
@@ -127,12 +183,12 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return
height
(
root
);
}
private
int
height
(
Node
x
)
{
private
int
height
(
KD
Node
x
)
{
if
(
x
==
null
)
return
0
;
return
1
+
Math
.
max
(
height
(
x
.
left
),
height
(
x
.
right
));
}
private
int
size
(
Node
x
)
{
private
int
size
(
KD
Node
x
)
{
if
(
x
==
null
)
return
0
;
else
return
x
.
n
;
}
...
...
@@ -145,7 +201,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return
toString
(
root
,
""
);
}
private
String
toString
(
Node
x
,
String
depth
)
{
private
String
toString
(
KD
Node
x
,
String
depth
)
{
if
(
x
==
null
)
return
depth
+
"null\n"
;
String
result
=
""
;
result
+=
depth
+
x
.
keyval
.
toString
()
+
"\n"
;
...
...
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