tree height parent level balanced
tree height parent level balanced
balanced leaf level parent tree
public void print() { LLNode<T> curr = front; while (curr != null) { System.out.println(curr.getInfo()); curr = curr.getLink(); }
public void addFront(T element) { LLNode<T> newnode = new LLNode<T>(element); newnode.setLink(front); front = newnode; // if this is the only node, make rear point to it if (read == null) rear = newnode; numElements++; }
public void add(T element) { // Adds element to this list. int i = 0; int k; boolean locfound = false; // loop till we find a bigger element or reach the end while (i < numElements && !locfound) { if (list[i].equals(element)) throw new DuplicateElementException(); // if list[i] is greater then add element before it if (comp.compare(list[i],element) > 0) locfound = true; else i++; } // if a bigger element was not found, add at the end if (!locfound) list[numElements] = element; // otherwise move later elements to make room for new one // i is location where the new element will be placed else { for (k = numElements ; k > i ; k--) list[k] = list[k-1]; list[i] = element; } numElements++; }
public T smallest() { int i; if (numElements == 0) return null; T small = elements[0]; for (i = 1 ; i < numElements ; i++) if (elements[i].compareTo(small) < 0) small = elements[i]; return small; }
public T smallest() { LLNode<T> curr; if (numElements == 0) return null; T small = front.getInfo(); for (curr = front ; curr != null ; curr = curr.getLink()) if (curr.getInfo().compareTo(small) < 0) small = curr.getInfo(); return small; }
public static ClassA smallest(ListInterface<ClassA> mylist) { int i; ClassA small, curr; if (mylist.size() == 0) return null; small = mylist.get(0); for (i = 1 ; i < numElements ; i++) { curr = mylist.get(i); if (curr.compareTo(small) < 0) small = curr; } return small; }
public static ClassA smallest(ListInterface<ClassA> mylist) { int i; ClassA small, curr; Comparator<ClassA> mycomp = ClassA.comp(); if (mylist.size() == 0) return null; Iterator<ClassA> iter = mylist.iterator(); small = iter.next(); while (iter.hasNext() { curr = mylist.next(); if (mycomp.compare(curr, small) < 0) small = curr; } return small; }
public void removeAll(T target) { int curr = 0; while (curr < numElements-1) { if (elements[curr].equals(target)) { elements[curr] = elements[numElements-1]; numElements--; } else curr++; } }
public static void removeAll(ArrayCollection<String> collect, String target) { String str = collect.get(target); while (str != null) { collect.remove(str); str = collect.get(target); } }
public class Visit implements Comparable<T> { private String name; private String continent; int year; int month; public Visit(String nn, String cc, int yy, int mm) { name = nn; continent = cc; year = yy; month = mm; } public String toString() { return "Country: " + name + "\nContinent: " + continent + "\nDate: " + month + "/" + year + "\n"; } public int compareTo(Visit visit) { return name.compareTo(visit.name); } public boolean equals(Object ob) { if (ob == null || !(ob instanceof Visit)) return false; Visit visit = (Visit) ob; if (continent.equals(visit.continent) && name.equals(visit.name)) return true; return false; } }
Visit one = new Visit("Venezuela", "South America", 12, 2012); Visit two = new Visit("Spain", "Europe", 5, 2007); Visit three = new Visit("Italy", "Europe", 6, 2010); Visit four = new Visit("China", "Asia", 6, 2005); SortedABList<Visit> trips = new SortedABList<Visit>(); trips.add(one); trips.add(two); trips.add(three); trips.add(four); for (Visit trip : trips) System.out.println(trip);
public static Comparator<Visit> dateCountryComp() { return new Comparator<Visit> () { public int compare(Visit v1, Visit v2) { if (v1.year != v2.year) return v1.year - v2.year; if (v1.month != v2.month) return v1.month - v2.month; return v1.name.compareTo(v2.name); } }; }This method is part of the Visit class.
SortedABList<Visit> trips = new SortedABList<Visit>(Visit.dateCountryComp()); trips.add(new Visit("Venezuela", "South America", 12, 2012)); trips.add(new Visit("Spain", "Europe", 5, 2007)); trips.add(new Visit("Italy", "Europe", 6, 2010)); trips.add(new Visit("China", "Asia", 6, 2005)); for (Visit trip : trips) System.out.println(trip);
public static Comparator<Visit> contCountryDateComp() { return new Comparator<Visit> () { public int compare(Visit v1, Visit v2) { int comp = v1.continent.compareTo(v2.continent); if (comp != 0) return comp; comp = v1.name.compareTo(v2.name); if (comp != 0) return comp; if (v1.year != v2.year) return v1.year - v2.year; return v1.month - v2.month; } }; }This method is part of the Visit class.
SortedABList<Visit> trips = new SortedABList<Visit>(Visit.dateCountryComp()); trips.add(new Visit("Venezuela", "South America", 12, 2012)); trips.add(new Visit("Spain", "Europe", 5, 2007)); trips.add(new Visit("Italy", "Europe", 6, 2010)); trips.add(new Visit("China", "Asia", 6, 2005)); Visit visit; tripIter = trips.iterator(); while (tripIter.hasNext()) { visit = tripIter.next(); System.out.println(visit); }
public Iterator<T> iterator() { return new ArrayCollIter(); } class ArrayCollIter implements Iterator { private int previousPos = -1; // last position we have returned in the iteration // create the iterator public ArrayCollIter() { } public boolean hasNext() { // Returns true if the iteration has more elements; otherwise returns false. // if previousPos is at the end of the list, we are done return (previousPos < (numElements - 1)) ; } public T next() { // Returns the next element in the iteration. // Throws NoSuchElementException - if the iteration has no more elements if (!hasNext()) throw new IndexOutOfBoundsException("Illegal invocation of next " + " in ArrayCollection iterator.\n"); previousPos++; return elements[previousPos]; } } // end of ArrayCollIter class
ArrayCollection<Visit> trips = new ArrayCollection<Visit>(); trips.add(new Visit("Venezuela", "South America", 12, 2012)); trips.add(new Visit("Spain", "Europe", 5, 2007)); trips.add(new Visit("Italy", "Europe", 6, 2010)); trips.add(new Visit("China", "Asia", 6, 2005)); Visit visit; tripIter = trips.iterator(); while (tripIter.hasNext()) { visit = tripIter.next(); System.out.println(visit); }
ArrayCollection<Visit> trips = new ArrayCollection<Visit>(); trips.add(new Visit("Venezuela", "South America", 12, 2012)); trips.add(new Visit("Spain", "Europe", 5, 2007)); trips.add(new Visit("Italy", "Europe", 6, 2010)); trips.add(new Visit("China", "Asia", 6, 2005)); for (Visit visit : trips)) { System.out.println(visit); }
public Iterator<T> iterator() { // the return statement creates a new iterator and the definition // of the Iterator class starts with the { return new Iterator<T>() { private int previousPos = -1; public boolean hasNext() { // Returns true if the iteration has more elements; otherwise returns false. return (previousPos < (size() - 1)) ; } public T next() { // Returns the next element in the iteration. // Throws NoSuchElementException - if the iteration has no more elements if (!hasNext()) throw new IndexOutOfBoundsException("Illegal invocation of next " + " in ArrayCollection iterator.\n"); previousPos++; return elements[previousPos]; } }; // end of return statement } // end of iterator() method
public Iterator<T> iterator() { return new LinkedIterator(); } // inner class for iterator private class LinkedIterator implements Iterator { private LLNode<T> loc = head; public boolean hasNext() { return loc != null; } public T next() { if (loc == null) throw new IndexOutOfBoundsException("invalid next in LinkedIterator"); T nextval = loc.getInfo(); loc = loc.getLink(); return nextval; } } // end of LinkedIterator
LinkedCollection<Visit> trips = new LinkedCollection<Visit>(); trips.add(new Visit("Venezuela", "South America", 12, 2012)); trips.add(new Visit("Spain", "Europe", 5, 2007)); trips.add(new Visit("Italy", "Europe", 6, 2010)); trips.add(new Visit("China", "Asia", 6, 2005)); Visit visit; tripIter = trips.iterator(); while (tripIter.hasNext()) { visit = tripIter.next(); System.out.println(visit); }
LinkedCollection<Visit> trips = new LinkedCollection<Visit>(); trips.add(new Visit("Venezuela", "South America", 12, 2012)); trips.add(new Visit("Spain", "Europe", 5, 2007)); trips.add(new Visit("Italy", "Europe", 6, 2010)); trips.add(new Visit("China", "Asia", 6, 2005)); for (Visit visit : trips)) { System.out.println(visit); }
// anonymous inner class for iterator public Iteratoriterator() { return new Iterator () { private LLNode loc = head; public boolean hasNext() { return loc != null; } public T next() { if (loc == null) throw new IndexOutOfBoundsException("invalid next in Collection iterator"); T nextval = loc.getInfo(); loc = loc.getLink(); return nextval; } }; // end of inner class & return stmt } // end of iterator method
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2020