Question: In Eclipse. Node public class Node , Value> { Key key; // key Value val; // associated value Node left, right; // links to subtrees

In Eclipse.
Node
public class Node
Key key; // key
Value val; // associated value
Node
int n; // # nodes in subtree rooted here
public Node(Key key, Value val, int n) {
this.key = key;
this.val = val;
this.n = n;
}
/**
* @param node root of the sub-tree
* @return true if for each node in the sub-tree, n field value matches the number of nodes in its sub-tree, otherwise false
*/
public static
// provide your implementation here
throw new UnsupportedOperationException("Not implemented yet");
}
}
Node test.
import org.junit.Assert;
import org.junit.jupiter.api.Test;
class NodeTest {
@Test
void testIsBinaryTree_1() {
Node
Assert.assertTrue(Node.isBinaryTree(node));
}
@Test
void testIsBinaryTree_2() {
Node
node = new Node
Assert.assertFalse(Node.isBinaryTree(node));
node = new Node
Assert.assertFalse(Node.isBinaryTree(node));
node = new Node
Assert.assertTrue(Node.isBinaryTree(node));
}
@Test
void testIsBinaryTree_3() {
Character[] keys = {'S', 'E', 'X', 'A', 'R', 'C', 'H', 'M'};
Node
for (int i=0; i root = put(root, keys[i], i); } Assert.assertTrue(Node.isBinaryTree(root)); updateN(root, 'R', 2); Assert.assertFalse(Node.isBinaryTree(root)); updateN(root, 'R', 4); Assert.assertFalse(Node.isBinaryTree(root)); } @Test void testIsBinaryTree_4() { Character[] keys = {'S', 'E', 'X', 'A', 'R', 'C', 'H', 'M', 'Z', 'Y'}; Node for (int i=0; i root = put(root, keys[i], i); } Assert.assertTrue(Node.isBinaryTree(root)); updateN(root, 'S', 11); Assert.assertFalse(Node.isBinaryTree(root)); updateN(root, 'S', 12); Assert.assertFalse(Node.isBinaryTree(root)); } private static if (node == null) return new Node int cmp = key.compareTo(node.key); if (cmp node.left = put(node.left, key, val); else if (cmp > 0) node.right = put(node.right, key, val); else node.val = val; node.n = size(node.left) + size(node.right) + 1; return node; } private static if (node == null) return; int cmp = key.compareTo(node.key); if (cmp updateN(node.left, key, n); else if (cmp > 0) updateN(node.right, key, n); else node.n = n; } private static return node==null? 0: node.n; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
