QuestionQuestion

This assignment is an extension of Part 2 of Homework 3. You need to extend IntLinkedBinaryTree, which you wrote for Homework 3, in the following way:
(1). Implement a delete method whose specification is given below:
Signature: public Integer delete(Position<Integer> p, Integer e)
Input:
p: the root of the tree from which a node is deleted
e: the integer key of the node to be deleted
Output: Returns the deleted key, if e exists. If e does not exist, returns null.
How to delete a node from a binary search tree is described in pages 464 and 465 of the textbook.
(2). Make your program a menu driven program. When your program starts, it must display the following main menu:
Choose an option:
1. Add a key
2. Remove a key
3. Print the tree
4. Exit
If the user chooses option 1, your program performs the followings:
Prompt the user to enter an integer.
If the integer does not exist in the tree, add the integer and display an appropriate message, such as “the key added successfully,” and display the main menu.
If the integer already exists, display an appropriate message, such as “the key already exists,” and display the main menu.
If the user chooses option 2, your program performs the followings:
Prompt the user to enter an integer.
- If the integer does not exist in the tree, display an appropriate message, such as “the key does not exist,” and display the main menu.
- If the integer exists, display an appropriate message, such as “the key deleted successfully,” and display the main menu.
- If the user chooses option 3, display all keys in the tree in increasing order, and display the main menu.
- If the user chooses option 4, terminate a program.
Documentation
No separate documentation is needed. However, you must include the specification of each method in your program, right above each method, and you must include sufficient inline comments within your program.
At the beginning of the source code, write your name as comments.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public Position<Integer> add(Position<Integer> p, Integer e){
    Position<Integer> current = p;
    if (current == null) {
      return addRoot(e);
    }
    while (true) {
      if (e < current.element()) {
       try {
          current = left(current);
       } catch (BoundaryViolationException ex) {
          insertLeft(current, e);
       }
      } else if (e > current.element()) {
       try {
          current = right(current);
       } catch (BoundaryViolationException ex) {
          insertRight(current, e);
       }
      } else {
       return null;
      }
    }
}

// Method to delete node from tree
public Integer delete(Position<Integer> p, Integer e){
    Position<Integer> current = p;
    if (current == null) {
      return null;
    }
    while (true) {
      if (e < current.element()) {
       try {
          current = left(current);
       } catch (BoundaryViolationException ex) {
          return null;
       }
      } else if (e > current.element()) {
       try {
          current = right(current);
       } catch (BoundaryViolationException ex) {
          return null;
       }
      } else {
       return remove(current);
      }
    }
}

// Method to get String representation of tree
public String toString(){
    return toString...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$54.00 $27.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Data Structures and Algorithms Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats