/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ package tests.persistent.map; import java.util.Iterator; import java.util.Random; import ds.persistent.map.BinaryTree; import ds.util.KV; import ds.util.K; import static ds.util.K.mkK; import static ds.util.V.mkV; import static ds.util.KV.mkPair; import tests.util.Args; public class BinaryTreeRandom { public static void main(String [] args) { int noOfElems = Args.getInt(args, 0, 1023); new Main(noOfElems) .run(); } private static class Main extends tests.persistent.map.util.MainBinaryTree { final Random r; Main(int n1) { super(n1); r = new Random(42); } protected void buildTree() { buildTreeSimple(); buildTreeSmart(); } protected void buildTreeSimple() { startTime("building binary search tree by inserting " + n + " random integers\n" + "(simple version by stepwise insertion)"); for (int i = 0; i < n; ++i) { int k = r.nextInt(); t = t.insert(mkK(k), mkV(i)); } stopTime(); } protected void buildTreeSmart() { startTime("building binary search tree by inserting " + n + " random integers\n" + "(with iterator)"); Iterator elems = new ds.util.Iterator() { int i = 0; public boolean hasNext() { return i < n; } public KV next() { int k = r.nextInt(); ++i; return mkPair(mkK(k), mkV(i)); } }; t = BinaryTree.fromIterator(elems); stopTime(); } protected void removeAll() { startTime("removing all elements by stepwise removing the root element"); for (int i = t.size(); i > 0; --i) { K k = t.findRoot().fst; t = t.remove(k); } stopTime(); } } }