Question

Hello, I've been working on this for a while and I can't figure the rest out....

Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help.

maxBSt,minBST,isBST, and inOrder

package lab7;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;


public class TreeExercise {
   /*
   * Construct BST from preorder traversal
   */
   public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end)
   {
              
       if(start > end) return null;
      
       Node<Integer> root = new Node<Integer>(arr[start], null, null);
      
      
       int index = start+1;
       while(index <= end)
       {
           if (arr[index] < arr[start])
           {
               index ++;
           }
           else
           {
               break;
           }
       }
              
       root.left = consBSTfromPreOrder(arr, start + 1, index -1);
      
       root.right = consBSTfromPreOrder(arr, index, end);
      
       return root;      
   }
  
  
   /*
   * Tree level traversal
   */
   public static void levelOrder(Node<Integer> root)
   {
       Queue<Node<Integer>> que = new LinkedList<Node<Integer>>();
      
       que.offer(root);
      
       while(!que.isEmpty())
       {
          
           Node<Integer> temp = que.poll();
          
           System.out.print(temp + " ");
          
           if (temp.left != null)
               que.offer(temp.left);
          
           if(temp.right != null)
               que.offer(temp.right);
      
       }  
      
   }
  
  
   /**
   *
   * @param root - root of the given BST
   * @param ret - an ArrayList of the data following inOrder tree traversal
   */
   public static void inOrder(Node<Integer> root, ArrayList<Integer> ret)
   {
      
   }
  
  
   /**
   *
   * @param root - the root of given BST
   * @return the maximum data in the given BST or 0 if empty BST
   */
   public static int maxBST(Node<Integer> root)
   {
       //TODO: please add your code here
      
       return 0; // please remove this line after your coding
      
   }
  
   /**
   *
   * @param root - the root of given binary search tree
   * @return - the minimum data in the given BST or 0 if empty BST
   */
  
   public static int minBST(Node<Integer> root)
   {
      
       //TODO: please add your code here
      
       return 0; // please remove this line after your coding
   }
  
  
   /**
   *
   * @param root - the given root of binary tree
   * @return true if a binary search tree; otherwise false
   */
  
   public static boolean isBST(Node<Integer> root)
   {
       //TODO: please add your code here
      
       return false; // please remove this line after your coding
      
   }
  
  
  
   /* ====== Extra Credit (10%) ==============================*/
  
   /**
   *
   * @param root - the root of the given BST
   * @return data following tree level traversal
   */
   /*
   * Example: 7
   * / \
   * 5 10
   * / \ \
   * 3 6 12
   *   
   * return: 7
   * 5 10
   * 3 6 12
   */
   public static ArrayList<ArrayList<Integer>> levelOrderBST(Node<Integer> root)
   {
      
       //TODO: add your code here
       return null; //remove this line after your coding
   }
  
  
  
  
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
        
        /**
         *
         * @param root - root of the given BST
         * @param ret  - an ArrayList of the data following inOrder tree traversal
         */
        public static void inOrder(Node<Integer> root, ArrayList<Integer> ret) {
                if(root.left != null) {
                        inOrder(root.left, ret);
                }
                ret.add(root.data);
                if(root.right != null) {
                        inOrder(root.right, ret);
                }
        }

        /**
         *
         * @param root - the root of given BST
         * @return the maximum data in the given BST or 0 if empty BST
         */
        public static int maxBST(Node<Integer> root) {
                if(root == null) {
                        return 0;
                }
                while(root.right != null) {
                        root = root.right;
                }

                return root.data;
        }

        /**
         *
         * @param root - the root of given binary search tree
         * @return - the minimum data in the given BST or 0 if empty BST
         */

        public static int minBST(Node<Integer> root) {

                if(root == null) {
                        return 0;
                }
                while(root.left != null) {
                        root = root.left;
                }

                return root.data;
        }

        /**
         *
         * @param root - the given root of binary tree
         * @return true if a binary search tree; otherwise false
         */

        public static boolean isBST(Node<Integer> root) {
                if(root == null) {
                        return true;
                }
                if(root.left == null && root.right == null) {
                        return true;
                }
                
                int leftValue = root.data - 1;
                int rightValue = root.data + 1;
                
                if(root.left != null) {
                        leftValue = root.left.data;
                }
                if(root.right != null) {
                        rightValue = root.right.data;
                }
                
                return (leftValue < root.data) && (rightValue > root.data)
                                && isBST(root.left) && isBST(root.right); 

        }

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Hello, I've been working on this for a while and I can't figure the rest out....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT