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");
}
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.
JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth * * Returns the number of nodes with...
Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...
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 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 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 * * 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 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 binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...
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 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;...
Writing traversal methods You will create two files in the csc403 package: one called A3SimplerBST.java and the other TestA3.java. Details on these programs appears below. Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3SimplerBST. Add two public methods: one named twoChildrenCount that takes no parameters and that, when called, traverses every node of the tree to count how many nodes have two children. one named sameValueCount that takes a paremeter of generic...