Question

JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

JAVA QUESTION:

*******THE QUESTION:********

/** numberOfNodesAtDepth
   *
   * Returns the number of nodes with depth == d
   * Precondition: none
   *
   * param: d the depth to search for
   *
   * hint: use a recursive helper function
   *    
   * ToDo 4
   */
   public int numNodesAtDepth(int d) {
       return -1;
   }

**********USEFUL CODE FROM THE ASSIGNMENT:***********

public class simpleBST<Key extends Comparable<Key>, Value> {
   private Node root;             // root of BST

   static boolean verbose = true;   // set to false to suppress positive test results
   private class Node {
       private Key key;           // key
       private Value val;         // associated data
       private Node left, right; // left and right subtrees

       public Node(Key key, Value val) {
           this.key = key;
           this.val = val;
       }

       /**
       * this method is provided to facilitate testing
       */
       public void buildString(StringBuilder s) {
           s.append(left == null ? '[' : '(');
           s.append(key + "," + val);
           s.append(right == null ? ']' : ')');
           if (left != null) left.buildString(s);
           if (right != null) right.buildString(s);
       }
   }
   /**
   * This method is provided to facilitate testing
   */
   public String toString() {
       StringBuilder s = new StringBuilder();
       root.buildString(s);
       return s.toString();
   }

   /**
   * Initializes an empty symbol table.
   */
   public simpleBST() {
       root = null;
   }

   /** size
   *
   * return the size of the tree
   */
   public int size() {
       return size( root);
   }
   private int size(Node x) {
       if ( x == null) return 0;
       return 1 + size(x.left)+size(x.right);
   }

******TEST CODES:*******

    public static void testnumNodesAtDepth( String keys, int theDepth, int expected ) {

       // create and populate the table from the input string
       simpleBST<String,String> aTree = from(keys,keys);
       // do the test
       int actual = aTree.numNodesAtDepth(theDepth);

       if ( actual == expected) {// test passes
           if (verbose)
               StdOut.format("testnumNodesAtDepthD: Correct   Keys: [ %s ]   actual: %d ", keys, actual);
       }
       else
           StdOut.format("testnumNodesAtDepthD: *Error*   Keys: [ %s ]   expected: %d actual: %d ", keys, expected, actual);
   }

public static void numNodesAtDepthDTests() {
       testnumNodesAtDepth("",0, 0);         // empty tree has no nodes at any depth
       testnumNodesAtDepth("C",0, 1);
       testnumNodesAtDepth("BAC",0, 1);
       testnumNodesAtDepth("ABCDEFG",3, 1);
       testnumNodesAtDepth("DBACFEG",1, 2);
       testnumNodesAtDepth("DBACFEG",2, 4);
       testnumNodesAtDepth("DBACFEG",3, 0);
       testnumNodesAtDepth("EAIDFBHCG",4, 2);

       StdOut.println("----------- numNodesAtDepthD tests completed");
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int find_nodes(Node node, int k)

    {

        if (node == null)

            return 0;

       int l = 0, r = 0;

        if (k == 0)

        {

            return 1;

        }

        else

        {

            l = find_nodes(node.left, k - 1);

            r = find_nodes(node.right, k - 1);

        }

        return (l+r);

    }

public int numNodesAtDepth(int d) {
       return find_nodes(root, d);
   }

There may be some compilation error but logic is correct.

Add a comment
Know the answer?
Add Answer to:
JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...
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
  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values...

    package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...

  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

  • 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;...

  • Removing Nodes from a Binary Tree in Java This section requires you to complete the following...

    Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....

  • Add printRang method to BST.java that, given a low key value, and high key value, print...

    Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys. (Both low key and high key do not have to be a key on the list). BST.java import java.lang.Comparable; /** Binary Search Tree implementation for Dictionary ADT */ class BST<Key extends Comparable<? super Key>, E> implements Dictionary<Key, E> { private BSTNode<Key,E> root; // Root of the BST int nodecount; //...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

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
Active Questions
ADVERTISEMENT