Added minimalist TreeSet implementation, as well as Test classes for trees

and lists.
This commit is contained in:
Eric Scharff 2007-09-27 16:23:05 -06:00
parent 0efc498837
commit 3fb90d4c3a
3 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,81 @@
package java.util;
public class TreeSet<T> implements Iterable<T> {
private final Comparator<T> comparator;
private int size;
private Cell<T> root;
public TreeSet(Comparator<T> comparator) {
this.comparator = comparator;
size=0;
root=null;
}
public Iterator<T> iterator() {
return walk().iterator();
}
private ArrayList<T> walk() {
return walk(root, new ArrayList<T>(size));
}
private ArrayList<T> walk(Cell<T> cell, ArrayList<T> 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<T>(o);
return true;
} else {
Cell<T> newElt = new Cell<T>(o);
Cell<T> 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<T> {
public final T value;
public Cell<T> parent;
public Cell<T> left;
public Cell<T> right;
public Cell(T val) {
value = val;
}
}
}

43
test/List.java Normal file
View File

@ -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<Integer> 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<Integer> l = new ArrayList<Integer>();
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]);
}
}
}

40
test/Tree.java Normal file
View File

@ -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<Integer> 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<Integer> {
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
}
public static void main(String args[]) {
TreeSet<Integer> l = new TreeSet<Integer>(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");
}
}