Question

PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help.

import java.util.LinkedList;

public class TwoDTree {

private TwoDTreeNode root;
private int size;

public TwoDTree() {
   clear();
}

/**
* Returns true if a point is already in the tree.
* Returns false otherwise.
*
* The traversal remains the same. Start at the root, if the tree
* is not empty, and compare the x-coordinates of the point passed
* in and the root.
*
* If the x-coordinate of the pointToSearch is less
* than the x-coordinate of the root then go left if possible.
*
* If the x-coordinate of the pointToSearch is greater than or equal to
* the x-coordinate of the root then go right if possible.
*
* Alternate which coordinate is checked at each level by first
* checking x then y then x then y and so on.
*
* If a null is ever reached or the tree is empty a false should be the result.
* If the point that is passed in is found in the tree, in other words
* if N is a node in the tree and N.point.equals(pointToSearch) then return true.
*
* @param pointToSearch The point that is to be determined if it exists in the tree.
* @return True if a point with the same X and Y values as pointToSearch exists
*        False otherwise.
*/
public boolean contains(Point2D pointToSearch) {
   return false;
}

/**
* Adds a point to the 2D using the following traversal scheme.
*
* If the tree is empty, set root to a new tree node that contains newPoint.
* Otherwise, set a tree node, current, to root.
*        If newPoint.x < current.point.x
*            If current.left == null
*                Add a new TreeNode with newPoint to current.left
*            else
*                Set current to current.left
*        If newPoint.x >= current.point.x
*            Check that current.point equals newPoint
*                if it does then raise the IllegalArgumentException
*                Else do nothing
*            if current.right == null
*                Add a new TreeNode with newPoint to current.right
*            else
*                Set current to current.right
*
* This should alternate between x then y then x then y. If the element is added, remember
* to increment size.
*
* @param newPoint Point to be added to the tree.
*/
public void add(Point2D newPoint) {
  
}

/********************************/
public void clear() {
   root = null;
   size = 0;
}

public int getSize() {
   return size;
}

public boolean isEmpty() {
   return size == 0;
}

@Override
public String toString() {
   StringBuilder sb = new StringBuilder();
   buildLevelOrderString(sb);
   return sb.toString();
}

private void buildLevelOrderString(StringBuilder sb) {
   if (!isEmpty()) {
   // Will be used as a queue for level order traversal
   LinkedList<TwoDTreeNode> q = new LinkedList<>();
   q.addLast(root);
   TwoDTreeNode temp;
   while (!q.isEmpty()) {
       temp = q.removeFirst();
       if (temp.left != null) {
       q.addLast(temp.left);
       }

       if (temp.right != null) {
       q.addLast(temp.right);
       }

       sb.append(temp.point);

       if (!q.isEmpty()) {
       sb.append(", ");
       }
   }
   }
}

private class TwoDTreeNode {
   private TwoDTreeNode left;
   private TwoDTreeNode right;
   private Point2D point;

   public TwoDTreeNode() {
   }

   public TwoDTreeNode(Point2D p) {
   this.point = p;
   }

   public TwoDTreeNode getLeft() {
   return left;
   }

   public TwoDTreeNode getRight() {
   return right;
   }

   public void setLeft(TwoDTreeNode left) {
   this.left = left;
   }

   public void setRight(TwoDTreeNode right) {
   this.right = right;
   }

   public int getX() {
   return point.x;
   }

   public int getY() {
   return point.y;
   }
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java implementation to check if given Binary tree

//is a BST or not

  

/* Class containing left and right child of current

node and key value*/

class Node

{

    int data;

    Node left, right;

  

    public Node(int item)

    {

        data = item;

        left = right = null;

    }

}

  

public class BinaryTree

{

    //Root of the Binary Tree

    Node root;

  

    /* can give min and max value according to your code or

    can write a function to find min and max value of tree. */

  

    /* returns true if given search tree is binary

     search tree (efficient version) */

    boolean isBST() {

        return isBSTUtil(root, Integer.MIN_VALUE,

                               Integer.MAX_VALUE);

    }

  

    /* Returns true if the given tree is a BST and its

      values are >= min and <= max. */

    boolean isBSTUtil(Node node, int min, int max)

    {

        /* an empty tree is BST */

        if (node == null)

            return true;

  

        /* false if this node violates the min/max constraints */

        if (node.data < min || node.data > max)

            return false;

  

        /* otherwise check the subtrees recursively

        tightening the min/max constraints */

        // Allow only distinct values

        return (isBSTUtil(node.left, min, node.data-1) &&

                isBSTUtil(node.right, node.data+1, max));

    }

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...
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
  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the...

    Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...

  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • i need help to modify this Tree class to include a method leavesCount that returns the...

    i need help to modify this Tree class to include a method leavesCount that returns the number of leaves in a binary tree. (NOTE: Complete leavesCount and leavesCountHelper methods in the Tree class). I already have the test class. java class TreeNode< T extends Comparable< T > > { TreeNode< T > leftNode; T data; TreeNode< T > rightNode; public TreeNode( T nodeData ) { data = nodeData; leftNode = rightNode = null; } public void insert( T insertValue )...

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

  • Please help me with me. I did the first part to write the operations but in...

    Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...

  • JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

    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;            ...

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