use these methods : this A recursive method that returns the level of the node c
ID: 3919612 • Letter: U
Question
use these methods :
this A recursive method that returns the level of the node containing el or -1 if el does not exist
in the tree. Example: Level of node 12 in the following tree is 3.
----------------------------------------------------------------------------------------------------------------------------
this
A recursive method that returns true if the tree referenced by p is a decision tree.
Hint: A decision tree is one in which each node has its children as either both empty or both
non-empty.
-----------------------------------------------------------------------------------------------------------------------------------
this A recursive method that returns the length of the path from the root of the tree referenced
by p to the node containing el. Example: path length for node 12 in the following tree is 2.
------------------------------------------------------------------------------------------------------------------------------
add new case in this class to test these methods :
import java.util.Scanner;
import java.util.*;
public class TestIntegerBST {
public static void main (String[] args){
BST<Integer> tree = new BST<Integer>();
int option, target;
Scanner reader = new Scanner(System.in);
do {
System.out.println(" ***************************");
System.out.println("* Testing Binary Search Tree *");
System.out.println("*************************** ");
System.out.println("1. Insert an element");
System.out.println("2. Search for an element");
System.out.println("3. Delete an element");
System.out.println("4. Print in Breadth-First-Order");
System.out.println("5. Print in Pre-Order");
System.out.println("6. Print in In-Order");
System.out.println("7. Print in Post-Order");
System.out.println("8. Print sum of the elements");
System.out.println("9. Quit");
System.out.print(" Select an Option [1...9] : ");
option = reader.nextInt();
switch (option) {
case 1 : System.out.print("Enter the element to insert: ");
tree.insert(reader.nextInt());
break;
case 2 : System.out.print("Enter the element to search for: ");
target = reader.nextInt();
Integer result = tree.search(target);
if (result != null)
System.out.println("Element, "+result+ " was found in the tree");
else
System.out.println("Sorry, the element was not found");
break;
case 3 : System.out.print("Enter the element delete: ");
tree.delete(reader.nextInt());
break;
case 4 : tree.breadthFirst();
break;
case 5 : tree.preorder();
break;
case 6 : tree.inorder();
break;
case 7 : tree.postorder();
break;
case 8 : System.out.print("Sum of the elements in the tree is: "+sum(tree));
break;
} //end of switch
} while (option != 9);
} //end of main
public static int sum(BST<Integer> tree) {
int sum = 0;
for (int n : tree)
sum += n;
return sum;
}
}
(i) protected int Level(T el, BSTNode T> p) A recursive method that returns the level of the node containing el or -1 if el does not exist in the tree. Example: Level of node 12 in the following tree is 3. (ii) protected boolean isDecision Tree(BSTNode A recursive method that returns true if the tree referenced by p is a decision tree. Hint: A decision tree is one in which each node has its children as either both empty or both non-empty (iv) protected int getPathLength(T el, BSTNode A recursive method that returns the length of the path from the root of the tree referenced by p to the node containing el. Example: path length for node 12 in the following tree is 2. Note: For each of the recursive methods above, you need to have a public overloaded version of the method (without parameter p) that calls the recursive version with root as the initial value of p. It is this public method you will call in the TestlntegerBST class. 1 0 1 5 2 1 2 20Explanation / Answer
import java.util.Scanner;
import java.util.*;
public class TestIntegerBST {
public static void main (String[] args){
BST<Integer> tree = new BST<Integer>();
int option, target;
Scanner reader = new Scanner(System.in);
do {
System.out.println(" ***************************");
System.out.println("* Testing Binary Search Tree *");
System.out.println("*************************** ");
System.out.println("1. Insert an element");
System.out.println("2. Search for an element");
System.out.println("3. Delete an element");
System.out.println("4. Print in Breadth-First-Order");
System.out.println("5. Print in Pre-Order");
System.out.println("6. Print in In-Order");
System.out.println("7. Print in Post-Order");
System.out.println("8. Print sum of the elements");
System.out.println("9. Print level");
System.out.println("10. Is it a decision tree");
System.out.println("11. Print path length");
System.out.println("12. Quit");
System.out.print(" Select an Option [1...12] : ");
option = reader.nextInt();
switch (option) {
case 1 : System.out.print("Enter the element to insert: ");
tree.insert(reader.nextInt());
break;
case 2 : System.out.print("Enter the element to search for: ");
target = reader.nextInt();
Integer result = tree.search(target);
if (result != null)
System.out.println("Element, "+result+ " was found in the tree");
else
System.out.println("Sorry, the element was not found");
break;
case 3 : System.out.print("Enter the element delete: ");
tree.delete(reader.nextInt());
break;
case 4 : tree.breadthFirst();
break;
case 5 : tree.preorder();
break;
case 6 : tree.inorder();
break;
case 7 : tree.postorder();
break;
case 8 : System.out.print("Sum of the elements in the tree is: "+sum(tree));
break;
case 9 :
System.out.print("Enter the node value:");
int a = Integer.parseInt(reader.nextLine());
System.out.print("The level is: "+ tree.level(a));
break;
case 10 :
if (tree.isDecisionTree())
System.out.println("It is a decision tree");
else
System.out.println("It is not a decision tree");
break;
case 11 :
System.out.print("Enter the node value:");
int a = Integer.parseInt(reader.nextLine());
System.out.print("The path length is: "+ tree.getPathLength(a));
break;
} //end of switch
} while (option != 12);
} //end of main
public static int sum(BST<Integer> tree) {
int sum = 0;
for (int n : tree)
sum += n;
return sum;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.