Advertisement
Guest User

Untitled

a guest
Oct 4th, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. import java.util.Optional;
  2.  
  3. public class InOrderSuccessorBinaryTree<K,V> extends SimpleBinaryTree<K,V> {
  4.     public Optional<K> inOrderSuccessorKey(K key) {
  5.         Optional <BinaryTreeNode <K, V>> node = Optional.ofNullable(root);
  6.         Optional <K> successor = Optional.empty();
  7.         while(node.isPresent() && !node.get().getKey().equals(key)) {
  8.             if(((Comparable) node.get().getKey()).compareTo(key) > 0) {
  9.                 successor = node.map(BinaryTreeNode::getKey);
  10.                 node = node.flatMap(BinaryTreeNode::getLeft);
  11.             } //end of if
  12.             else
  13.                 node = node.flatMap(BinaryTreeNode::getRight);
  14.         } //end of while
  15.         return node.flatMap(BinaryTreeNode::getRight).map(this::minKey).map(Optional::of).orElse(successor);
  16.     } //end of method inOrderSuccessorBinaryTreeKey
  17. } //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement