subject

In the BinarySearchTree class, write the new method: private E deletePredecessor(BSTNode where) This method should find and delete the in-order predecessor of where, returning the value that was deleted. If where itself is null, or if where doesn’t have a predecessor (i. e., the left subtree doesn’t exist), the method should return null. The method is declared private since it’s meant to be called only from another method of BinarySearchTree, to be written later.
In the BinarySearchTree class, write the new method: private E deleteSuccessor(BSTNode where)
This method should find and delete the in-order successor of where, returning the value that was deleted. If where itself is null, or if where doesn’t have a successor (i. e., the right subtree doesn’t exist), the method should return null. The method is declared private since it’s meant to be called only from another method of BinarySearchTree, to be written later.
In the BinarySearchTree class, write the new method: public E remove(E item)
This method should find and delete the item from the BST, returning the value that was re- moved. If the item is not found in the tree, the method should leave the tree unchanged and return null. Use the pseudocode in Figure 9.6.1 of the zyBook as a starting point. However, when removing a node with two children, your code should randomly select whether to use the in-order predecessor or successor in the delete algorithm. This will prevent the tree from get- ting too unbalanced. Call your previously written deletePredecessor and deleteSuccessormethods as needed.
In the BinarySearchTree class, write a main method that tests your removemethod. Be sure to test at least four cases:
• Removing a node with no children
• Removing a node with only a left child
• Removing a node with only a right child
• Removing a node with two children
BinarySearchTree
public class BinarySearchTree< E extends Comparable> {
// Maintains the root (first node) of the tree
private BSTNode root;
// Adds the newValue to the BST
// This method also prevents duplicate values from being added to the BST.
public void add(E newValue) {
// Create a new BSTNode that contains the newValue
BSTNode newNode = new BSTNode<>(newValue, null, null);
// If tree is empty, the newNode should become the root
if (root == null)
root = newNode;
else {
BSTNode currentNode = root;
while (currentNode != null) {
// Compare the newValue vs. the data in the currentNode
int compareResult = newValue. compareTo(currentNode. getData());
// newValue is "less than" the current node - go left
if (compareResult < 0) {
// If there is no left child for currentNode, make newNode
// the left child of currentNode
if (currentNode. getLeft() == null) {
currentNode. setLeft(newNode);
currentNode = null;
}
// If there *is* a left child for currentNode, just move
// currentNode down the left subtree
else
currentNode = currentNode. getLeft();
}
// newValue is "greater than" the current node - go right
else if (compareResult > 0) {
// If there is no right child for currentNode, make newNode
// the right child of currentNode
if (currentNode. getRight() == null) {
currentNode. setRight(newNode);
currentNode = null;
}
// If there *is* a right child for currentNode, just move
// currentNode down the right subtree
else
currentNode = currentNode. getRight();
}
// newValue is "equal to" the current node - exit the loop without adding newValue
else
currentNode = null;
}
}
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 12:20
Usually, when we sniff packets, we are only interested certain types of packets. we can do that by setting filters in sniffing. scapy’s filter use the bpf (berkeley packet filter) syntax; you can find the bpf manual from the internet. set the following filters and demonstrate your sniffer program again (each filter should be set separately): (a) capture only the icmp packet. (b) capture any tcp packet that comes from a particular ip and with a destination port number 23. (c) capture packets comes from or to go to a particular subnet. you can pick any subnet, such as 128.230.0.0/16; you should not pick the subnet that your vm is attached to.
Answers: 3
question
Computers and Technology, 22.06.2019 16:20
Consider the following statements, then select one of the answers below: the signal() function shown below registers "sig_handler()" as the signal handler function for the sigkill signal, without the complexity of using when the sigkill signal is sent to a process running this code, by a user typing "kill -kill ", where the correct process id is used for to target the process, sig_handler() will be executed.
Answers: 1
question
Computers and Technology, 22.06.2019 22:00
During physical science class ben and jerry connected three identical lightbulbs in parallel to a battery where happens when ben removes one of the lightbulbs from it’s socket
Answers: 2
question
Computers and Technology, 23.06.2019 00:30
Quic which one of the following is the most accurate definition of technology? a electronic tools that improve functionality b electronic tools that provide entertainment or practical value c any type of tool that serves a practical function d any type of tool that enhances communication
Answers: 1
You know the right answer?
In the BinarySearchTree class, write the new method: private E deletePredecessor(BSTNode where) Th...
Questions
Questions on the website: 13722367