Question

Java- With Binary Tree we want to count the nodes after a certain depth. Add a...

Java-

With Binary Tree we want to count the nodes after a certain depth.

Add a recursive method countNodesAtDepth to your Driver class, which takes two parameters: an Integer node n and an integer d. This method should return the number of nodes at depth d in the subtree rooted at n.

Hint: if a node is at depth d in the subtree rooted at n, what depth is it at in the subtree rooted at n.left or n.right? What are the possible base cases for your recursive method?

Homework Answers

Answer #1

/*If you have any query do comment in the comment section else like the solution*/

//Below code will return number of nodes at particular depth d, if you want to count number of nodes at depth d and after that, then just change d==0 to d<=0

int countNodesAtDepth(node n, int d) {
   if(n == null) return 0;
   if(n.left == null && n.right == null) {
       if(d == 0) {
           return 1;
       }
   }
   if(d == 0) {
       return 1 + countNodesAtDepth(n.left, d-1) + countNodesAtDepth(n.right, d-1);
   }
   return countNodesAtDepth(n.left, d-1) + countNodesAtDepth(n.right, d-1);
}

Know the answer?
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
Similar Questions
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Consider a binary search tree where each tree node v has a field v.sum which stores...
Consider a binary search tree where each tree node v has a field v.sum which stores the sum of all the keys in the subtree rooted at v. We wish to add an operation SumLE(K) to this binary search tree which returns the sum of all the keys in the tree whose values are less than or equal to K. (a) Describe an algorithm, SumLE(K), which returns the sum of all the keys in the tree whose values are less...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: 1. Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Provide the following: A list of two functional requirements. A list of two nonfunctional requirements (must...
    asked 8 minutes ago
  • Select a restaurant with which you are familiar and determine the most likely storage management challenge...
    asked 13 minutes ago
  • Proved 3 examples of problems that have surfaced in health and social services from the arrival...
    asked 13 minutes ago
  • A soccer goalie allows a goal on average of 2.3 times per game. Make an estimate...
    asked 13 minutes ago
  • What are some of the similarities and differences associated with caregiver/parental support across these classifications?
    asked 20 minutes ago
  • It is believed that the mean height of middle school students who play basketball on the...
    asked 20 minutes ago
  • Python Indicate whether each statement will evaluate to True or False after running these initializations: a...
    asked 1 hour ago
  • Briefly explain with two examples how traditional leaders in communities can employ the Ubuntu concept for...
    asked 1 hour ago
  • Question 1 Which are two of the four key aspects of hegemony? Group of answer choices...
    asked 2 hours ago
  • A consumer’s preferences are represented by the following utility function: u(x, y) = lnx + 1/2...
    asked 2 hours ago
  • Kingsford Furnishings Company manufactures designer furniture. Kingsford Furnishings uses a job order cost system. Balances on...
    asked 2 hours ago
  • 300 words:      (b) Do you think that it is appropriate for firms like Black Diamond...
    asked 3 hours ago