| 1 | /* |
|---|
| 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. |
|---|
| 6 | * |
|---|
| 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. |
|---|
| 11 | * |
|---|
| 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. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * KDTreeNode.java |
|---|
| 19 | * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | package weka.core.neighboursearch.kdtrees; |
|---|
| 23 | |
|---|
| 24 | import weka.core.RevisionHandler; |
|---|
| 25 | import weka.core.RevisionUtils; |
|---|
| 26 | |
|---|
| 27 | import java.io.Serializable; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * A class representing a KDTree node. A node does not explicitly |
|---|
| 31 | * store the instances that it contains. Instead, it only stores |
|---|
| 32 | * the start and end index of a portion in a master index array. Each |
|---|
| 33 | * node is assigned a portion in the master index array that stores |
|---|
| 34 | * the indices of the instances that the node contains. Every time a |
|---|
| 35 | * node is split by the KDTree's contruction method, the instances of |
|---|
| 36 | * its left child are moved to the left and the instances of its |
|---|
| 37 | * right child are moved to the right, in the portion of the master |
|---|
| 38 | * index array belonging to the node. The start and end index in each |
|---|
| 39 | * of its children are then set accordingly within that portion so |
|---|
| 40 | * that each have their own portion which contains their instances. |
|---|
| 41 | * P.S.: The master index array is only stored in KDTree class. |
|---|
| 42 | * |
|---|
| 43 | * @author Ashraf M. Kibriya (amk14[at-the-rate]cs[dot]waikato[dot]ac[dot]nz) |
|---|
| 44 | * @version $Revision: 5953 $ |
|---|
| 45 | */ |
|---|
| 46 | public class KDTreeNode |
|---|
| 47 | implements Serializable, RevisionHandler { |
|---|
| 48 | |
|---|
| 49 | /** for serialization. */ |
|---|
| 50 | private static final long serialVersionUID = -3660396067582792648L; |
|---|
| 51 | |
|---|
| 52 | /** node number (only for debug). */ |
|---|
| 53 | public int m_NodeNumber; |
|---|
| 54 | |
|---|
| 55 | /** left subtree; contains instances with smaller or equal to split value. */ |
|---|
| 56 | public KDTreeNode m_Left = null; |
|---|
| 57 | |
|---|
| 58 | /** right subtree; contains instances with larger than split value. */ |
|---|
| 59 | public KDTreeNode m_Right = null; |
|---|
| 60 | |
|---|
| 61 | /** value to split on. */ |
|---|
| 62 | public double m_SplitValue; |
|---|
| 63 | |
|---|
| 64 | /** attribute to split on. */ |
|---|
| 65 | public int m_SplitDim; |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * lowest and highest value and width (= high - low) for each |
|---|
| 69 | * dimension. |
|---|
| 70 | */ |
|---|
| 71 | public double[][] m_NodeRanges; |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * The lo and high bounds of the hyper rectangle described by the |
|---|
| 75 | * node. |
|---|
| 76 | */ |
|---|
| 77 | public double[][] m_NodesRectBounds; |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * The start index of the portion of the master index array, |
|---|
| 81 | * which stores the indices of the instances/points the node |
|---|
| 82 | * contains. |
|---|
| 83 | */ |
|---|
| 84 | public int m_Start = 0; |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * The end index of the portion of the master index array, |
|---|
| 88 | * which stores indices of the instances/points the node |
|---|
| 89 | * contains. |
|---|
| 90 | */ |
|---|
| 91 | public int m_End = 0; |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Constructor. |
|---|
| 95 | */ |
|---|
| 96 | public KDTreeNode() {} |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Constructor. |
|---|
| 100 | * |
|---|
| 101 | * @param nodeNum The node number/id. |
|---|
| 102 | * @param startidx The start index of node's portion |
|---|
| 103 | * in master index array. |
|---|
| 104 | * @param endidx The start index of node's portion |
|---|
| 105 | * in master index array. |
|---|
| 106 | * @param nodeRanges The attribute ranges of the |
|---|
| 107 | * Instances/points contained in this node. |
|---|
| 108 | */ |
|---|
| 109 | public KDTreeNode(int nodeNum, int startidx, int endidx, double[][] nodeRanges) { |
|---|
| 110 | m_NodeNumber = nodeNum; |
|---|
| 111 | m_Start = startidx; m_End = endidx; |
|---|
| 112 | m_NodeRanges = nodeRanges; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * |
|---|
| 117 | * @param nodeNum The node number/id. |
|---|
| 118 | * @param startidx The start index of node's portion |
|---|
| 119 | * in master index array. |
|---|
| 120 | * @param endidx The start index of node's portion |
|---|
| 121 | * in master index array. |
|---|
| 122 | * @param nodeRanges The attribute ranges of the |
|---|
| 123 | * Instances/points contained in this node. |
|---|
| 124 | * @param rectBounds The range of the rectangular |
|---|
| 125 | * region in the point space that this node |
|---|
| 126 | * represents (points inside this rectangular |
|---|
| 127 | * region can have different range). |
|---|
| 128 | */ |
|---|
| 129 | public KDTreeNode(int nodeNum, int startidx, int endidx, double[][] nodeRanges, double[][] rectBounds) { |
|---|
| 130 | m_NodeNumber = nodeNum; |
|---|
| 131 | m_Start = startidx; m_End = endidx; |
|---|
| 132 | m_NodeRanges = nodeRanges; |
|---|
| 133 | m_NodesRectBounds = rectBounds; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Gets the splitting dimension. |
|---|
| 138 | * |
|---|
| 139 | * @return splitting dimension |
|---|
| 140 | */ |
|---|
| 141 | public int getSplitDim() { |
|---|
| 142 | return m_SplitDim; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | * Gets the splitting value. |
|---|
| 147 | * |
|---|
| 148 | * @return splitting value |
|---|
| 149 | */ |
|---|
| 150 | public double getSplitValue() { |
|---|
| 151 | return m_SplitValue; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Checks if node is a leaf. |
|---|
| 156 | * |
|---|
| 157 | * @return true if it is a leaf |
|---|
| 158 | */ |
|---|
| 159 | public boolean isALeaf() { |
|---|
| 160 | return (m_Left == null); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * Returns the number of Instances |
|---|
| 165 | * in the rectangular region defined |
|---|
| 166 | * by this node. |
|---|
| 167 | * @return The number of instances in |
|---|
| 168 | * this KDTreeNode. |
|---|
| 169 | */ |
|---|
| 170 | public int numInstances() { |
|---|
| 171 | return (m_End-m_Start+1); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * Returns the revision string. |
|---|
| 176 | * |
|---|
| 177 | * @return the revision |
|---|
| 178 | */ |
|---|
| 179 | public String getRevision() { |
|---|
| 180 | return RevisionUtils.extract("$Revision: 5953 $"); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|