Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in java. It has to use merge sort to sort. Write a program that reads N

ID: 3818438 • Letter: T

Question

This is in java. It has to use merge sort to sort.

Write a program that reads N points, specified by their 2D coordinates, and outputs any set of four or more collinear points (i.e., points on the same line). The obvious brute-force algorithm requires O(N4) time. However, there is a better algorithm that makes use of sorting and runs in O(N2 log N) time (Merge Sort).

Your program should read input from a text file called points.txt that is in the same directory as your java source and class files. The file should contain the coordinates of a set of points in the format shown below:

(2, 3) (4, 4) (-1, 2) (1, 0) (10, 10) (-1, 0) (3, 3) (-1, -1) (0, 1) (7, 5) (7, 8) (-4, 5) (9, 10)

You must use the name “points.txt” for your input file and you can hardcode the filename in your Java program. For testing we will use different sets of points in a file with the same name - “points.txt”.

The x and y coordinates of the points can be integers only.

Your program should calculate if there are any sets of four or more points that lie on the same line and output the sets. In the above example, there is a set of four points that lie on the line y = x, and, another set of five other points, that line on the line y = x+1. There is another set of four collinear points too – you could find that out.

Your program should determine these collinear point sets and output them in the format shown below. Note that in each set, points are sorted by their x-axis value.

Found these sets of four of more collinear points:

Set 1 (4 points): (-1, -1) (3, 3) (4, 4) (10, 10)

Set 2 (5 points): (-1, 0) (0, 1) (2, 3) (7, 8) (9, 10)

Explanation / Answer

import java.util.Scanner;
/* Class BTNode */
class BTNode
{
BTNode left, right;
int information;
/* Constructor */
open BTNode()
{
left = invalid;
right = invalid;
information = 0;
}
/* Constructor */
open BTNode(int n)
{
left = invalid;
right = invalid;
information = n;
}
/* Function to set left hub */
open void setLeft(BTNode n)
{
left = n;
}
/* Function to set right hub */
open void setRight(BTNode n)
{
right = n;
}
/* Function to get left hub */
open BTNode getLeft()
{
return left;
}
/* Function to get right hub */
open BTNode getRight()
{
return right;
}
/* Function to set information to hub */
open void setData(int d)
{
information = d;
}
/* Function to get information from hub */
open int getData()
{
return information;
}
}
/* Class BT */
class BT
{
private BTNode root;
/* Constructor */
open BT()
{
root = invalid;
}
/* Function to check if tree is unfilled */
open boolean isEmpty()
{
return root == invalid;
}
/* Functions to embed information */
open void insert(int information)
{
root = insert(root, information);
}
/* Function to embed information recursively */
private BTNode insert(BTNode hub, int information)
{
on the off chance that (hub == invalid)
hub = new BTNode(data);
else
{
on the off chance that (node.getRight() == invalid)
node.right = insert(node.right, information);
else
node.left = insert(node.left, information);
}
return hub;
}
/* Function to tally number of hubs */
open int countNodes()
{
return countNodes(root);
}
/* Function to tally number of hubs recursively */
private int countNodes(BTNode r)
{
on the off chance that (r == invalid)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to scan for a component */
open boolean search(int val)
{
return search(root, val);
}
/* Function to scan for a component recursively */
private boolean search(BTNode r, int val)
{
on the off chance that (r.getData() == val)
return genuine;
on the off chance that (r.getLeft() != invalid)
on the off chance that (search(r.getLeft(), val))
return genuine;
on the off chance that (r.getRight() != invalid)
on the off chance that (search(r.getRight(), val))
return genuine;
return false;
}
/* Function for inorder traversal */
open void inorder()
{
inorder(root);
}
private void inorder(BTNode r)
{
on the off chance that (r != invalid)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
open void preorder()
{
preorder(root);
}
private void preorder(BTNode r)
{
on the off chance that (r != invalid)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
open void postorder()
{
postorder(root);
}
private void postorder(BTNode r)
{
in the event that (r != invalid)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
/* Class BinaryTree */
open class BinaryTree
{
open static void main(String[] args)
{
Scanner filter = new Scanner(System.in);
/* Creating object of BT */
Bt = new BT();
/* Perform tree operations */
System.out.println("Binary Tree Test ");
scorch ch;
do
{
System.out.println(" Binary Tree Operations ");
System.out.println("1. embed ");
System.out.println("2. seek");
System.out.println("3. number hubs");
System.out.println("4. check discharge");
int decision = scan.nextInt();
switch (decision)
{
case 1 :
System.out.println("Enter whole number component to embed");
bt.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter whole number component to look");
System.out.println("Search result : "+ bt.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ bt.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ bt.isEmpty());
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display tree */
System.out.print(" Post arrange : ");
bt.postorder();
System.out.print(" Pre arrange : ");
bt.preorder();
System.out.print(" In arrange : ");
bt.inorder();
System.out.println(" Do you need to proceed with (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote