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...
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...
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 class TreeNode is defined as below: public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } public TreeNode(int x, TreeNode lChild, TreeNode rChild) { val = x; left = lChild; right = rChild; } } Suppose you only have the reference of the root, please write a method that returns a deep copy of the binary tree. Your method returns the root of the deep copy. Your algorithm should...
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; ...
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
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...
Implement downHeap(Node *n) for a min heap implemented as an ordinary binary tree with an integer member variable "value" and left and right child pointers. Any node might have only a left child, only a right child, both, or neither. The starter code below defines a class called "Node" that has two child pointers ("left" , "right") and an integer "value" member variable. There is also a constructor Node(int val) that initializes the children to nullptr and the node's value...