Question

In Java: Given the following binary tree class, write a recursive method called size() which will...

In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node:

class myBinaryTree{
int myValue;
myBinaryTree left;
myBinaryTree right;

myBinaryTree(int inValue) {myValue = inValue;}

public int size(){
<CODE WRITTEN HERE>
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class myBinaryTree {
    int myValue;
    myBinaryTree left;
    myBinaryTree right;

    myBinaryTree(int inValue) {
        myValue = inValue;
    }

    public int size() {
        int count = 1;
        if (left != null) count += left.size();
        if (right != null) count += right.size();
        return count;
    }
}
Add a comment
Know the answer?
Add Answer to:
In Java: Given the following binary tree class, write a recursive method called size() which will...
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
  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • answer in java Given a Binary Search Tree containing integer values, write a method to print...

    answer in java Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Complete in Java : Question 2: Write a program that can convert a sorted array into...

    Complete in Java : Question 2: Write a program that can convert a sorted array into a balanced binary search tree. A binary search tree is a balanced binary search tree if the size of the left and right subtree at each node differs by at most one. The size of the left subtree is defined as the number of nodes in the left subtree. The size of the right subtree is defined as the number of nodes in the...

  • Java - Data Structures The method removeLeftmost() in a BinaryNode class is given below. Write this...

    Java - Data Structures The method removeLeftmost() in a BinaryNode class is given below. Write this method in BinaryTree class. → *BinaryNode class private class BinaryNode<T> {    private T data;    private BinaryNode<T> leftChild;    private BinaryNode<T> rightChild;    ...    // constructors    // get and set methods    // other methods    /** removeLeftmost() - removes the leftmost node of the subtree rooted at this node */    public BinaryNode<T> removeLeftmost()    {       if(leftChild == null)          return rightChild       else          return leftChild.removeLeftmost();    } } ****************************************************************************************************************** --> BinaryTree class...

  • in java please thanks! Given a pointer to a binary tree write a routine which will...

    in java please thanks! Given a pointer to a binary tree write a routine which will traverse the tree and return the number of nodes in the tree whose information field which is an integer is between 59 and 112 In addition return the value of the node with the largest integer and as well return a count of the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and...

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • java please IV. Given a pointer to a binary tree write a routine which will traverse...

    java please IV. Given a pointer to a binary tree write a routine which will traverse the tree and return the 59 and 112 number of nodes in the tree whose information field which is an integer is between In addition return the value of the node with the largest integer and as well return a count or the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and left...

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