private int countLeaves(Node node) { int count = 0; if (node.children == null) { count = node.result.numClusters(); } else { for (int i=0; i<node.result.numClusters(); i++) { count += countLeaves(node.children[i]); } } return count; }
private void writeNodeASCII(PrintWriter writer, final Node node) throws IOException { //write node type if (node.children == null) writer.write("L\n"); //intermediate else writer.write("I\n"); //leaf //write result data node.result.writeASCII(writer); // node.result.writeASCII(writer, false); writer.flush(); //write children if (node.children != null) { for (int i=0; i<node.result.numClusters(); i++) { writeNodeASCII(writer, node.children[i]); } } }
private Node readNode(HierarchicalFloatKMeansResult hFloatkm, Scanner reader) throws IOException { String line; while ((line = reader.nextLine()).length()==0) {/*do nothing*/} char type = line.charAt(0); //read result data Node node = new Node(); node.result = new FloatCentroidsResult(); node.result.readASCII(reader); if (type == 'I') { node.children = new Node[node.result.numClusters()]; for (int i=0; i<node.result.numClusters(); i++) { node.children[i] = readNode(hFloatkm,reader); } } else { node.children = null; } return node ; }
private void writeNodeB(DataOutput dos, Node node) throws IOException { //write node type char type; if (node.children == null) type='L'; //intermediate else type='I'; //leaf dos.writeByte(type); //write result data node.result.writeBinary(dos); //write children if (node.children != null) { for (int i=0; i<node.result.numClusters(); i++) { writeNodeB(dos, node.children[i]); } } }
private Node readNodeB(HierarchicalFloatKMeansResult hFloatkm, DataInput dis) throws IOException { Node node = new Node(); char type = (char) dis.readByte(); //read result data node.result = new FloatCentroidsResult(); node.result.readBinary(dis); if (type == 'I') { node.children = new Node[node.result.numClusters()]; for (int i=0; i<node.result.numClusters(); i++) { node.children[i] = readNodeB(hFloatkm, dis); } } else { node.children = null; } return node ; }