// Yet another implementation of the Tree ADT in Java. // In this implementation, emptyTree() returns null. // This eliminates one subclass. Since only once subclass // is left, it is folded into the Tree class, and all of // the dynamic methods are folded into the static methods. public class Tree { public static Tree emptyTree () { return null; } public static Tree newTree (int n, Tree t1, Tree t2) { return new Tree (n, t1, t2); } public static boolean isEmpty (Tree t) { if (t == null) return true; else return false; } public static int label (Tree t) { return t.n; } public static Tree left (Tree t) { return t.t1; } public static Tree right (Tree t) { return t.t2; } public static int height (Tree t) { if (t == null) return 0; else return t.height; } public static int size (Tree t) { if (t == null) return 0; else return t.size; } Tree (int n, Tree t1, Tree t2) { this.n = n; this.t1 = t1; this.t2 = t2; this.height = 1 + Math.max (height (t1), height (t2)); this.size = 1 + size (t1) + size (t2); } private int n; private Tree t1; private Tree t2; private int height; private int size; }