Java Code Netbeans / Methods + Outputs (BinarySearchTree WITH BSTNode) -A method
ID: 3838928 • Letter: J
Question
Java Code Netbeans / Methods + Outputs
(BinarySearchTree WITH BSTNode)
-A method to count how many nodes containing a given value V
-A method to count the number of nodes that contain even numbers in a BST
-A method to count the number of nodes with 2 children (child may be a single node or a sub-tree)
-A method to print a BST sorted in reverse order
-A method to print a BST in breadth first order
-A method reconstruct a BST given its preorder traversal
-A method to check if two BSTs are identical
-A method mirror() to create a mirror image of BST
-A method to check if a BST T1 is a mirror of T2
-A method to find the common elements between two BSTs, and insert them in 3rd BST
Explanation / Answer
public class BSTree {
Node<Integer> head;
public int count(int value) {
return count(head, value);
}
private int count(Node<Integer> head, int value){
if (head == null)
return 0;
int count = 0;
if (value == head.data)
count = 1;
return count + count(head.left, value) + count(head.right, value);
}
public int countEven() {
return countEven(head);
}
private int countEven(Node<Integer> head){
if (head == null)
return 0;
return (head.data%2 == 0? 1:0) + countEven(head.left) + countEven(head.right);
}
public int countTwoChild() {
return countTwoChild(head);
}
private int countTwoChild(Node<Integer> head){
if (head == null)
return 0;
int count = 1;
if (head.left == null || head.right == null)
count = 0;
return count + countTwoChild(head.left) + countTwoChild(head.right);
}
public void printReverse() {
printReverse(head);
}
private void printReverse(Node<Integer> head){
if ( head == null)
return;
printReverse(head.right);
System.out.println(head.data);
printReverse(head.left);
}
}
I have implemented 4 of the above method, since in Chegg we are allowed only to answer 4 sub questions. I hope you understand. Let me know if you have any querry on this
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.