Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Optional;
- public class InOrderSuccessorBinaryTree<K,V> extends SimpleBinaryTree<K,V> {
- public Optional<K> inOrderSuccessorKey(K key) {
- Optional <BinaryTreeNode <K, V>> node = Optional.ofNullable(root);
- Optional <K> successor = Optional.empty();
- while(node.isPresent() && !node.get().getKey().equals(key)) {
- if(((Comparable) node.get().getKey()).compareTo(key) > 0) {
- successor = node.map(BinaryTreeNode::getKey);
- node = node.flatMap(BinaryTreeNode::getLeft);
- } //end of if
- else
- node = node.flatMap(BinaryTreeNode::getRight);
- } //end of while
- return node.flatMap(BinaryTreeNode::getRight).map(this::minKey).map(Optional::of).orElse(successor);
- } //end of method inOrderSuccessorBinaryTreeKey
- } //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement