/* * ListPlus.java * * Created on March 28, 2007, 5:52 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package exercise5; /** * * @author Sui Huang */ import java.util.*; public class ListPlus extends List{ /** Creates a new instance of ListPlus */ private ListPlus() { // dummy } public static boolean isEmpty(List k) { return k.same(List.nil()); } public static Element getHead(List k) throws BadIndexException { return k.getMember(0); } public static List getTail(List k) throws BadIndexException{ return List.drop(1,k); } public static int getLength(List k) { int length=0; List L=k; while (true){ try { L = List.drop(1,L); length = length+1; } catch (BadIndexException e){ break; } } return length; } public static int getHeight(List k) { int length = getLength(k); // this is an extramely unbalanced tree if (length==0){ return 0; } else if (length==1){ return 1; } else { return (length-1); } } public static List reverse(List k) { List L=List.nil(); int len = getLength(k); for (int i=0; i