2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
22
package weka.core.neighboursearch.kdtrees;
24
import java.io.Serializable;
27
* A class representing a KDTree node. A node does not explicitly
28
* store the instances that it contains. Instead, it only stores
29
* the start and end index of a portion in a master index array. Each
30
* node is assigned a portion in the master index array that stores
31
* the indices of the instances that the node contains. Every time a
32
* node is split by the KDTree's contruction method, the instances of
33
* its left child are moved to the left and the instances of its
34
* right child are moved to the right, in the portion of the master
35
* index array belonging to the node. The start and end index in each
36
* of its children are then set accordingly within that portion so
37
* that each have their own portion which contains their instances.
38
* P.S.: The master index array is only stored in KDTree class.
40
* @author Ashraf M. Kibriya (amk14[at-the-rate]cs[dot]waikato[dot]ac[dot]nz)
41
* @version $Revision: 1.1 $
43
public class KDTreeNode
44
implements Serializable {
46
/** for serialization. */
47
private static final long serialVersionUID = -3660396067582792648L;
49
/** node number (only for debug). */
50
public int m_NodeNumber;
52
/** left subtree; contains instances with smaller or equal to split value. */
53
public KDTreeNode m_Left = null;
55
/** right subtree; contains instances with larger than split value. */
56
public KDTreeNode m_Right = null;
58
/** value to split on. */
59
public double m_SplitValue;
61
/** attribute to split on. */
62
public int m_SplitDim;
65
* lowest and highest value and width (= high - low) for each
68
public double[][] m_NodeRanges;
71
* The lo and high bounds of the hyper rectangle described by the
74
public double[][] m_NodesRectBounds;
77
* The start index of the portion of the master index array,
78
* which stores the indices of the instances/points the node
81
public int m_Start = 0;
84
* The end index of the portion of the master index array,
85
* which stores indices of the instances/points the node
93
public KDTreeNode() {}
98
* @param nodeNum The node number/id.
99
* @param startidx The start index of node's portion
100
* in master index array.
101
* @param endidx The start index of node's portion
102
* in master index array.
103
* @param nodeRanges The attribute ranges of the
104
* Instances/points contained in this node.
106
public KDTreeNode(int nodeNum, int startidx, int endidx, double[][] nodeRanges) {
107
m_NodeNumber = nodeNum;
108
m_Start = startidx; m_End = endidx;
109
m_NodeRanges = nodeRanges;
114
* @param nodeNum The node number/id.
115
* @param startidx The start index of node's portion
116
* in master index array.
117
* @param endidx The start index of node's portion
118
* in master index array.
119
* @param nodeRanges The attribute ranges of the
120
* Instances/points contained in this node.
121
* @param rectBounds The range of the rectangular
122
* region in the point space that this node
123
* represents (points inside this rectangular
124
* region can have different range).
126
public KDTreeNode(int nodeNum, int startidx, int endidx, double[][] nodeRanges, double[][] rectBounds) {
127
m_NodeNumber = nodeNum;
128
m_Start = startidx; m_End = endidx;
129
m_NodeRanges = nodeRanges;
130
m_NodesRectBounds = rectBounds;
134
* Gets the splitting dimension.
136
* @return splitting dimension
138
public int getSplitDim() {
143
* Gets the splitting value.
145
* @return splitting value
147
public double getSplitValue() {
152
* Checks if node is a leaf.
154
* @return true if it is a leaf
156
public boolean isALeaf() {
157
return (m_Left == null);
161
* Returns the number of Instances
162
* in the rectangular region defined
164
* @return The number of instances in
167
public int numInstances() {
168
return (m_End-m_Start+1);