Java please: Do Exercise 31 but use an array-based implementation of a binary se
ID: 3713556 • Letter: J
Question
Java please:
Do Exercise 31 but use an array-based implementation of a binary search tree. Include only user options 1, 2, 5, and 6. Your program should allow the user to specify the minimum density of the structure.
Exercise 31 instructions:
Code an application program that keeps track of student information at your college. Include their names, identification numbers, and grade point averages in a fully encapsulated, homogeneous, linked-based binary search tree. When launched, the user will be presented with the following menu:
Enter:
1 to insert a new student's information,
2 to fetch and output a student's information,
3 to delete a student's information,
4 to update a student's information,
5 to output all the student information in descending order, and
6 to exit the program.
Exercise 31 Code:
public class BinaryTree
{
TreeNode root;
public BinaryTree()
{
root = null;
}
public boolean insert(Listing7 newListing)
{
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
TreeNode n = new TreeNode();
if(n == null)
return false;
else
{
n.node = newListing.deepCopy();
n.lc = null;
n.rc = null;
if(root == null)
{
root = n;
}
else
{
findNode(newListing.getKey(), p, c );
if(newListing.getKey().compareTo(p.get().node.getKey( )) < 0)
p.get().lc = n;
else
p.get().rc = n;
}
return true;
}
}
public Listing7 fetch(String targetKey)
{
boolean found;
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
found = findNode(targetKey, p, c);
if(found == true)
return c.get().node.deepCopy();
else
return null;
}
public boolean delete(String targetKey)
{
boolean found;
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
TreeNode largest;
TreeNode nextLargest;
found = findNode(targetKey, p, c);
if(found == false)
return false;
else
{
if(c.get().lc == null && c.get().rc == null)
{
if(p.get().lc == c.get())
p.get().lc = null;
else
p.get().rc = null;
}
else if(c.get().lc == null || c.get().rc == null)
{
if(p.get().lc == c.get())
{
if(c.get().lc != null)
p.get().lc = c.get().lc;
else
p.get().lc = c.get().rc;
}
else
{
if(c.get().lc != null)
p.get().rc = c.get().lc;
else
p.get().rc = c.get().rc;
}
}
else
{
nextLargest = c.get().lc;
largest = nextLargest.rc;
if(largest != null)
{
while(largest.rc != null)
{
nextLargest = largest;
largest = largest.rc;
}
c.get().node = largest.node;
nextLargest.rc = largest.lc;
}
else
{
nextLargest.rc = c.get().rc;
if(p.get().lc == c.get())
p.get().lc = nextLargest;
else
p.get().rc = nextLargest;
}
}
return true;
}
}
public boolean update(String targetKey, Listing7 newListing)
{
if(delete(targetKey) == false)
return false;
else if(insert(newListing) == false)
return false;
return true;
}
public void showAll( )
{ if(root == null)
System.out.println("the structure is empty");
else
LNRoutputTraversal(root);
}
public void LNRoutputTraversal(TreeNode root)
{
if(root.lc !=null)
LNRoutputTraversal(root.lc);
System.out.println(root.node);
if(root.rc != null)
LNRoutputTraversal(root.rc);
}
public class TreeNode
{
private Listing7 node;
private TreeNode lc;
private TreeNode rc;
public TreeNode()
{
}
}
private boolean findNode(String targetKey, TreeNodeWrapper parent,TreeNodeWrapper child)
{
parent.set(root);
child.set(root);
if(root == null)
return true;
while(child.get( ) != null)
{
if(child.get( ).node.compareTo(targetKey) == 0)
return true;
else
{
parent.set(child.get( ));
if(targetKey.compareTo(child.get( ).node.getKey( )) < 0)
child.set(child.get( ).lc);
else
child.set(child.get( ).rc);
}
}
return false;
}
public class TreeNodeWrapper
{
TreeNode treeRef = null;
public TreeNodeWrapper()
{
}
public TreeNode get()
{
return treeRef;
}
public void set(TreeNode t)
{
treeRef = t;
}
}
}
Listing:
public class Listing7
{
private String name;
private String number;
private String gpa;
public Listing7(String name, String number, String gpa)
{
this.name = name;
this.number = number;
this.gpa = gpa;
}
public String toString()
{
return("Name is " + name + " Number is " + number + " GPA is " + gpa );
}
public Listing7 deepCopy()
{
Listing7 clone = new Listing7(name, number, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return (number.compareTo(targetKey));
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNumber()
{
return number;
}
public void setNumber(String number)
{
this.number = number;
}
public String getGPA()
{
return gpa;
}
public void setGPA(String gpa)
{
this.gpa = gpa;
}
public String getKey()
{
return name;
}
}
Main:
public class Main
{
public static void main(String[] args)
{
int n1;
int n2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the maximun size of the data set: ");
n1 = scan.nextInt();
System.out.println("Please enter the number of students to be entered now: ");
n2 = scan.nextInt();
while(n2 > n1)
{
System.out.println("Number of students to be inserted exceeds the number of students allowed.");
System.out.println("Please enter the number of students to be entered now: ");
n2 = scan.nextInt();
}
BinaryTree bt = new BinaryTree();
for(int i = 0; i < n2; i++)
{
System.out.println("Please enter the information for student " + (i+1)+ ": ");
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter the name: ");
String name = userInput.nextLine();
System.out.println("Please enter the number: ");
String number = userInput.nextLine();
System.out.println("Please enter a GPA:");
String gpa = userInput.nextLine();
Listing7 listing7 = new Listing7(name, number, gpa);
bt.insert(listing7);
}
int option;
String name;
while(true)
{
System.out.println("Select 1 to insert a new student's information");
System.out.println("Select 2 to fetch and output a student's information");
System.out.println("Select 3 to delete a student's information");
System.out.println("Select 4 to update a new student's information");
System.out.println("Select 5 to output all student's information");
System.out.println("Select 6 to exit the program");
System.out.println("Please select an option: ");
option = scan.nextInt();
scan.nextLine();
switch(option)
{
case 1:
if(n2 == n1)
{
System.out.println("Data set full. Can not add another student");
break;
}
System.out.println("Please enter the information for the new student: ");
System.out.println("Please enter the name to be added: ");
name = scan.nextLine();
System.out.println("Please enter the number: ");
String number = scan.nextLine().trim();
System.out.println("Please enter a GPA: ");
String gpa = scan.nextLine();
Listing7 newListing = new Listing7(name, number, gpa);
bt.insert(newListing);
System.out.println("Operation Finished");
n2++;
break;
case 2:System.out.println("Please enter the number of the student to fetch: ");
name = scan.nextLine();
Listing7 data = bt.fetch(name.trim());
if(data != null)
{
System.out.println("Student information: " + data.toString());
}
else
{
System.out.println("Sorry there is no student for this number.");
}
System.out.println("Operation Finished");
break;
case 3:System.out.println("Please enter the name to delete: ");
name = scan.nextLine();
bt.delete(name );
System.out.println("Operation Finished");
break;
case 4:System.out.println("Please enter the name to update: ");
String targetKey = scan.nextLine();
System.out.println("Please enter an updated name: ");
name = scan.nextLine();
System.out.println("Please enter an updated number: ");
number = scan.nextLine();
System.out.println("Please enter an updated GPA: ");
gpa = scan.nextLine();
newListing = new Listing7(name, number, gpa);
bt.update(targetKey, newListing);
System.out.println("Operation Finished");
break;
case 5: bt.showAll();
break;
case 6: System.exit(0);
}
}
}
}
Explanation / Answer
package trees;
public class BinaryTree
{
TreeNode root;
public BinaryTree()
{
root = null;
}
public boolean insert(Listing7 newListing)
{
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
TreeNode n = new TreeNode();
if(n == null)
return false;
else
{
n.node = newListing.deepCopy();
n.lc = null;
n.rc = null;
if(root == null)
{
root = n;
}
else
{
findNode(newListing.getKey(), p, c );
if(newListing.getKey().compareTo(p.get().node.getKey( )) < 0)
p.get().lc = n;
else
p.get().rc = n;
}
return true;
}
}
public Listing7 fetch(String targetKey)
{
boolean found;
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
found = findNode(targetKey, p, c);
if(found == true)
return c.get().node.deepCopy();
else
return null;
}
public void showAll( )
{ if(root == null)
System.out.println("the structure is empty");
else
LNRoutputTraversal(root);
}
public void LNRoutputTraversal(TreeNode root)
{
if(root.lc !=null)
LNRoutputTraversal(root.lc);
System.out.println(root.node);
if(root.rc != null)
LNRoutputTraversal(root.rc);
}
public class TreeNode
{
private Listing7 node;
private TreeNode lc;
private TreeNode rc;
public TreeNode()
{
}
}
private boolean findNode(String targetKey, TreeNodeWrapper parent,TreeNodeWrapper child)
{
parent.set(root);
child.set(root);
if(root == null)
return true;
while(child.get( ) != null)
{
if(child.get( ).node.compareTo(targetKey) == 0)
return true;
else
{
parent.set(child.get( ));
if(targetKey.compareTo(child.get( ).node.getKey( )) < 0)
child.set(child.get( ).lc);
else
child.set(child.get( ).rc);
}
}
return false;
}
public class TreeNodeWrapper
{
TreeNode treeRef = null;
public TreeNodeWrapper()
{
}
public TreeNode get()
{
return treeRef;
}
public void set(TreeNode t)
{
treeRef = t;
}
}
}
package trees;
public class Listing7
{
private String name;
private String number;
private String gpa;
public Listing7(String name, String number, String gpa)
{
this.name = name;
this.number = number;
this.gpa = gpa;
}
public String toString()
{
return("Name is " + name + " Number is " + number + " GPA is " + gpa );
}
public Listing7 deepCopy()
{
Listing7 clone = new Listing7(name, number, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return (number.compareTo(targetKey));
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNumber()
{
return number;
}
public void setNumber(String number)
{
this.number = number;
}
public String getGPA()
{
return gpa;
}
public void setGPA(String gpa)
{
this.gpa = gpa;
}
public String getKey()
{
return name;
}
}
package trees;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n1;
int n2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the maximun size of the data set: ");
n1 = scan.nextInt();
System.out.println("Please enter the number of students to be entered now: ");
n2 = scan.nextInt();
while(n2 > n1)
{
System.out.println("Number of students to be inserted exceeds the number of students allowed.");
System.out.println("Please enter the number of students to be entered now: ");
n2 = scan.nextInt();
}
BinaryTree bt = new BinaryTree();
for(int i = 0; i < n2; i++)
{
System.out.println("Please enter the information for student " + (i+1)+ ": ");
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter the name: ");
String name = userInput.nextLine();
System.out.println("Please enter the number: ");
String number = userInput.nextLine();
System.out.println("Please enter a GPA:");
String gpa = userInput.nextLine();
Listing7 listing7 = new Listing7(name, number, gpa);
bt.insert(listing7);
}
int option;
String name;
while(true)
{
System.out.println("Select 1 to insert a new student's information");
System.out.println("Select 2 to fetch and output a student's information");
System.out.println("Select 5 to output all student's information");
System.out.println("Select 6 to exit the program");
System.out.println("Please select an option: ");
option = scan.nextInt();
scan.nextLine();
switch(option)
{
case 1:
if(n2 == n1)
{
System.out.println("Data set full. Can not add another student");
break;
}
System.out.println("Please enter the information for the new student: ");
System.out.println("Please enter the name to be added: ");
name = scan.nextLine();
System.out.println("Please enter the number: ");
String number = scan.nextLine().trim();
System.out.println("Please enter a GPA: ");
String gpa = scan.nextLine();
Listing7 newListing = new Listing7(name, number, gpa);
bt.insert(newListing);
System.out.println("Operation Finished");
n2++;
break;
case 2:System.out.println("Please enter the number of the student to fetch: ");
name = scan.nextLine();
Listing7 data = bt.fetch(name.trim());
if(data != null)
{
System.out.println("Student information: " + data.toString());
}
else
{
System.out.println("Sorry there is no student for this number.");
}
System.out.println("Operation Finished");
break;
case 5: bt.showAll();
break;
case 6: System.exit(0);
}
}
}
}
/*
Please enter the maximun size of the data set:
2
Please enter the number of students to be entered now:
2
Please enter the information for student 1:
Please enter the name:
adasda
Please enter the number:
36
Please enter a GPA:
34
Please enter the information for student 2:
Please enter the name:
hrtjf
Please enter the number:
56
Please enter a GPA:
55
Select 1 to insert a new student's information
Select 2 to fetch and output a student's information
Select 5 to output all student's information
Select 6 to exit the program
Please select an option:
2
Please enter the number of the student to fetch:
36
Student information: Name is adasda
Number is 36
GPA is 34
Operation Finished
Select 1 to insert a new student's information
Select 2 to fetch and output a student's information
Select 5 to output all student's information
Select 6 to exit the program
Please select an option:
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.