From 3fb90d4c3afcd06f86155f026302945702d3b081 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 27 Sep 2007 16:23:05 -0600 Subject: [PATCH] Added minimalist TreeSet implementation, as well as Test classes for trees and lists. --- classpath/java/util/TreeSet.java | 81 ++++++++++++++++++++++++++++++++ test/List.java | 43 +++++++++++++++++ test/Tree.java | 40 ++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 classpath/java/util/TreeSet.java create mode 100644 test/List.java create mode 100644 test/Tree.java diff --git a/classpath/java/util/TreeSet.java b/classpath/java/util/TreeSet.java new file mode 100644 index 0000000000..69d1b4a715 --- /dev/null +++ b/classpath/java/util/TreeSet.java @@ -0,0 +1,81 @@ +package java.util; + +public class TreeSet implements Iterable { + private final Comparator comparator; + private int size; + private Cell root; + + public TreeSet(Comparator comparator) { + this.comparator = comparator; + size=0; + root=null; + } + + public Iterator iterator() { + return walk().iterator(); + } + + private ArrayList walk() { + return walk(root, new ArrayList(size)); + } + + private ArrayList walk(Cell cell, ArrayList list) { + if (cell != null) { + walk(cell.left, list); + list.add(cell.value); + walk(cell.right, list); + } + return list; + } + + public boolean add(T o) { + ++size; + if (root == null) { + root = new Cell(o); + return true; + } else { + Cell newElt = new Cell(o); + Cell cur = root; + do { + int result = comparator.compare(o, cur.value); + if (result == 0) return false; + if (result < 0) { + if (cur.left == null) { + newElt.parent = cur; + cur.left = newElt; + return false; + } else { + cur = cur.left; + } + } else { + if (cur.right == null) { + newElt.parent = cur; + cur.right = newElt; + return false; + } else { + cur = cur.right; + } + } + } while (cur != null); + throw new RuntimeException("Fell off end of TreeSet"); + } + } + + public boolean remove(T o) { + throw new UnsupportedOperationException(); + } + + public int size() { + return size; + } + + private static class Cell { + public final T value; + public Cell parent; + public Cell left; + public Cell right; + public Cell(T val) { + value = val; + } + } +} diff --git a/test/List.java b/test/List.java new file mode 100644 index 0000000000..ee3ffc0204 --- /dev/null +++ b/test/List.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; + +public class List { + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } + + private static String printList(ArrayList list) { + StringBuilder sb = new StringBuilder(); + + for (Integer i : list) { + sb.append(i); + sb.append(", "); + } + sb.setLength(sb.length()-2); + return sb.toString(); + } + + private static void isEqual(String s1, String s2) { + System.out.println(s1); + expect(s1.equals(s2)); + } + + public static void main(String args[]) { + ArrayList l = new ArrayList(); + l.add(1); l.add(2); l.add(3); l.add(4); l.add(5); + isEqual(printList(l), "1, 2, 3, 4, 5"); + l.add(0, 6); + isEqual(printList(l), "6, 1, 2, 3, 4, 5"); + l.add(2, 7); + isEqual(printList(l), "6, 1, 7, 2, 3, 4, 5"); + l.remove(1); + isEqual(printList(l), "6, 7, 2, 3, 4, 5"); + l.add(6, 8); + isEqual(printList(l), "6, 7, 2, 3, 4, 5, 8"); + Integer[] ints = new Integer[15]; + Integer[] z = l.toArray(ints); + expect(z == ints); + for (int i=0; i < z.length; i++) { + System.out.println(z[i]); + } + } +} diff --git a/test/Tree.java b/test/Tree.java new file mode 100644 index 0000000000..780af9832d --- /dev/null +++ b/test/Tree.java @@ -0,0 +1,40 @@ +import java.util.Comparator; +import java.util.TreeSet; + +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 (Integer i : list) { + sb.append(i); + sb.append(", "); + } + sb.setLength(sb.length()-2); + 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); + } + } + + public static void main(String args[]) { + TreeSet l = new TreeSet(new MyCompare()); + l.add(5); l.add(2); l.add(1); l.add(8); l.add(3); + isEqual(printList(l), "1, 2, 3, 5, 8"); + l.add(4); + isEqual(printList(l), "1, 2, 3, 4, 5, 8"); + l.remove(3); + isEqual(printList(l), "1, 2, 4, 5, 8"); + } +}