# Create a recursive method that takes as input the root Node of a tree and returns true if the N v...

Create a recursive method that takes as input the root Node of a tree and returns true if the N variable of each node within that tree has the correct value (i.e., all nodes are labeled with the proper size of the subtree they define), and false otherwise.

Program :

import java.util.Scanner;
class BTNode{
BTNode left, right;
int data;
public BTNode(){
left = null;
right = null;
data = 0;
}
public BTNode(int n){
left = null;
right = null;
data = n;
}
public void setLeft(BTNode n){
left = n;
}
public void setRight(BTNode n){
right = n;
}
public BTNode getLeft(){
return left;
}
public BTNode getRight(){
return right;
}
public void setData(int d){
data = d;
}
public int getData()

{

return data;

}

}
class BT{
private BTNode root;
public BT(){
root = null;
}
public boolean isEmpty(){
return root == null;
}
public void insert(int data){
root = insert(root, data);
}
private BTNode insert(BTNode node, int data){
if (node == null)
node = new BTNode(data);
else{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);
}
return node;
}
public void inorder(){
inorder(root);
}
private void inorder(BTNode r){
if (r != null){
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
public boolean search(int val){
return search(root, val);
}
private boolean search(BTNode r, int val){
if (r.getData() == val)
return true;
if (r.getLeft() != null)
if (search(r.getLeft(), val))
return true;
if (r.getRight() != null)
if (search(r.getRight(), val))
return true;
return false;

}
}

public class BinaryTree{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
BT bt = new BT();
char ch;
System.out.println("#############Binary Tree ###################");
do{
System.out.println("Enter integer element to insert :");
bt.insert(scan.nextInt());
System.out.print(" In order : ");
bt.inorder();
System.out.print(" ");
System.out.println(" Do you want to continue (Type y or n) : ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
System.out.println("Enter a node value to check is correct or not in the tree :");
int a = scan.nextInt();
System.out.println(bt.search(a));
}
}

Output :

##### Add Answer of: Create a recursive method that takes as input the root Node of a tree and returns true if the N v...
More Homework Help Questions Additional questions in this topic.