Currently My remove method is not removing any target from any part of a tree(ro
ID: 3917958 • Letter: C
Question
Currently My remove method is not removing any target from any part of a tree(root, child, left, right, ect), what can be done to make it work?
Tree Class:
public class Tree>
{
private BSTNode root;
private int numItems = 0;
public Tree(){
}
public boolean add(E newElement){
{
if (root == null)
{
root = new BSTNode(newElement,null, null);
++numItems;
return true;
}
BSTNode cursor = root;
while (true)
{
int compareholder = cursor.getData().compareTo(newElement);
if (compareholder == 0)
{
// key is already in the tree
return false;
}
else if (compareholder > 0)
{
if (cursor.getLeft() != null)
{
cursor = cursor.getLeft();
}
else
{
cursor.setLeft(new BSTNode(newElement,null, null));
++numItems;
return true;
}
}
else
{
if (cursor.getRight() != null)
{
cursor = cursor.getRight();
}
else
{
cursor.setRight(new BSTNode(newElement,null, null));
++numItems;
return true;
}
}
}
}
}
public boolean remove(E target) {
boolean found = false;
BSTNode parentOfCursor = null;
BSTNode cursor = root;
while (cursor != null && target != cursor.getData()) {
parentOfCursor = cursor;
if (target < cursor.getData()) {
cursor = cursor.getLeft();
} else {
cursor = cursor.getRight();
}
}
if (cursor == null) {
return false;
} else if (cursor.getLeft() == null) {
if (parentOfCursor == null) {
root = cursor.getRight();
} else if (cursor == parentOfCursor.getLeft()) {
parentOfCursor.setLeft(cursor.getRight());
} else {
parentOfCursor.setRight(cursor.getRight());
}
return true;
} else {
cursor.setData(cursor.getLeft().getRightMostData());
cursor.setLeft(cursor.getLeft().removeRightMost());
return true;
}
}
public int size(){
return numItems;
}
public void printTree(){
if (root != null){
root.inorderPrint( );
}
}
}//Tree
BSTNode Class:
public class BSTNode> {
private E data;
private BSTNode left;
private BSTNode right;
public BSTNode(E newData, BSTNode newLeft, BSTNode newRight){
data = newData;
left = newLeft;
right = newRight;
}
public E getData() {
return data;
}
public BSTNode getLeft(){
return left;
}
public BSTNode getRight(){
return right;
}
public E getRightMostData(){
if(right == null){
return data;
}
else{
return right.getRightMostData();
}
}
public void inorderPrint( ){
if (left != null)
left.inorderPrint( );
System.out.println(data);
if (right != null)
right.inorderPrint( );
}
public BSTNode removeRightMost(){
if(right == null){
return left;
}
else{
right = right.removeRightMost();
return this;
}
}
public void setData(E newData){
data = newData;
}
public void setLeft(BSTNode newLeft){
left = newLeft;
}
public void setRight(BSTNode newRight){
right = newRight;
}
}//BSTNode
Car Class(just example class Im using):
public class Car implements Comparable
{
private int yearModel;
private String make;
private int speed;
/**
Constructor`
@param newYearModel The year of the Car.
@param newMake The make of the Car.
*/
public Car(int newYearModel, String newMake)
{
yearModel = newYearModel;
make = newMake;
speed = 0;
}
/**
The getYearModel method returns a Car
object's yearModel.
@return The value in the yearModel field.
*/
public int getYearModel()
{
return yearModel;
}
/**
The getMake method returns a Car
object's make.
@return The value in the make field.
*/
public String getMake()
{
return make;
}
/**
The getSpeed method returns a Car
object's speed.
@return The value in the speed field.
*/
public int getSpeed()
{
return speed;
}
/**
The accelerate method increases speed.
*/
public void accelerate()
{
// i am ommitting a regulator
speed += 5;
}
/**
The brake method decreases speed.
*/
public void brake()
{
speed-=5;
if (speed < 0)
speed = 0;
}
/**
The equals method compares this Car to another Object .
@param The object to test for equality.
@return boolean with result of test for equality.
*/
public boolean equals(Object obj)
{
if (!(obj instanceof Car))
throw new ClassCastException("A Car object expected.");
Car otherCar = (Car) obj; // cast the Object to a Car
if (make.equalsIgnoreCase(otherCar.getMake())
&& yearModel == otherCar.getYearModel())
return true;
else
return false;
}
/**
The compareTo method compares this Car to another Object .
if this car is before other car, return a value < 0.
if this car is after other car, return a value > 0.
if this car is equal to other car, return 0.
@param The object to test for equality.
@return int with result of comparison.
*/
public int compareTo(Car anotherCar)
throws ClassCastException
{
if (!(anotherCar instanceof Car))
throw new ClassCastException("A Car object expected.");
if (getYearModel() < anotherCar.getYearModel())
return -1;
else if (getYearModel() > anotherCar.getYearModel())
return 1;
else
return make.compareToIgnoreCase(anotherCar.getMake());
}
/**
The toString method returns a Car
object's yearModel, make, speed as a String.
@return A String with values in the yearModel, make, speed fields.
*/
public String toString()
{
return "[Car " + yearModel +" "+ make+"]";
}
}
Explanation / Answer
You code seems to be perfect.. however i can think of one more condition, which i have added below in remove method:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.