import java.util.Comparator; import java.util.TreeSet; import java.util.TreeMap; import java.util.ArrayList; import java.util.Collection; import java.util.Map; import java.util.Iterator; public class Tree { private static void expect(boolean v) { if (! v) throw new RuntimeException(); } private static String printList(TreeSet list) { StringBuilder sb = new StringBuilder(); for (Object o : list) { sb.append(o); sb.append(", "); } sb.setLength(sb.length()-2); return sb.toString(); } private static String printMap(TreeMap map) { StringBuilder sb = new StringBuilder(); for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry e = it.next(); sb.append(e.getKey()); sb.append("="); sb.append(e.getValue()); if (it.hasNext()) { sb.append(", "); } } return sb.toString(); } private static void isEqual(String s1, String s2) { System.out.println(s1); expect(s1.equals(s2)); } private static class MyCompare implements Comparator { public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } } private static void ascendingIterator() { TreeSet t = new TreeSet(); t.add(7); t.add(2); t.add(9); t.add(2); Iterator iter = t.iterator(); expect(2 == (int)iter.next()); expect(7 == (int)iter.next()); iter.remove(); expect(9 == (int)iter.next()); expect(!iter.hasNext()); isEqual(printList(t), "2, 9"); } private static void descendingIterator() { TreeSet t = new TreeSet(); t.add(7); t.add(2); t.add(9); t.add(2); Iterator iter = t.descendingIterator(); expect(9 == (int)iter.next()); expect(7 == (int)iter.next()); iter.remove(); expect(2 == (int)iter.next()); expect(!iter.hasNext()); isEqual(printList(t), "2, 9"); } public static void main(String args[]) { ascendingIterator(); descendingIterator(); TreeSet t1 = new TreeSet(new MyCompare()); t1.add(5); t1.add(2); t1.add(1); t1.add(8); t1.add(3); isEqual(printList(t1), "1, 2, 3, 5, 8"); t1.add(4); isEqual(printList(t1), "1, 2, 3, 4, 5, 8"); t1.remove(3); isEqual(printList(t1), "1, 2, 4, 5, 8"); TreeSet t2 = new TreeSet(new Comparator() { public int compare(String s1, String s2) { return s1.compareTo(s2); } }); t2.add("one"); t2.add("two"); t2.add("three"); t2.add("four"); t2.add("five"); isEqual(printList(t2), "five, four, one, three, two"); for (int i=0; i < 1000; i++) { t2.add(Integer.toString(i)); } expect(t2.size() == 1005); for (int i=0; i < 999; i++) { t2.remove(Integer.toString(i)); } expect(t2.size() == 6); t2.add("kappa"); isEqual(printList(t2), "999, five, four, kappa, one, three, two"); TreeMap map = new TreeMap (new Comparator() { public int compare(String s1, String s2) { return s1.compareTo(s2); } }); map.put("q", "Q"); map.put("a", "A"); map.put("b", "B"); map.put("z", "Z"); map.put("c", "C"); map.put("y", "Y"); isEqual(printMap(map), "a=A, b=B, c=C, q=Q, y=Y, z=Z"); Collection list = new ArrayList(); list.add(7); list.add(2); list.add(9); list.add(2); isEqual(printList(new TreeSet(list)), "2, 7, 9"); } }