Implement the code using Java. Binary Tree Programming problem: Create and test
ID: 3903760 • Letter: I
Question
Implement the code using Java.
Binary Tree Programming problem:
Create and test a Binary Search Tree class that stores names in String format. This class will closely parallel the IntTreeSearch code discussed in class and in Canvas.
These are the classes you are going to create (and their IntTreeSearch equivalents in parentheses):
NameTreeNode (IntTreeNode): Stores data for the node, a String, and the left and right subtree nodes. Add a one - argument and three - argument constructor.
NameTreeSearch (IntTreeSearch): Implement these methods:
public NameTreeSearch() {
}
public void add(String aName) {
}
private NameTreeNode add(NameTreeNode root, String aName) {
// Hint: Think Comparable
}
public void print() {
// Inorder traversal
}
private void print(NameTreeNode root) {
}
public void printSideways() {
}
private void printSideways(NameTreeNode root, int indent) {
}
NameTreeTest (IntTreeTest): Provide a test program that does these things:
1)Declares an array of names
2)Initialize array to these values: {"Beth", "Sue", "Dave", "Pat", "Mike", "Dawn", "Cindi", "Gina"}
3)Iterate the array and add the names to your NameTreeSearchby calling add.
4)Produce this exact output:
Sue
Pat
Mike
|Gina
Dawn
Dave
Cindi
Beth
Beth Cindi Dave Dawn Gina Mike Pat Sue
The first uses the printSideways method and the second uses the print() method that uses Inorder traversal (and proves the binary tree is sorted).
Explanation / Answer
here is your answer : ------------>>>>>>>>>>
class NameTreeNode{
private String data;
private NameTreeNode left;
private NameTreeNode right;
public NameTreeNode(String data){
this.data = data;
left = null;
right = null;
}
public NameTreeNode(String data,NameTreeNode l,NameTreeNode r){
this.data = data;
left = l;
right = r;
}
public String getData(){
return data;
}
public NameTreeNode getRight(){
return right;
}
public NameTreeNode getLeft(){
return left;
}
public void setRight(NameTreeNode r){
right = r;
}
public void setLeft(NameTreeNode l){
left = l;
}
}
public class NameTreeSearch{
private NameTreeNode root;
public NameTreeSearch() {
root = null;
}
public void add(String aName) {
root = add(root,aName);
}
private NameTreeNode add(NameTreeNode root, String aName) {
// Hint: Think Comparable
if(root == null){
root = new NameTreeNode(aName);
return root;
}
NameTreeNode temp = null;
if(root.getData().compareTo(aName) > 0){
temp = add(root.getLeft(),aName);
root.setLeft(temp);
return root;
}
if(root.getData().compareTo(aName) < 0){
temp = add(root.getRight(),aName);
root.setRight(temp);
return root;
}
return root;
}
public void print() {
// Inorder traversal
print(this.root);
}
private void print(NameTreeNode root) {
if(root == null){
return;
}
print(root.getLeft());
System.out.print(" "+root.getData());
print(root.getRight());
}
public void printSideways() {
System.out.print(" ");
printSideways(this.root,0);
}
private void printSideways(NameTreeNode root, int indent) {
if(root == null){
return;
}
if(root.getRight() != null){
printSideways(root.getRight(),indent+1);
}
for(int i = 0;i<indent;i++)
System.out.print(" ");
System.out.print(root.getData()+" ");
printSideways(root.getLeft(),indent+1);
}
}
public class NameTreeTest{
public static void main(String[] args) {
String[] arr = {"Beth", "Sue", "Dave", "Pat", "Mike", "Dawn", "Cindi", "Gina"};
NameTreeSearch ns = new NameTreeSearch();
for(int i = 0;i<arr.length;i++){
ns.add(arr[i]);
}
ns.printSideways();
System.out.print(" ");
ns.print();
}
}