Node implements Writable { @Override public final void write(DataOutput out) throws IOException { out.writeInt(getType().ordinal()); writeNode(out); }  abstract double classify(Instance instance); abstract long nbNodes(); abstract long maxDepth(); static Node read(DataInput in); @Override final String toString(); @Override final void write(DataOutput out);  }
@Test public void testReadTree() throws Exception { Node node1 = new CategoricalNode(rng.nextInt(), new double[] { rng.nextDouble(), rng.nextDouble() }, new Node[] { new Leaf(rng.nextDouble()), new Leaf(rng.nextDouble()) }); Node node2 = new NumericalNode(rng.nextInt(), rng.nextDouble(), new Leaf(rng.nextDouble()), new Leaf(rng.nextDouble())); Node root = new CategoricalNode(rng.nextInt(), new double[] { rng.nextDouble(), rng.nextDouble(), rng.nextDouble() }, new Node[] { node1, node2, new Leaf(rng.nextDouble()) }); root.write(out); assertEquals(root, readNode()); }
@Test public void testReadLeaf() throws Exception { Node leaf = new Leaf(rng.nextDouble()); leaf.write(out); assertEquals(leaf, readNode()); }
@Test public void testParseNumerical() throws Exception { Node node = new NumericalNode(rng.nextInt(), rng.nextDouble(), new Leaf(rng .nextInt()), new Leaf(rng.nextDouble())); node.write(out); assertEquals(node, readNode()); }
@Test public void testCategoricalNode() throws Exception { Node node = new CategoricalNode(rng.nextInt(), new double[]{rng.nextDouble(), rng.nextDouble(), rng.nextDouble()}, new Node[]{ new Leaf(rng.nextDouble()), new Leaf(rng.nextDouble()), new Leaf(rng.nextDouble())}); node.write(out); assertEquals(node, readNode()); }