Advertisement
wolfsinner

AEDEX2

Jan 15th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. public Iterator<Boolean> maxPath(){
  2.     List<Boolean> lst = this.postorder(root);
  3.         return lst.iterator();
  4. }
  5.    
  6. private List<Boolean> postorder(BSTNode<K, V> node){
  7.     if(node == null) return null;
  8.         else {
  9.             List<Boolean> ret;
  10.             List<Boolean> left = (node.getLeft() != null) ? postorder(node.getLeft()) : null;
  11.             List<Boolean> right = (node.getRight() != null) ? postorder(node.getRight()) : null;
  12.             if(node.isLeaf()) return new DoublyLinkedList<Boolean>();
  13.             if((right != null && left == null) || (right != null && left != null && right.size() > left.size())){
  14.                 ret = right;
  15.                 ret.addFirst(true);
  16.             } else if((left != null && right == null) || (left != null && right != null && left.size() > right.size())){
  17.                 ret = left;
  18.                 ret.addFirst(false);
  19.             } else {
  20.                 ret = right;
  21.                 ret.addFirst(true);
  22.             }
  23.             return ret;
  24.         }
  25. }
  26.  
  27.  
  28.  
  29. public Entry<K,V> entryAt(Iterator<Boolean> path){
  30.         BSTNode<K,V> node = root;
  31.         while(path.hasNext()){
  32.             Boolean next = path.next();
  33.             if((next && node.getRight() == null) || (!next && node.getLeft() == null)) throw new NoSuchElementException();
  34.             if(next) node = node.getRight();
  35.             else node = node.getLeft();
  36.         }
  37.        
  38.         return node.getEntry();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement