BST.Java   import java.util.NoSuchElementException; /** * Your implementation of a BST. */ public class BST> { /* * Do not add new instance variables or modify existing ones. */ private BSTNode root; private int size; /* * Do not add a constructor. */ /** * Adds the data to the tree. * * This must be done recursively. * * The new data should become a leaf in the tree. * * Traverse the tree to find the appropriate location. If the data is * already in the tree, then nothing should be done (the duplicate * shouldn't get added, and size should not be incremented). * * Should be O(log n) for best and average cases and O(n) for worst case. * * @param data The data to add to the tree. * @throws java.lang.IllegalArgumentException If data is null. */ public void add(T data) { // WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)! } /** * Removes and returns the data from the tree matching the given parameter. * * This must be done recursively. * * There are 3 cases to consider: * 1: The node containing the data is a leaf (no children). In this case, * simply remove it. * 2: The node containing the data has one child. In this case, simply * replace it with its child. * 3: The node containing the data has 2 children. Use the SUCCESSOR to * replace the data. You should use recursion to find and remove the * successor (you will likely need an additional helper method to * handle this case efficiently). * * Do NOT return the same data that was passed in. Return the data that * was stored in the tree. * * Hint: Should you use value equality or reference equality? * * Must be O(log n) for best and average cases and O(n) for worst case. * * @param data The data to remove. * @return The data that was removed. * @throws java.lang.IllegalArgumentException If data is null. * @throws java.util.NoSuchElementException If the data is not in the tree. */ public T remove(T data) { // WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)! } /** * Returns the root of the tree. * * For grading purposes only. You shouldn't need to use this method since * you have direct access to the variable. * * @return The root of the tree */ public BSTNode getRoot() { // DO NOT MODIFY THIS METHOD! return root; } /** * Returns the size of the tree. * * For grading purposes only. You shouldn't need to use this method since * you have direct access to the variable. * * @return The size of the tree */ public int size() { // DO NOT MODIFY THIS METHOD! return size; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Provided Code:

 

BST.Java

 

import java.util.NoSuchElementException;

/**
* Your implementation of a BST.
*/
public class BST<T extends Comparable<? super T>> {

/*
* Do not add new instance variables or modify existing ones.
*/
private BSTNode<T> root;
private int size;

/*
* Do not add a constructor.
*/

/**
* Adds the data to the tree.
*
* This must be done recursively.
*
* The new data should become a leaf in the tree.
*
* Traverse the tree to find the appropriate location. If the data is
* already in the tree, then nothing should be done (the duplicate
* shouldn't get added, and size should not be incremented).
*
* Should be O(log n) for best and average cases and O(n) for worst case.
*
* @param data The data to add to the tree.
* @throws java.lang.IllegalArgumentException If data is null.
*/
public void add(T data) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
}

/**
* Removes and returns the data from the tree matching the given parameter.
*
* This must be done recursively.
*
* There are 3 cases to consider:
* 1: The node containing the data is a leaf (no children). In this case,
* simply remove it.
* 2: The node containing the data has one child. In this case, simply
* replace it with its child.
* 3: The node containing the data has 2 children. Use the SUCCESSOR to
* replace the data. You should use recursion to find and remove the
* successor (you will likely need an additional helper method to
* handle this case efficiently).
*
* Do NOT return the same data that was passed in. Return the data that
* was stored in the tree.
*
* Hint: Should you use value equality or reference equality?
*
* Must be O(log n) for best and average cases and O(n) for worst case.
*
* @param data The data to remove.
* @return The data that was removed.
* @throws java.lang.IllegalArgumentException If data is null.
* @throws java.util.NoSuchElementException If the data is not in the tree.
*/
public T remove(T data) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
}

/**
* Returns the root of the tree.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return The root of the tree
*/
public BSTNode<T> getRoot() {
// DO NOT MODIFY THIS METHOD!
return root;
}

/**
* Returns the size of the tree.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return The size of the tree
*/
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
}

 

BSTNode.Java

 

/**
* Node class used for implementing the BST.
*
* DO NOT MODIFY THIS FILE!!
*
* @author CS 1332 TAs
* @version 1.0
*/
public class BSTNode<T extends Comparable<? super T>> {

private T data;
private BSTNode<T> left;
private BSTNode<T> right;

/**
* Constructs a BSTNode with the given data.
*
* @param data the data stored in the new node
*/
BSTNode(T data) {
this.data = data;
}

/**
* Gets the data.
*
* @return the data
*/
T getData() {
return data;
}

/**
* Gets the left child.
*
* @return the left child
*/
BSTNode<T> getLeft() {
return left;
}

/**
* Gets the right child.
*
* @return the right child
*/
BSTNode<T> getRight() {
return right;
}

/**
* Sets the data.
*
* @param data the new data
*/
void setData(T data) {
this.data = data;
}

/**
* Sets the left child.
*
* @param left the new left child
*/
void setLeft(BSTNode<T> left) {
this.left = left;
}

/**
* Sets the right child.
*
* @param right the new right child
*/
void setRight(BSTNode<T> right) {
this.right = right;
}
}

Binary Search Tree
For this assignment, you will be coding the add() and remove() methods of a binary search tree. A binary search tree, or
BST, is a collection of nodes, each having a data item and a reference pointing to a left and a right child node. The BST must
adhere to the following order property: for any given node, its left child's data and all of its children's data must be less than
the current node while its right child's data and all of its children's data must be greater than the current node. In order to
compare the data, all elements added to the tree must implement Java's generic Comparable interface. Since trees are
naturally recursive structures, each of these methods should be implemented recursively.
IMPORTANT:
• You will be given unlimited attempts on this assignment, with no cooldown between submissions.
• Please run your code before each submission to ensure that there are no formatting errors! If there are
formatting errors in your code, your code will not be graded and a submission attempt will be logged. For
more information, please review the Vocareum overview below.
BSTNode
A BSTNode class is provided to you and will be used to represent the nodes in the tree. This file should be treated as read-
only and should not be modified in any way. This BSTNode class contains getter and setter methods to access and mutate
the structure of the nodes. Please make sure that you understand how this class works, as interaction with this class is
crucial for this assignment.
Pointer Reinforcement
Since both the add) and remove() methods may change the structure of the tree, we highly recommend that you use a
technique called pointer reinforcement. Although using pointer reinforcement is not required, it will help to make your code
cleaner, and it'll also help greatly in future assignments if you end up taking the next course in our series! Below is a video
created by our 1332 TAS, timestamped to a section on pointer reinforcement.
Pointer Reinforcement Overview
Comparable
As stated, the data in the BST must implement the Comparable interface. As you'll see in the les, the generic typing has
been specified to require that it implements the Comparable interface. You use the interface by making a method call like
data1.compareTo(data2). This will return an int, and the value tells you how data1 and data2 are in relation to each other.
•
If the int is positive, then datal is larger than data2.
• If the int is negative, then datal is smaller than data2.
• If the int is zero, then datal equals data2.
Note that the returned value can be any integer in Java's int range, not just -1, 0, 1.
Successor
Recall that earlier in the modules you learned about the successor and predecessor of a node in a tree. As a refresher, the
successor of a node, n, is the node in the tree that contains the smallest data that is larger than n's data. The predecessor of
a node, n, is the node in the tree that contains the largest data that is smaller than n's data. When removing a node from a
BST that has two children, we can choose to replace the removed node with either it's successor or predecessor. For the 2-
child case in remove(), you will be replacing the removed node with its successor node, NOT the predecessor node. For
more details, please refer to the javadocs for remove().
Helper Methods
You'll also notice that the public method stubs we've provided do not contain the parameters necessary for recursion to
work, so these public methods should act as "wrapper methods" for you to use. You will have to write private recursive
helper methods and call them in these wrapper methods. All of these helper methods must be private. To reiterate,
do not change the method headers for the provided methods.
Transcribed Image Text:Binary Search Tree For this assignment, you will be coding the add() and remove() methods of a binary search tree. A binary search tree, or BST, is a collection of nodes, each having a data item and a reference pointing to a left and a right child node. The BST must adhere to the following order property: for any given node, its left child's data and all of its children's data must be less than the current node while its right child's data and all of its children's data must be greater than the current node. In order to compare the data, all elements added to the tree must implement Java's generic Comparable interface. Since trees are naturally recursive structures, each of these methods should be implemented recursively. IMPORTANT: • You will be given unlimited attempts on this assignment, with no cooldown between submissions. • Please run your code before each submission to ensure that there are no formatting errors! If there are formatting errors in your code, your code will not be graded and a submission attempt will be logged. For more information, please review the Vocareum overview below. BSTNode A BSTNode class is provided to you and will be used to represent the nodes in the tree. This file should be treated as read- only and should not be modified in any way. This BSTNode class contains getter and setter methods to access and mutate the structure of the nodes. Please make sure that you understand how this class works, as interaction with this class is crucial for this assignment. Pointer Reinforcement Since both the add) and remove() methods may change the structure of the tree, we highly recommend that you use a technique called pointer reinforcement. Although using pointer reinforcement is not required, it will help to make your code cleaner, and it'll also help greatly in future assignments if you end up taking the next course in our series! Below is a video created by our 1332 TAS, timestamped to a section on pointer reinforcement. Pointer Reinforcement Overview Comparable As stated, the data in the BST must implement the Comparable interface. As you'll see in the les, the generic typing has been specified to require that it implements the Comparable interface. You use the interface by making a method call like data1.compareTo(data2). This will return an int, and the value tells you how data1 and data2 are in relation to each other. • If the int is positive, then datal is larger than data2. • If the int is negative, then datal is smaller than data2. • If the int is zero, then datal equals data2. Note that the returned value can be any integer in Java's int range, not just -1, 0, 1. Successor Recall that earlier in the modules you learned about the successor and predecessor of a node in a tree. As a refresher, the successor of a node, n, is the node in the tree that contains the smallest data that is larger than n's data. The predecessor of a node, n, is the node in the tree that contains the largest data that is smaller than n's data. When removing a node from a BST that has two children, we can choose to replace the removed node with either it's successor or predecessor. For the 2- child case in remove(), you will be replacing the removed node with its successor node, NOT the predecessor node. For more details, please refer to the javadocs for remove(). Helper Methods You'll also notice that the public method stubs we've provided do not contain the parameters necessary for recursion to work, so these public methods should act as "wrapper methods" for you to use. You will have to write private recursive helper methods and call them in these wrapper methods. All of these helper methods must be private. To reiterate, do not change the method headers for the provided methods.
15
10
15
10
15
10
15
50
50
50
75
75
75
75
Add() Example
100
Remove() Example
(0 child case)
100
Ack 25
100
Remove 10
(1 child case)
100
Romov 5
10
(2 child case)
Remove 15
Replace w/ sucesso
5
15
10
15
15
20
50
10
50
50
25
75
75
75
75
100
100
100
100
Transcribed Image Text:15 10 15 10 15 10 15 50 50 50 75 75 75 75 Add() Example 100 Remove() Example (0 child case) 100 Ack 25 100 Remove 10 (1 child case) 100 Romov 5 10 (2 child case) Remove 15 Replace w/ sucesso 5 15 10 15 15 20 50 10 50 50 25 75 75 75 75 100 100 100 100
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education