import java.util.Iterator; public class TreeTable, V> implements SimpleTable, Iterable { private Node head = null; public int nodecount = 0; public long getitcount = 0; public void put(K key, V value) { Node b = new Node(key, value); Node n=head; if (head == null) { head=b; return; } nodecount++; for (;;) { if (key.equals(n.key)) { n.value = value; return; } if (key.compareTo(n.key)<0) { if (n.left == null) { n.left = b; return; } n = n.left; } else { if (n.right == null) { n.right = b; return; } n = n.right; } } } public V get(K key) { if (head == null) return null; else return head.get(key); // for (Node n = head; n!=null;) { // getitcount++; // if (key.equals(n.key)) // return n.value; // if (key.compareTo(n.key)<0) // n = n.left; // else // n = n.right; // } // return null; } public String toString() { if (head == null) return ""; else return head.toString(); } public Iterator iterator() { // return new TTI(head.toList(null)); // return new TTI2(head); return new TTI3(head); } private class List { private K key; private List next; } private class Node, V> { private K key; private V value; private Node left, right; Node(K k, V v) { key = k; value = v; left = null; right = null; } private V get(K k) { getitcount++; if (k.equals(key)) return value; if (k.compareTo(key)<0) { if (left == null) return null; return left.get(k); } else { if (right == null) return null; return right.get(k); } } public void print() { if (left != null) left.print(); System.out.println(key+": "+value+"\n"); if (right != null) right.print(); } public void gen(TTI3 it) { if (left != null) left.gen(it); synchronized (it) { it.worker.current = key; it.notify(); try { it.wait(); } catch(InterruptedException Ex) { }; } if (right != null) right.gen(it); } public String toString() { String l = (left == null) ? "" : left.toString(); String r = (right == null) ? "" : right.toString(); return l+key+": "+value+"\n"+r; } public List toList(List rest) { List r= right!=null ? right.toList(rest) : rest; List r1 = new List(); r1.next = r; r1.key = key; return left!=null ? left.toList(r1) : r1; } } private class NodeList,V> { private Node node; private NodeList next; NodeList(Node n, NodeListl) { node = n; next = l; } } private class TTI2> implements Iterator { private NodeList stack = null; TTI2(Node n) { stack = n==null ? null : new NodeList(n,null); walkleft(); } private void walkleft() { while (stack != null && stack.node.left != null) { stack = new NodeList(stack.node.left,stack); } } public boolean hasNext() { return stack != null; } public K next() { if (stack == null) return null; K res = stack.node.key; if (stack.node.right != null) { stack.node = stack.node.right; walkleft(); } else { stack = stack.next; } return res; } public void remove() { throw new UnsupportedOperationException(); } } private class TTI implements Iterator { private List c; TTI(List head) { c=head; } public boolean hasNext() { return c!=null; } public K next() { K k=c.key; c = c.next; return k; } public void remove() { throw new UnsupportedOperationException(); } } private class TTI3> implements Iterator { TTI3 it; Worker worker; class Worker implements Runnable { Node head; boolean done; K current; Worker(Node n, TTI3 i) { head = n; done = head==null; it = i; } public void run() { if (head != null) head.gen(it); done = true; } } TTI3(Node n) { worker=new Worker(n,this); new Thread(worker).start(); } public boolean hasNext() { return !worker.done; } public synchronized K next() { try { wait(); } catch(InterruptedException Ex) { }; K r = worker.current; notify(); return r; } public void remove() { throw new UnsupportedOperationException(); } } }