Question

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
Add a comment Improve this question Transcribed image text
Answer #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 a comment
Know the answer?
Add Answer to:
Create a recursive method that takes as input the root Node of a tree and returns true if the N v...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (true/false) All nodes in the right subtree of a node must be smaller in value than...

    (true/false) All nodes in the right subtree of a node must be smaller in value than their parent (true/false) Each node in a binary search tree may contain zero, one, or two children nodes. (true/false) It is possible to recursively process a binary search tree to print all the data in a binary search tree in sorted order. (true/false) The value of a parent must be less than all its children. (true/false) All nodes in the right subtree of a...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Write a function int levelSearch(Node* root, int key) that takes as input the root node of...

    Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public:         ...

  • Let T be a proper binary tree. Given a node v ∈ T, the imbalance of...

    Let T be a proper binary tree. Given a node v ∈ T, the imbalance of v, denoted imbalance(v), is defined as the difference, in absolute value, between the number of leaves of the left subtree of v and the number of leaves of the right subtree of v. (If v is a leaf, then imbalance(v) is defined to be 0.) Define imbalance(T) = maxv∈T imbalance(v). (a) Provide an upper bound to the imbalance of a proper binary tree with...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • MATLAB question! PLEASE HELP Create a function that returns true if the input is a scalar...

    MATLAB question! PLEASE HELP Create a function that returns true if the input is a scalar value (i.e. of size 1x1) of class type double and false otherwise. • Function Name: isScalar • Input: single input which may be any data class • Output: the output is true or false • Useful functions: size(), class(), strncmp()

  • Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if...

    Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true

  • QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent...

    QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent parent sibling QUESTION 2 In a tree, the ____ is a measure of the distance from a node to the root. count degree branch height level QUESTION 3 Which of the following is not a characteristic of a binary search tree? Each node has zero, one, or two successors. The preorder traversal processes the node first, then the left subtree, and then the right...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT