package rtree;
import java.util.*;
/**
* To create a new RTree use the first two constructors. You must specify the dimension, the fill factor as
* a float between 0 and 0.5 (0 to 50% capacity) and the variant of the RTree which is one of:
* <ul>
* <li>RTREE_QUADRATIC</li>
* </ul>
* The first constructor creates by default a new memory resident page file. The second constructor takes
* the page file as an argument. If the given page file is not empty, then all data are deleted.
* <p>
* The third constructor initializes the RTree from an already filled page file. Thus, you may store the
* RTree into a persistent page file and recreate it again at any time.
* <p>
* Created: Tue May 18 12:57:35 1999
* <p>
* @author Hadjieleftheriou Marios
* @version 1.003
*/
public class RTree {
public final String version = "1.003";
public final String date = "December 7th 1999";
/** Page file where data is stored. */
protected PageFile file = null;
/** static identifier used for the parent of the root node. */
public static final int NIL = -1;
/** Available RTree variants. */
public static final int RTREE_LINEAR = 0;
public static final int RTREE_QUADRATIC = 1;
public static final int RTREE_EXPONENTIAL = 2;
public static final int RSTAR = 3;
/**
* Creates a memory resident RTree with an empty root node, which is stored in page 0 and has a
* parent identifier equal to NIL. A new memory page file is created and used as a storage manager
* by default.
*
* @param dimension The data dimension.
* @param fillFactor A percentage between 0 and 0.5. Used to calculate minimum number of entries
* present in each node.
* @param capacity The maximun number of entries that each node can hold.
* @param type The RTree variant to use.
*/
public RTree(int dimension, float fillFactor, int capacity, int type) {
this(dimension, fillFactor, capacity, new MemoryPageFile(), type);
}
/**
* Creates a new RTree with an empty root node, which is stored in page 0 and has
* a parent identifier equal to NIL. The given page file is used for storing the rtree nodes.
* If the page file is not empty, than all entries will be overwritten.
*
* @param dimension The data dimension.
* @param fillFactor A percentage between 0 and 0.5. Used to calculate minimum number of entries
* present in each node.
* @param capacity The maximun number of entries that each node can hold.
* @param file The page file to use for storing the nodes of the rtree.
* @param type The RTree variant to use.
*/
public RTree(int dimension, float fillFactor, int capacity, PageFile file, int type) {
if (dimension <= 1) {
throw new IllegalArgumentException("Dimension must be larger than 1.");
}
if (fillFactor < 0 || fillFactor > 0.5) {
throw new IllegalArgumentException("Fill factor must be between 0 and 0.5.");
}
if (capacity <= 1) {
throw new IllegalArgumentException("Capacity must be larger than 1.");
}
if (type != RTREE_QUADRATIC /*&& type != RTREE_LINEAR && type != RTREE_EXPONENTIAL && type != RSTAR*/) {
throw new IllegalArgumentException("Invalid tree type.");
}
if (file.tree != null) {
throw new IllegalArgumentException("PageFile already in use by another rtree instance.");
}
file.initialize(this, dimension, fillFactor, capacity, type);
this.file = file;
Leaf root = new Leaf(this, NIL, 0);
file.writeNode(root);
}
/**
* Creates an rtree from an already initialized page file, probably stored into persistent storage.
*/
public RTree(PageFile file) {
if (file.tree != null) {
throw new IllegalArgumentException("PageFile already in use by another rtree instance.");
}
if (file.treeType == -1) {
throw new IllegalArgumentException("PageFile is empty. Use some other RTree constructor.");
}
file.tree = this;
this.file = file;
}
/**
* Retruns the maximun capacity of each Node.
*/
public int getNodeCapacity() {
return file.nodeCapacity;
}
/**
* Returns the percentage between 0 and 0.5, used to calculate minimum number of entries
* present in each node.
*/
public float getFillFactor() {
return file.fillFactor;
}
/**
* Returns the data dimension.
*/
public int getDimension() {
return file.dimension;
}
/**
* Returns the page length.
*/
public int getPageSize() {
return file.pageSize;
}
/**
* Returns the level of the root Node, which signifies the level of the whole tree. Loads one
* page into main memory.
*/
public int getTreeLevel() {
return file.readNode(0).getLevel();
}
/**
* Returns the RTree variant used.
*/
public int getTreeType() {
return file.treeType;
}
/**
* Inserts a HyperCube into the tree, pointing to the data stored at the given page number.
*
* @param h The hypercube to insert.
* @param page The page where the real data is stored.
* @return The page number of the Leaf where the hypercube was inserted (the parent of the
* data entry.)
*/
public int insert(HyperCube h, int page) {
if (h == null) {
throw new IllegalArgumentException("HyperCube cannot be null.");
}
if (h.getDimension() != file.dimension) {
throw new IllegalArgumentException("HyperCube dimension different than RTree dimension.");
}
AbstractNode root = file.readNode(0);
Leaf l = root.chooseLeaf(h);
return l.insert(h, page);
}
/**
* Deletes a HyperCube from the leaf level of the tree. If there is no leaf containg a hypercube
* that matches the given hypercube, the tree is left intact.
*
* @param h The HyperCube to delete.
* @return The data pointer of the deleted entry, NIL if no matching entry was found.
*/
public int delete(HyperCube h) {
if (h == null) {
throw new IllegalArgumentException("HyperCube cannot be null.");
}
if (h.getDimension() != file.dimension) {
throw new IllegalArgumentException("HyperCube dimension different than RTree dimension.");
}
AbstractNode root = file.readNode(0);
Leaf l = root.findLeaf(h);
if (l != null) {
return l.delete(h);
}
return NIL;
}
/**
* Returns a Vector containing all tree nodes from bottom to top, left to right.
* CAUTION: If the tree is not memory resident, all nodes will be loaded into main memory.
*
* @param root The node from which the traverse should begin.
* @return A Vector containing all Nodes in the correct order.
*/
public Vector traverseByLevel(AbstractNode root) {
if (root == null) {
throw new IllegalArgumentException("Node cannot be null.");
}
Vector ret = new Vector();
Vector v = traversePostOrder(root);
for (int i = 0; i <= getTreeLevel(); i++) {
Vector a = new Vector();
for (int j = 0; j < v.size(); j++) {
Node n = (Node) v.elementAt(j);
if (n.getLevel() == i) {
a.addElement(n);
}
}
for (int j = 0; j < a.size(); j++) {
ret.addElement(a.elementAt(j));
}
}
return ret;
}
/**
* Returns an Enumeration containing all tree nodes from bottom to top, left to right.
*
* @return An Enumeration containing all Nodes in the correct order.
*/
public Enumeration traverseByLevel() {
class ByLevelEnum implements Enumeration {
// there is at least one node, the root node.
private boolean hasNext = true;
private Vector nodes;
private int index = 0;
public ByLevelEnum() {
AbstractNode root = file.readNode(0);
nodes = traverseByLevel(root);
}
public boolean hasMoreElements() {
return hasNext;
}
public Object nextElement() {
if (! hasNext) {
throw new NoSuchElementException("traverseByLevel");
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
一个java实现的R树,R树在存储空间数据方面用处很大,特别是区域性的按空间划分的数据.这种结构用在数据库处理多维数据极为方便,oracle索引多维数据就是用的这种.zip (57个子文件)
Sort.java 2KB
Comparator.java 183B
Leaf.java 5KB
doc.bat 98B
AbstractNode.java 13KB
HyperCube.java 8KB
PageFile.java 3KB
Data.java 399B
PersistentPageFile.java 8KB
MemoryPageFile.java 2KB
Point.java 2KB
RTree.java 21KB
CachedPersistentPageFile.java 4KB
PageFaultError.java 291B
Index.java 7KB
doc
index-all.html 35KB
rtree
PageFile.html 15KB
Data.html 9KB
class-use
PageFile.html 7KB
Data.html 4KB
Node.html 5KB
MemoryPageFile.html 4KB
HyperCube.html 19KB
PersistentPageFile.html 5KB
Comparator.html 5KB
Leaf.html 7KB
Sort.html 4KB
PageFaultError.html 9KB
RTree.html 8KB
AbstractNode.html 17KB
Point.html 9KB
CachedPersistentPageFile.html 4KB
Index.html 4KB
Node.html 10KB
MemoryPageFile.html 13KB
HyperCube.html 18KB
PersistentPageFile.html 16KB
Comparator.html 7KB
Leaf.html 16KB
Sort.html 7KB
PageFaultError.html 8KB
RTree.html 36KB
AbstractNode.html 27KB
Point.html 11KB
CachedPersistentPageFile.html 13KB
Index.html 16KB
stylesheet.css 1KB
allclasses-frame.html 2KB
packages.html 684B
overview-tree.html 5KB
serialized-form.html 4KB
package-list 0B
help-doc.html 7KB
deprecated-list.html 3KB
index.html 660B
Node.java 708B
www.pudn.com.txt 218B
共 57 条
- 1
航向正北
- 粉丝: 3
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页