To create a node for a binary search tree (BTNode, ) and to create a binary search tree of
comparable objects (Tree BTNode BTNode -data:E -left: BTNode -right: BTNode +BTNode
(newData:E, newLeft: BTNode, newRight: BTNode) +getData():E +getLeft():BTNode
+getRight():BTNode +getRightMostData():E +inOrderPrint():void +removeRightmost():
BTNode +setData(newData:E):void +setLeft(newleft: BTNode):void +setRight(newRight:
BTNode):void needed for remove needed for remove Tree for Lab 7 Tree -root: 8TNode -
numltems:int +Tree() +add(element:E):void +remove(element:E):boolean + remove
+size():int + printTree():void
Solution
//BTNode.java
public class BTNode
{
private E data;
private BTNode left;
private BTNode right;
public BTNode(E newData, BTNode newLeft, BTNode newRight)
{
data = newData;
left = newLeft;
right = newRight;
}//end constructor
/**
* Returns the data
* @return The data
*/
public E getData()
{
return data;
}//end getData
/**
* Returns the left link
* @return the left link
*/
public BTNode getLeft()
{
return left;
}//end getLeft
/**
* Returns the right link
* @return the right link
*/
public BTNode getRight()
{
return right;
}//end getRight
/**
* Prints the tree in order from left to right.
*/
public void inOrderPrint()
{
if (left != null){
left.inOrderPrint();
}//end left
System.out.println(data);
if(right != null){
right.inOrderPrint();
}//end right
}//end inOrderPrint
/**
* Sets the data in the node
* @param newData The new data to be passed
*/
public void setData(E newData)
{
data = newData;
}//end setData
/**
* Sets a new link to left
* @param newLeft the new link to go left
*/
public void setLeft(BTNode newLeft)
{
left = newLeft;
}//end setLeft
/**
* Sets a new link to the right
* @param newRight the new link to the right
*/
public void setRight(BTNode newRight)
{
right = newRight;
}//end setRight
public E getRightMostData()
{
if(right == null){
return data;
} else {
return right.getRightMostData();
}//end right == null if-else
}//end getRightMostData
public BTNode removeRightmost()
{
if (right == null){
return left;
} else {
right = right.removeRightmost();
return this;
}//end if right == null if=-else
}//end removeRightMost
}//end BTNode
=====================================================================
==
//Tree.java
public class Tree>
{
private BTNode root;
private int numItems;
/**
* No-arg constructor
*/
public Tree()
{
root = null;
numItems = 0;
}//end constructor
public void add(E element)
{
if (root == null) {
root = new BTNode(element, null, null);
}else{
BTNode cursor = root;
boolean done = false;
while(!done){
if (element.compareTo(cursor.getData()) <= 0){//cursor.getData().compareTo(element) > 0){
if (cursor.getLeft() == null) {
cursor.setLeft(new BTNode(element,null,null));
done = true;
} else {
cursor = cursor.getLeft();
} //end left is null if-else
}else{
if(cursor.getRight() == null){
cursor.setRight(new BTNode(element, null, null));
done = true;
}else{
cursor = cursor.getRight();
}//end cursor.getRight if-else
}/.
Seal of Good Local Governance (SGLG) 2024Final.pptx
To create a node for a binary search tree (BTNode, ) and to create a .pdf
1. To create a node for a binary search tree (BTNode, ) and to create a binary search tree of
comparable objects (Tree BTNode BTNode -data:E -left: BTNode -right: BTNode +BTNode
(newData:E, newLeft: BTNode, newRight: BTNode) +getData():E +getLeft():BTNode
+getRight():BTNode +getRightMostData():E +inOrderPrint():void +removeRightmost():
BTNode +setData(newData:E):void +setLeft(newleft: BTNode):void +setRight(newRight:
BTNode):void needed for remove needed for remove Tree for Lab 7 Tree -root: 8TNode -
numltems:int +Tree() +add(element:E):void +remove(element:E):boolean + remove
+size():int + printTree():void
Solution
//BTNode.java
public class BTNode
{
private E data;
private BTNode left;
private BTNode right;
public BTNode(E newData, BTNode newLeft, BTNode newRight)
{
data = newData;
left = newLeft;
right = newRight;
}//end constructor
/**
* Returns the data
* @return The data
*/
public E getData()
{
return data;
}//end getData
/**
2. * Returns the left link
* @return the left link
*/
public BTNode getLeft()
{
return left;
}//end getLeft
/**
* Returns the right link
* @return the right link
*/
public BTNode getRight()
{
return right;
}//end getRight
/**
* Prints the tree in order from left to right.
*/
public void inOrderPrint()
{
if (left != null){
left.inOrderPrint();
}//end left
System.out.println(data);
if(right != null){
right.inOrderPrint();
}//end right
}//end inOrderPrint
/**
* Sets the data in the node
3. * @param newData The new data to be passed
*/
public void setData(E newData)
{
data = newData;
}//end setData
/**
* Sets a new link to left
* @param newLeft the new link to go left
*/
public void setLeft(BTNode newLeft)
{
left = newLeft;
}//end setLeft
/**
* Sets a new link to the right
* @param newRight the new link to the right
*/
public void setRight(BTNode newRight)
{
right = newRight;
}//end setRight
public E getRightMostData()
{
if(right == null){
return data;
} else {
return right.getRightMostData();
}//end right == null if-else
}//end getRightMostData
public BTNode removeRightmost()
{
4. if (right == null){
return left;
} else {
right = right.removeRightmost();
return this;
}//end if right == null if=-else
}//end removeRightMost
}//end BTNode
=====================================================================
==
//Tree.java
public class Tree>
{
private BTNode root;
private int numItems;
/**
* No-arg constructor
*/
public Tree()
{
root = null;
numItems = 0;
}//end constructor
public void add(E element)
{
if (root == null) {
root = new BTNode(element, null, null);
}else{
BTNode cursor = root;
boolean done = false;
while(!done){
if (element.compareTo(cursor.getData()) <= 0){//cursor.getData().compareTo(element) > 0){
5. if (cursor.getLeft() == null) {
cursor.setLeft(new BTNode(element,null,null));
done = true;
} else {
cursor = cursor.getLeft();
} //end left is null if-else
}else{
if(cursor.getRight() == null){
cursor.setRight(new BTNode(element, null, null));
done = true;
}else{
cursor = cursor.getRight();
}//end cursor.getRight if-else
}//end compareto if-else
}//end while
}//end root ==null if-else
numItems++;
}//end add
/**
* Get the number of elements in the tree
* @return The size of the tree
*/
public int size()
{
return numItems;
}//end size
/**
* Print the tree in order
*/
public void printTree()
{
if (root == null){
System.out.println("This tree is empty");
}else{
6. root.inOrderPrint();
}//end if-else
}//end inOrderPrint
public boolean remove(E target)
{
BTNode cursor = root;
BTNode parentOfCursor = null;
boolean found = false;
while (cursor != null && !found){
if (target.equals(cursor.getData())){
found = true;
}else{
if(target.compareTo(cursor.getData()) <= 0){
parentOfCursor = cursor;
cursor = cursor.getLeft();
}else{
parentOfCursor = cursor;
cursor = cursor.getRight();
}//end target.compareTo if-else
}//end target.equals if-else
}//end cursor && found while
if (cursor != null){
if (cursor.getLeft() == null && cursor == root){
root = root.getRight();
} else if (cursor.getLeft() == null && cursor!= root){
if (cursor == parentOfCursor.getLeft()){
parentOfCursor.setLeft(cursor.getRight());
} else {
parentOfCursor.setRight(cursor.getRight());
}//end cursor ==parentOfCursor.setLEft if-else
} else{
cursor.setData(cursor.getLeft().getRightMostData());
cursor.setLeft(cursor.getLeft().removeRightmost());
7. } //end cursor == root if-else
}//end found if
return found;
}
}//end Tree