Question

In: Computer Science

In Java For this assignment you will implement two methods, find() and replace() relating to Binary...

In Java

For this assignment you will implement two methods, find() and replace() relating to Binary Search Trees. These methods are both to be recursive functions. The signatures for those functions must be:

 /* This method takes a tree node and a key as an argument and returns the tree node if the key is found and returns null if the key is not found. */ 
BST find(BST T, char key)

/* This method takes a tree node and a key as an argument and inserts the tree node if the key is already in the tree it does not insert the node. It returns the new tree. */
BST insert(BST T, char key)

Also, for this assignment you are to implement the preOrder(), inOrder(), and postOrder() tree traversal methods. These methods are also to be recursive functions.

All five of these methods are detailed and discussed in the lecture notes and videos. You are allowed to use the pseudo-code and any associated code described in the lecture and videos. However, you are not required for any of it.

The input for this program will be from the keyboard and accept single-character keys, labeled 'A' - 'Z'.

Programming Notes & Rules:

  1. You can not delete any code in the BST class. But you can add code to the BST class if you wish.
  2. All five methods must be recursive. Zero credit will be given for any non-recursive method even if it works correctly.
  3. Do not assume any maximum length for any input string.
  4. You need not test for the null or empty string ("") case,
  5. You are not allowed to add any arrays or ArrayLists or any Java built-in (ADTs), such as Lists, Sets, Maps, Stacks, Queues, Deques, Trees, Graphs, Heaps, etc. Or add any class that inherits any of those ADTs.
  6. For your node and list class, you can use the code that was used in the book, video, and lecture notes related to the node and lists class examples.
  7. You are not allowed to use Java Generics.
  8. If hard-coding detected in any part of your solution your score will be zero for the whole assignment.

Starter Code:

import java.util.Scanner;  // Import the Scanner class

public class BST {
  
  public char key;
  public BST  left;
  public BST  right;

  public BST(char c) {
    key   = c;
    left  = null;
    right = null;
  }
  
  public static BST find(BST T, char key) {
  // put your solution here
  }

  public static BST insert(BST T, char key) {
  //  put your solution here
  }
    
  public static void preOrder(BST T) {
  //  put your solution here
  }
  
  public static void inOrder(BST T) {
  //  put your solution here 
  }
  
  public static void postOrder(BST T) {
  //  put your solution here
  }
  
  public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     BST t = null;
     String stream = input.nextLine();
     for (int i = 0; i < stream.length(); i++)
       t = insert(t, stream.charAt(i));
     System.out.print("Preorder: ");
     preOrder(t);
     System.out.println();
     System.out.print("Inorder: ");
     inOrder(t);
     System.out.println();
     System.out.print("Postorder: ");
     postOrder(t);
     System.out.println();
     System.out.println("Enter a character to search for: ");
     char c = input.nextLine().charAt(0);
     if (find(t, c) == null)
       System.out.println("The character " + "'" + c + "' was not found.");
     else
       System.out.println("The character " + "'" + c + "' was found.");
   }

Solutions

Expert Solution

WHOLE CODE WITH IMPLEMENTED ABOVE METHODS :

import java.util.Scanner;  // Import the Scanner class

public class BST {

   public char key;
   public BST  left;
   public BST  right;

   public BST(char c) {
      key   = c;
      left  = null;
      right = null;
   }


   public static BST find(BST T, char key) {

      if(T==null)
         return null;

      if(T.key==key)
         return T;

      else if(key>T.key){
         return find(T.right,key);
      }

      else{
         return find(T.left,key);
      }

   }


   public static BST insert(BST T, char key) {

      if(T==null){
         BST newNode = new BST(key);
         return newNode;
      }

      if(T.key>=key){
         T.left = insert(T.left,key);
      }

      else{
         T.right=insert(T.right,key);
      }

      return T;
   }


   public static void preOrder(BST T) {
      if(T==null)
         return;

      System.out.print(T.key+" ");
      preOrder(T.left);
      preOrder(T.right);
   }


   public static void inOrder(BST T) {
      if(T==null)
         return;

      preOrder(T.left);
      System.out.print(T.key+" ");
      preOrder(T.right);

   }


   public static void postOrder(BST T) {
      if(T==null)
         return;

      preOrder(T.left);
      preOrder(T.right);
      System.out.print(T.key+" ");

   }


   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      BST t = null;
      String stream = input.nextLine();
      for (int i = 0; i < stream.length(); i++)
         t = insert(t, stream.charAt(i));
      System.out.print("Preorder: ");
      preOrder(t);
      System.out.println();
      System.out.print("Inorder: ");
      inOrder(t);
      System.out.println();
      System.out.print("Postorder: ");
      postOrder(t);
      System.out.println();
      System.out.println("Enter a character to search for: ");
      char c = input.nextLine().charAt(0);
      if (find(t, c) == null)
         System.out.println("The character " + "'" + c + "' was not found.");
      else
         System.out.println("The character " + "'" + c + "' was found.");
   }



}


EXAMPLE :


Related Solutions

Overview In this assignment you are required to implement binary code comparator using Xilinx that it...
Overview In this assignment you are required to implement binary code comparator using Xilinx that it is compatible with the MXK Seven Segment Displays. You will draw your digital logic circuit using Xilinx and then simulate it to verify the functionality of your design. Software Requirements ? Xilinx ISE 10.1 or higher Specifications Binary Code Comparator The binary code comparator is to be implemented and made compatible with the seven 7-segment displays of the board. Represent the first five digits...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
JAVA WITH NO IMPORTS Method changeWords executes a find and replace like you would with ctrl+f...
JAVA WITH NO IMPORTS Method changeWords executes a find and replace like you would with ctrl+f            or command+f on here            The first value provided is the text that you search            Turn the text into an array of strings            find the string that is sent in the second position (find) and            replace it with the           changeWords("never have I ever", "ever", "cheated") returns ["never",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT