Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a recursive method isBinaryTree() that takes a Node as an argument and returns true if the subtree count field N is consistent in the

Write a recursive method isBinaryTree() that takes a Node as an argument and returns true if the subtree count field N is consistent in the data structure rooted at that node, false otherwise.

This is my code.

image text in transcribed

This is the test. My code failed at the highlighted code.

image text in transcribed

This is the additional code that showed the resource code of updateN()

image text in transcribed

How do do this problem?

public class Node , Value> Key key; // key Value val; // associated value NodeKey, Value> left, right; // links to subtrees static int lastvalue = Integer.MIN.VALUE; in n; // # nodes in subtree rooted here public Node (Key key, Value val, int n) this.keykey; this.val-val; k 3 @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 boolean isBinaryTree(Node node) { return isBST (node, null, null); public static , Value boolean isBST (Node node, Key low, Key high) if (nodenul1) ( return true; if (lownull && low.compareTo(node.key) ) return false if (high != null && high . compareTo(node . key) 0) { return Talse return isBST (node.left, low, node.key) && isBST(node.right, node.key, high); public class Node , Value> Key key; // key Value val; // associated value NodeKey, Value> left, right; // links to subtrees static int lastvalue = Integer.MIN.VALUE; in n; // # nodes in subtree rooted here public Node (Key key, Value val, int n) this.keykey; this.val-val; k 3 @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 boolean isBinaryTree(Node node) { return isBST (node, null, null); public static , Value boolean isBST (Node node, Key low, Key high) if (nodenul1) ( return true; if (lownull && low.compareTo(node.key) ) return false if (high != null && high . compareTo(node . key) 0) { return Talse return isBST (node.left, low, node.key) && isBST(node.right, node.key, high)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started