Implement the APMap interface with your BST implementation (see below)
Assignment Sheet: Lab21_CharCount_part2.pdf
Create a new class in your CharCount project and copy and paste the APMap interface into it. This is the interface you should implement with your BST.
public interface APMap{
public boolean containsKey(Object key);
public Object get(Object key);
public Object put(Object key, Object val);
public Set keySet();
public int size();
public Object remove(Object key);
}
And here are the three helper methods for the remove. You should go through the exercise of writing the move method yourself.
private TreeNode<Comparable> findLeftMost(TreeNode<Comparable> t){
TreeNode temp = t;
while(temp.getLeft()!=null) temp = temp.getLeft();
return temp;
}
private TreeNode<Comparable> findParent(TreeNode<Comparable> t){
TreeNode p = root;
if(t==root) return null;
int compVal;
while(p.getLeft()!=t && p.getRight()!=t){
compVal = t.getValue().compareTo(p.getValue());
if(compVal < 0) p = p.getLeft();
else p = p.getRight();
}
return p;
}
private void replaceNode(TreeNode a, TreeNode b){
TreeNode p = findParent(a);
if(p==null) root = b;
else if(p.getLeft()==a) p.setLeft(b);
else p.setRight(b);
}
Posted on March 3rd, 2008 by Baker Franke
Filed under: Labs | No Comments »