Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Iterator<Boolean> maxPath(){
- List<Boolean> lst = this.postorder(root);
- return lst.iterator();
- }
- private List<Boolean> postorder(BSTNode<K, V> node){
- if(node == null) return null;
- else {
- List<Boolean> ret;
- List<Boolean> left = (node.getLeft() != null) ? postorder(node.getLeft()) : null;
- List<Boolean> right = (node.getRight() != null) ? postorder(node.getRight()) : null;
- if(node.isLeaf()) return new DoublyLinkedList<Boolean>();
- if((right != null && left == null) || (right != null && left != null && right.size() > left.size())){
- ret = right;
- ret.addFirst(true);
- } else if((left != null && right == null) || (left != null && right != null && left.size() > right.size())){
- ret = left;
- ret.addFirst(false);
- } else {
- ret = right;
- ret.addFirst(true);
- }
- return ret;
- }
- }
- public Entry<K,V> entryAt(Iterator<Boolean> path){
- BSTNode<K,V> node = root;
- while(path.hasNext()){
- Boolean next = path.next();
- if((next && node.getRight() == null) || (!next && node.getLeft() == null)) throw new NoSuchElementException();
- if(next) node = node.getRight();
- else node = node.getLeft();
- }
- return node.getEntry();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement