Menu

Scenario Need Write Method Given Key Argument Returns Next Order Key Found Binary Search T Q43800326

Scenario

We need to write a method that, given a key as an argument,returns the next in order key found in the binary search tree. Ifthe key given as an argument is not found, the method should stillreturn the next in order key. If the binary tree is empty or allthe stored keys are smaller than the argument, then the returnvalue should be empty.

For example, using a collection of {10,13,52,67,68,83} stored inthe binary search tree:

An input of 13 results in 52

An input of 67 results in 68

An input of 55 results in 67

An input of 5 results in 10

An input of 83 results in Optional.empty

An input of 100 results in Optional.empty

Any input on an empty binary tree results in Optional.empty

Both the in order successor and predecessor algorithms have manyapplications. As an example, think about if you had to keep ascoreboard at some sports event where you only want to show thefirst three runners. If you keep your data in a binary search tree,you can find the maximum key and then work out the next twopredecessor nodes.

The solution needs to have a runtime complexity of O(log n).

Aim

Retrieve the successor of an element when the tree is traversedin inorder.

Prerequisites

Implement the following method, provided in theInOrderSuccessorBinaryTree class that extends the SimpleBinaryTreeclass.

public Optional<K> inOrderSuccessorKey(K key)

Steps for Completion

Use a non-recursive search operation first to find the firstnode with a key equal to or less than the input.

Realize that the inorder successor can be in only one of twoplaces, either as a parent of this node or the minimum key on thesubtree of the right child of this node (if any).

Grading

Complete each task listed below. Each task contains automatedchecks which are used to calculate your grade. When you havecompleted each task by clicking the checkbox, open the task listpanel on the left navigation bar and click the “Submit” button.

Task

Use a non-recursive search operation to retrieve the successorelement when traversing a tree in order.

Code Given:

import java.util.Optional;

public class InOrderSuccessorBinaryTree<K,V> extendsSimpleBinaryTree<K,V> {
public Optional<K> inOrderSuccessorKey(K key) {
return null;
}
}

Expert Answer


Answer to Scenario We need to write a method that, given a key as an argument, returns the next in order key found in the binary s…

OR