Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need a program in JAVA Using a BST in an Application Create a class SimpleBag th

ID: 3689140 • Letter: N

Question

Need a program in JAVA

Using a BST in an Application Create a class SimpleBag that uses a binary search tree to store the bag items.The class should have the methods listed below. Create a Main class to test your SimpleBag class.

a. SimpleBag(): default constructor; creates an empty bag

b. boolean isEmpty(): determines whether the bag is empty

c. void print(): prints the SimpleBag elements

d. void clear(): removes all of the items from the bag

e. void add(int item): adds an item to the bag

f. int count(int item): counts the number of occurrences of items in the bag.

Explanation / Answer

SimpleBag.java


/*
* Java Program to Implement Binary Search Tree
*/

import java.util.Scanner;

/* Class BSTNode */
class BSTNode
{
BSTNode left, right;
int data;

/* Constructor */
public BSTNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public BSTNode(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set left node */
public void setLeft(BSTNode n)
{
left = n;
}
/* Function to set right node */
public void setRight(BSTNode n)
{
right = n;
}
/* Function to get left node */
public BSTNode getLeft()
{
return left;
}
/* Function to get right node */
public BSTNode getRight()
{
return right;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}   
}

/* Class BST */
class BST
{
private BSTNode root;

/* Constructor */
public BST()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Functions to insert data */
public void add(int data)
{
root = add(root, data);
}
/* Function to insert data recursively */
private BSTNode add(BSTNode node, int data)
{
if (node == null)
node = new BSTNode(data);
else
{
if (data <= node.getData())
node.left = add(node.left, data);
else
node.right = add(node.right, data);
}
return node;
}
/* Functions to delete data */
public void delete(int k)
{
if (isEmpty())
System.out.println("Tree Empty");
else if (count(k) == false)
System.out.println("Sorry "+ k +" is not present");
else
{
root = delete(root, k);
System.out.println(k+ " deleted from the tree");
}
}
private BSTNode delete(BSTNode root, int k)
{
BSTNode p, p2, n;
if (root.getData() == k)
{
BSTNode lt, rt;
lt = root.getLeft();
rt = root.getRight();
if (lt == null && rt == null)
return null;
else if (lt == null)
{
p = rt;
return p;
}
else if (rt == null)
{
p = lt;
return p;
}
else
{
p2 = rt;
p = rt;
while (p.getLeft() != null)
p = p.getLeft();
p.setLeft(lt);
return p2;
}
}
if (k < root.getData())
{
n = delete(root.getLeft(), k);
root.setLeft(n);
}
else
{
n = delete(root.getRight(), k);
root.setRight(n);   
}
return root;
}
/* Functions to count number of nodes */
public int count()
{
return count(root);
}
/* Function to count number of nodes recursively */
private int count(BSTNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += count(r.getLeft());
l += count(r.getRight());
return l;
}
}
/* Functions to search for an element */
public boolean count(int val)
{
return count(root, val);
}
/* Function to search for an element recursively */
private boolean count(BSTNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getData();
if (val < rval)
r = r.getLeft();
else if (val > rval)
r = r.getRight();
else
{
found = true;
break;
}
found = count(r, val);
}
return found;
}
/* Function for inorder traversal */
public void inorder()
{
inorder(root);
}
private void inorder(BSTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root);
}
private void preorder(BSTNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());   
preorder(r.getRight());
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(root);
}
private void postorder(BSTNode r)
{
if (r != null)
{
postorder(r.getLeft());   
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
/** * Java function to clear the binary search tree. * Time complexity of this method is O(1) */
public void clear() { root = null; }

public void print(){
/* Display tree */
System.out.print(" Post order : ");
postorder();
System.out.print(" Pre order : ");
preorder();
System.out.print(" In order : ");
inorder();
}

}

/* Class BinarySearchTree */
public class SimpleBag
{
public static void main(String[] args)
{   
Scanner scan = new Scanner(System.in);
/* Creating object of BST */
BST bst = new BST();
System.out.println("Binary Search Tree Test ");
char ch;
/* Perform tree operations */
do
{
System.out.println(" Binary Search Tree Operations ");
System.out.println("1. Add ");
System.out.println("2. print");
System.out.println("3. clear");
System.out.println("4. count nodes");
System.out.println("5. check empty");

int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
bst.add( scan.nextInt() );   
break;
case 2 :
bst.print();   
break;   
case 3 :
bst.clear();
break;
case 4 :
System.out.println("Nodes = "+ bst.count());
break;   
case 5 :
System.out.println("Empty status = "+ bst.isEmpty());
break;
default :
System.out.println("Wrong Entry ");
break;   
}
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}

Output:

Binary Search Tree Test


Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
1
Enter integer element to insert
2

Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
1
Enter integer element to insert
3

Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
1
Enter integer element to insert
4

Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
2

Post order : 4 3 2
Pre order : 2 3 4
In order : 2 3 4
Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
4
Nodes = 3

Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
5
Empty status = false

Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
3

Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
2

Post order :
Pre order :
In order :
Do you want to continue (Type y or n)

y

Binary Search Tree Operations

1. Add
2. print
3. clear
4. count nodes
5. check empty
5
Empty status = true

Do you want to continue (Type y or n)

n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote