Consider the following BNode class:
class BNode {
int val;
BNode left, right;
...
boolean isBST(BNode u){...}
boolean isBST(int min, BNode u, int max){...}
}
Note that our BNodes do not have parent pointers! It has a method isBST(u) that returns true iff the subtree rooted at u is a BST. Please write the code for isBST(u) and its helper method isBST(min, u, max) which will return false if the keys in the subtree at u do not lie in the range (min, max). The helper method must be recursive.
int isBST(BNode root)
{
// if the tree is empty
if (root == null)
return true;
// if the current node disobeys the BST property
// use the isBST(min, u,max) function to check if each key in left subtree
// is smaller than root node
if (root.left != null && this.isBST( Integer.MIN_VALUE , root.left , root.value) )
return false;
// if the current node disobeys the BST property
// use the isBST(min, u,max) function to check if each key in right subtree
// is greater than root node
if (root.right != null && this.isBST( root.value , root.right , Integer.MAX_VALUE))
return false;
// recursively check if the right and left subtree also are in BST
if( isBST(root.left) != true || isBST(root.right) != true )
return false;
// if the tree is BST
return true;
}
boolean isBST(int min, BNode u, int max)
{
if( u == null )
return true;
// if the current node is outside the range
if( u.value < min || u.value > max )
return false;
// recursively check if the right and left subtree also are in BST
if( isBST(min, root.left, max) != true || isBST(min, root.right, max) != true )
return false;
return true;
}
Consider the following BNode class: class BNode { int val; BNode left, right; ... boolean isBST(BNode...
The code is in JAVA
public class CheckBST {
//method to implement
public static boolean isValidBST(TreeNode root) {
}
public static void main(String[] args) {
TreeNode a = new TreeNode(1);
TreeNode b = new TreeNode(2);
TreeNode c = new TreeNode(3);
a.left = b;
a.right = c;
System.out.println(isValidBST(a));
TreeNode d = new TreeNode(2);
TreeNode e = new TreeNode(1);
TreeNode f = new TreeNode(3);
d.left = e;
d.right = f;
System.out.println(isValidBST(d));
}
}
TreeNode.java
class TreeNode {
int val;
TreeNode left;
TreeNode...
Q4 Programming 10 Points public class BSTNode<t extends Comparable<T>> { public T data; public BSTNode<T> left, right; public BSTNode (T data, BSTNode<T> left, BSTNode<T> right) { this.data = data; this.left = left; this.right - right; In this section, you will write a method that checks the validity of a BST. You can avoid requiring such additional memory but directly using the definition of BST. Specifically, you can pass the minimum and maximum values down to the recursive calls to help...
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;...
In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node: class myBinaryTree{ int myValue; myBinaryTree left; myBinaryTree right; myBinaryTree(int inValue) {myValue = inValue;} public int size(){ <CODE WRITTEN HERE> } }
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; ...
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...
Public class TreeNode { TreeNode left, right; Int val; } Given a binary tree, print val in level order. Input: 1 2 3 4 5 6 7 Out: 1234567
PROMPT:
Consider a binary tree (not necessarily a binary search tree)
with node structure.
QUESTION:
Prove that findMax works by mathematical induction.
struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
Consider the partial implementation of a Binary Search Tree
(BST) class. For simplicity, each Node stores only the key. Add a
public member function to class BST that returns the largest
absolute value in the tree.
The language is C++
Want the height
#4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...