Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.*; public class PoD { //============================================================================= /** * Returns true if the binary tree is a binary search tree (BST) * * @param Tree
import java.util.*; public class PoD { //============================================================================= /** * Returns true if the binary tree is a binary search tree (BST) * * @param Tree BinaryTree of interest * @return boolean (true if BST) */ public static boolean isBST(BinaryTree tree) { /* Check if the binary tree is a BST: For all nodes, all left descendants are less than the value stored in the node and all right descendants have greater data values. d / \ (Today you are going to whether or not a binary tree is a binary search tree. Binary search tree Recall that a binary search tree is a binary tree in which for all nodes, all left descendants are less than the value stored in the node and all right descendants have greater data values. Details Download the ZIP files named FilesNeeded.zip for use in Intellij.When you are happy with your solution, upload the files into the src folder and run You are going to finish off PoD.java to finish the isBST method. Input The main method (already completed) creates a binary tree and passes that binary tree to the isBST method. Processing You are going to complete the details of isBST method. Details of each method and their expected parameters are described in the Javadoc comments preceding it. The method should return true if the binary tree is a binary search tree return false if notd) */ boolean meetsCriteria; return meetsCriteria; } //============================================================================= public static void main(String[] args) { Scanner in = new Scanner(System.in); //int i = in.nextInt(); //int j = in.nextInt(); BinaryTree newTree = buildTree(in); boolean isBinarySearchTree = isBST(newTree); in.close(); if (isBinarySearchTree) { System.out.println("This is a binary search tree"); } else { System.out.println("NOT a binary search tree"); } System.out.print("END OF OUTPUT"); } /** * Reads in data to form a binary tree: * N ( L R ) * N = node data * L = Left binary tree data (possibly of same form) * R = Right binary tree data (possibly of same form) * expects all nodes until reaching null * * e.g. input: a ( b ( null null ) c ( null null ) ) * creates tree: * a * / \ * b c */ public static BinaryTree buildTree(Scanner in) { String data = in.next(); BinaryTree left = null; BinaryTree right = null; if (data.equals("null")) { return null;} if (!in.next().equals("(")) { System.out.println("BAD INPUT: (L"); } left = buildTree(in); right = buildTree(in); if (!in.next().equals(")")) { System.out.println("BAD INPUT: R)"); } return new BinaryTree(data, left, right); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started