Question

In: Computer Science

Please show that the code is working at the end(screen shot of the final result +...

Please show that the code is working at the end(screen shot of the final result + classes and interfaces) and use interfaces.

Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to:

  • Build a binary search tree using these values in the order they are entered.

  • Print 3 traversals: pre-, in-, and post-order.

  • Allow the user to insert/delete a value. Once a new tree is generated, print it in-order.

  • Find predecessor of a given value. The predecessor is the node that appears right before

    the given value in an in-order traversal.

  • Find successor of a given value. The successor is the node that appears right after the given

    value in an in-order traversal.

    In your BST implementation, the add and delete methods must be implemented using recursion. You will lose major points for using a non-recursive implementation.

    Note that no duplicates are allowed in this BST. Your program should use an interactive interface with the format shown below (the user inputs are underlined):

    % java Project3
    Please enter the initial sequence of values:
    51 29 68 90 36 40 22 59 44 99 77 60 27 83 15 75 3 Pre-order: X X X ... X
    In-order: X X X ... X
    Post-order: X X X ... X
    Command? H

Solutions

Expert Solution

Driver.java:


import java.util.Scanner;
import java.util.Arrays;
public class Driver{
public static void main(String args[]) {
System.out.println("Please enter the initial sequence of values:");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String[] input_arr = input.split(" ");
// convert user's input of ints to an int array
int[] input_sequence = new int[input_arr.length];
for (int i = 0; i < input_arr.length; i++) {
try {
input_sequence[i] = Integer.parseInt(input_arr[i]);
} catch (NumberFormatException nfe) {
System.out.println(nfe);
}
}
// create a BinarySearchTree using the int array
BinarySearchTree tree = new BinarySearchTree();
for (int i = 0; i < input_sequence.length; i++) {
tree.insert(input_sequence[i], tree.root, null);
}
System.out.println();
System.out.println("Pre-order: " + tree.preOrder(tree.root, ""));
System.out.println("In-order: " + tree.inOrder(tree.root, ""));
System.out.println("Post-order: " + tree.postOrder(tree.root, ""));
System.out.println();
commandList();
System.out.println();
// while loop to ask for commands
while (input.charAt(0) != 'E') {
System.out.print("> ");
input = sc.nextLine().toUpperCase();
System.out.println();
input_arr = input.split(" ");
// used a switch-case because it seems intuitive for a given set of
// commands compared to a long if-else statement
switch(input.charAt(0)) {
case 'I' :
int insert = Integer.parseInt(input_arr[1]);
if (!tree.contains(insert, tree.root)) {
tree.insert(insert, tree.root, null);
System.out.println("In-order: " + tree.inOrder(tree.root, ""));
} else {
System.out.println(insert + " already exists, ignore.");
}
break;
case 'D' :
int delete = Integer.parseInt(input_arr[1]);
if (tree.contains(delete, tree.root)) {
tree.delete(delete, tree.root);
System.out.println("In-order: " + tree.inOrder(tree.root, ""));
} else {
System.out.println(delete + " does not exist!");
}
break;
case 'P' :
int predecessor = Integer.parseInt(input_arr[1]);
if (tree.predecessor(predecessor, tree.root) != null) {
System.out.println(tree.predecessor(predecessor, tree.root).getData());
} else {
System.out.println(predecessor + " does not have a predecessor.");
}
break;
case 'S' :
int successor = Integer.parseInt(input_arr[1]);
if (tree.successor(successor, tree.root) != null) {
System.out.println(tree.successor(successor, tree.root).getData());
} else {
System.out.println(successor + " does not have a successor.");
}
break;
case 'H' :
commandList();
break;
case 'E' :
break;
default :
System.out.println("That is an unrecognizable command :/");
}
System.out.println();
}
System.out.println("Thanks for using my program :)");
}
// separated the list of commands
public static void commandList() {
System.out.println("I Insert a value");
System.out.println("D Delete a value");
System.out.println("P Find predecessor");
System.out.println("S Find successor");
System.out.println("H Display this message");
System.out.println("E Exit the program");
}
}

BinarySearchTree.java:

//BinarySearchTree.java
public class BinarySearchTree {
protected Node root;
public BinarySearchTree() {
root = null;
}
/**
* The contains method will check if a given value is within a BinarySearchTree. This is
* used to prevent inserting duplicate values and deleting non-existent ones
*/
public boolean contains(int target, Node n) {
if (n == null) return false;
if (n.getData() == target) {
return true;
} else if (n.getData() > target) {
return contains(target, n.getLeft());
} else {
return contains(target, n.getRight());
}
}
/**
* This is my implementation of the predecessor. Although I didn't have it, I
* implemented recursion into this algorithm because it's good practice.
*/
public Node predecessor(int target, Node n) {
if (root == null) return null;
if (n == root.getLeftmost()) return null;
if (n.getData() == target) {
if (n.getLeft() != null) {
return n.getLeft().getRightmost();
} else {
Node temp = n.parent;
while (temp != null && n == temp.getLeft()) {
n = temp;
temp = temp.parent;
}
if (temp == null) {
return null;
} else {
return temp;
}
}
} else if (n.getData() > target) {
return predecessor(target, n.getLeft());
} else {
return predecessor(target, n.getRight());
}
}
/**
* This is my implementation of the successor. It is kind of like a mirrored
* version of the predecessor method.
*/
public Node successor(int target, Node n) {
if (root == null) return null;
if (n == root.getRightmost()) return null;
if (n.getData() == target) {
if (n.getRight() != null) {
return n.getRight().getLeftmost();
} else {
Node temp = n.parent;
while (temp != null && n == temp.getRight()) {
n = temp;
temp = temp.parent;
}
if (temp == null) {
return null;
} else {
return temp;
}
}
} else if (n.getData() > target) {
return successor(target, n.getLeft());
} else {
return successor(target, n.getRight());
}
}
/**
* The insert method will insert a new value starting from the root. It will
* find the right place in the BinarySearchTree to create a new node containing the value.
* The implementation of this method is recursive.
*/
public Node insert(int element, Node n, Node p) {
if (root == null) {
root = new Node(element, null, null, null);
return root;
}
if (n == null) {
return new Node(element, null, null, p);
} else {
if (element <= n.getData()) {
//System.out.println("set left");
n.setLeft(insert(element, n.getLeft(), n));
} else {
//System.out.println("set right");
n.setRight(insert(element, n.getRight(), n));
}
return n;
}
}
/**
* The delete method will delete a node from the BinarySearchTree based on a given value.
* This method is also recursive.
*/
public boolean delete(int target, Node n) {
// case #1: root is empty
if (n == null) return false;
if (n.getData() == target) {
if (n.getLeft() == null) {
// case #2: target found at root with no left child
if (n == root) {
root = root.getRight();
return true;
}
// case #3: target found with no left child
if (n == n.parent.getLeft()) {
n.parent.setLeft(n.getRight());
} else {
n.parent.setRight(n.getRight());
}
return true;
// case #4: there's a left child
} else {
Node temp = n;
n.setData(n.getLeft().getRightmost().getData());
n.setLeft(n.getLeft().removeRightmost());
return true;
}
} else if (n.getData() > target) {
return delete(target, n.getLeft());
} else {
return delete(target, n.getRight());
}
}
/**
* This is the method that returns a string of the pre-order of the BinarySearchTree.
*/
public String preOrder(Node n, String s) {
String print = s;
// Process the root.
if (n != null) {
print += n.getData() + " ";
}
// Process the nodes in the left subtree with a recursive call.
if (n.getLeft() != null) {
print += preOrder(n.getLeft(), s);
}
// Process the nodes in the right subtree with a recursive call.
if (n.getRight() != null) {
print += preOrder(n.getRight(), s);
}
return print;
}
/**
* This is the method that returns a string of the in-order of the BinarySearchTree.
*/
public String inOrder(Node n, String s) {
String print = s;
// Process the nodes in the left subtree with a recursive call.
if (n.getLeft() != null) {
print += inOrder(n.getLeft(), s);
}
// Process the root.
if (n != null) {
print += n.getData() + " ";
}
// Process the nodes in the right subtree with a recursive call.
if (n.getRight() != null) {
print += inOrder(n.getRight(), s);
}
return print;
}
/**
* This is the method that returns a string of the post-order of the BinarySearchTree.
*/
public String postOrder(Node n, String s) {
String print = s;
// Process the nodes in the left subtree with a recursive call.
if (n.getLeft() != null) {
print += postOrder(n.getLeft(), s);
}
// Process the nodes in the right subtree with a recursive call.
if (n.getRight() != null) {
print += postOrder(n.getRight(), s);
}
// Process the root.
if (n != null) {
print += n.getData() + " ";
}
return print;
}
}

Node.java:

public class Node {
protected int data;
protected Node left;
protected Node right;
protected Node parent;
public Node(int initalData, Node initalLeft, Node initalRight, Node initalParent) {
data = initalData;
left = initalLeft;
right = initalRight;
parent = initalParent;
}
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setData(int newData) {
data = newData;
}
public void setLeft(Node newLeft) {
left = newLeft;
}
public void setRight(Node newRight) {
right = newRight;
}
public boolean isLeaf() {
if (left == null && right == null) {
return true;
} else {
return false;
}
}
public Node getLeftmost() {
if (left != null) {
return left.getLeftmost();
} else {
return this;
}
}
public Node getRightmost() {
if (right != null) {
return right.getRightmost();
} else {
return this;
}
}
public Node removeLeftmost() {
if (left == null) {
return right;
} else {
left = left.removeLeftmost();
return this;
}
}
public Node removeRightmost() {
if (right == null) {
return left;
} else {
right = right.removeRightmost();
return this;
}
}
}

Output:


Related Solutions

* Make sure you turn in your code (take a screen shot of your code in...
* Make sure you turn in your code (take a screen shot of your code in R)and answers. Conduct the hypothesis and solve questions by using R. 2) A random sample of 12 graduates of a secretarial school averaged 73.2 words per minute with a standard deviation of 7.9 words per minute on a typing test. What can we conclude, at the .05 level, regarding the claim that secretaries at this school average less than 75 words per minute on...
Please note that I tried a screen shot and scanned, and I could not paste this...
Please note that I tried a screen shot and scanned, and I could not paste this on this site because of the browser will not allow, and I called, and was told to type this. The problem and questio Analysis of Variance by hand. The average number of purchases in three different stores are compared to determine if they are significantly different. The following summary statistics is given for each store. State the null and alternative hypothesis, calculate the F-statistics,...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits...
Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits strings, you can assume each strings only contains 0's and 1's. (Note: the two strings might have the same length or not!) for example: hamming('010001001111', '01010100') should return 5(1 bit changed plus 4 bits "lost" from the end). 2. write a main function that ask user for two file names, open files and read the 1st line of each, and compares them using Hamming...
Hello, I am working on a C-program that deals with memory mapping, Please show all code...
Hello, I am working on a C-program that deals with memory mapping, Please show all code (code must be in the C) and outputs. The instructions are as follows INSTRUCTIONS: You have two sample c codes, one for the client and one for the server. IF the client makes a change in the shared memory, those changes will show on the server side. Your task is to adapt the change strategy to develop two applications so they can send messages...
USE MINITAB 18 attach screen shots.... End Result Hospital wants to assess the effectiveness of diabetes...
USE MINITAB 18 attach screen shots.... End Result Hospital wants to assess the effectiveness of diabetes education through A1c levels. The hospital created three groups of patients. In the control group no instruction is provided, in the second group instruction is provided by physicians, and in the third group instruction is provided by RNs. Use the appropriate statistical test to determine if there is any difference in patient A1c levels between the three groups. Interpret using α = .05. Include...
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
Can someone send me a screen shot of how to input this into SPSS data and...
Can someone send me a screen shot of how to input this into SPSS data and variable view to obtain the information provided below? Question 1 The nominal scale of measurement is used to measure academic program in this example because data collected for the students is nothing but the names of their major subjects or classes and their status of mood whether they are nervous or excited. Question 2 The nominal scale of measurement is used to measure feeling...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
My code is working fine but after selecting an option and at the end I want...
My code is working fine but after selecting an option and at the end I want user could enter another option again without going back to menu. However, in my code when I enter another option, it prints the whole menu again then I can select option.Could anyone help me on that? #include "stdafx.h" #include #include #include using namespace std; const double pi = 3.14159265358979323846; const double deg_to_rad = (pi / 180); int main() { int option; do { cout...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein sequences as input and finds all the transmembrane domains and returns them in a list for each sequence in the list with given nonpolar regions and returns the lists for those. 1. This code should call two other functions that you write: regionProteinFind takes in a protein sequence and should return a list of 10 amino acid windows, if the sequence is less than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT