Homework Help Question & Answers

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.

0 0
Next > < Previous
ReportAnswer #1

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));
    }
}


import java.util.Scanner class BTNode f ...BTNode left, right int data; public BTNode ) left = null ; right-=-null ; ..public

class BT ..private BTNode root: ..public BT ) ..public boolean isEmpty)i ..public void insert (int data) ..private BTNode ins

public.boolean search (int val) f return search (root, val) 一»-private-boolean . search (BTNoder, .int .val)( if (r.getData()

Output :

Command Prom Enter integer element to insert: 10 In order 1 Do you want to continue (Type y or n) Enter integer element to in

Add Homework Help Answer
Add Answer of:
Create a recursive method that takes as input the root Node of a tree and returns true if the N v...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions