CSE116 / CSE504 Introduction to Computer Science II Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM – 2:00 PM Friday 8:30 AM – 10:30 AM OR request appointment via e-mail PROFESSIONALISM Turn off and put away electronics: cell phones pagers laptops tablets etc. © Dr. Carl Alphonce ROADMAP Last class java.util.LinkedList exercise: add Today java.util.LinkedList exercise: remove Wednesday asymptotic notation Friday exam Q&A session (bring questions!) Exam #2 EXAM INFORMATION Tuesday 4/12, 9:15 PM - 10:15 PM Will cover material up to 4/1 If you have a legitimate conflict (work/school): get documentation, scan it as a PDF e-mail it to me with subject line: [CSE116] Exam 2 conflict documentation NO LATER THAN 5:00 PM on 4/8 (FRIDAY) A set of sample questions will be posted soon. WEDNESDAY 4/13 IS EXAM MAKE-UP DAY: NO REGULAR LECTURE © Dr. Carl Alphonce BST OPERATIONS © Dr. Carl Alphonce insert(item) insert into a BST non-empty case if ( item < value at root ) { recursively insert on left } else if (item > value at root) { recursively insert on right } else { // do nothing: duplicate } empty case insert item as root of tree © Dr. Carl Alphonce membership in a BST contains(item) non-empty case if ( item < value at root ) { recursively search in left } else if (item > value at root) { recursively search in right } else { return true; } empty case return false © Dr. Carl Alphonce remove from a BST remove(item) non-empty case if ( item < value at root ) { recursively remove from left } else if (item > value at root) { recursively remove from right } else { remove item } empty case // do nothing: not in tree © Dr. Carl Alphonce What’s common? Each operation traverses the tree structure, to determine whether a value is already present in the tree, and if so where, and if not where. TRAVERSAL! © Dr. Carl Alphonce remove(item) TRAVERSAL non-empty case if ( item < value at root ) { recursively traverse left } else if (item > value at root) { recursively traverse right } else { return tree } empty case return tree © Dr. Carl Alphonce insert(item) TRAVERSE TO RIGHT SPOT, THEN: insert into a BST non-empty case // do nothing: duplicate empty case insert item as root of tree © Dr. Carl Alphonce membership in a BST contains(item) TRAVERSE TO RIGHT SPOT, THEN: non-empty case return true; empty case return false © Dr. Carl Alphonce remove from a BST remove(item) TRAVERSE TO RIGHT SPOT, THEN: non-empty case remove item empty case // do nothing: not in tree © Dr. Carl Alphonce Comparable<E> public interface Comparable<E> { /* @param o the object to be compared. * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. * * @throws NullPointerException if the specified object is null * @throws ClassCastException if the specified object's type prevents it * from being compared to this object. */ public int compareTo(E o); } x.compareTo(y) < 0 if x comes before y x.compareTo(y) == 0 if x and y are at the same place in the order x.compareTo(y) > 0 if x comes after y © Dr. Carl Alphonce TRAVERSAL public class FindVisitor<E extends Comparable<E>> implements IAlgo<BRStruct<E>,E,E> { @Override public BRStruct<E> emptyCase(BRStruct<E> host, E arg) { return host; } @Override public BRStruct<E> nonEmptyCase(BRStruct<E> host, E arg) { if (arg.compareTo(host.getDatum()) < 0) { return host.getLeft().execute(this, arg); } else if (arg.compareTo(host.getDatum()) > 0) { return host.getRight().execute(this, arg); } else { return host; } } } © Dr. Carl Alphonce BST public class BSTBRStruct<E extends Comparable<E>> { private BRStruct<E> _brs; public BSTBRStruct() { _brs = new BRStruct<E>(); } private final FindVisitor<E> FIND = new FindVisitor<E>(); public BSTBRStruct<E> insert(E item) { BRStruct<E> tree = _brs.execute(FIND, item); tree.execute(new IAlgo<Void,E,E>(){ @Override public Void emptyCase(BRStruct<E> host, E item) { host.insertRoot(item); // item is not in tree, so insert it return null; } @Override public Void nonEmptyCase(BRStruct<E> host, E item) { return null; // item is already in tree - do not insert again } }, item); return this; } public BSTBRStruct<E> remove(E item) { © Dr. Carl Alphonce BST private final RemoveRootVisitor<E> REMOVE = new RemoveRootVisitor<E>(); public BSTBRStruct<E> remove(E item) { BRStruct<E> tree = _brs.execute(FIND, item); tree.execute(REMOVE, item); return this; } public boolean member(E item) { BRStruct<E> tree = _brs.execute(FIND, item); return tree.execute(new IAlgo<Boolean,E,Void>(){ @Override public Boolean emptyCase(BRStruct<E> host, Void arg) { return false; } @Override public Boolean nonEmptyCase(BRStruct<E> host, Void arg) { return true; } }, null); } // REST OF CODE OMITTED } © Dr. Carl Alphonce