JAVA JAVA JAVA OUTPUT SHOWN PLEASE PLEASE SUBMIT BEFORE MIDNIGHT EST Write a pro
ID: 3681709 • Letter: J
Question
JAVA JAVA JAVA
OUTPUT SHOWN PLEASE
PLEASE SUBMIT BEFORE MIDNIGHT EST
Write a program which creates a binary search tree of different shapes from a file.
· The comparison is based on the shape’s area
· There are 3 shapes
o Rectangle
o Circle
o Right Triangle
· The file is tab delimited format goes as follows
o Rectangle side 1 side 2
o Circle radius
o Right Triangle side 1 side 2
· The binary search tree needs to have the following methods
o insert: inserts a new shape into the tree
o delete: deletes the shape instance. Keep in mind that in order for a shape to be equal it must have the same same type and area, but the sides can be interchangeable.
o print pre-order traversal: Print the data. Next explore the left subtree. Finally right explore subtree.
o print in-order traversal: Explore the left subtree. Print the data. Finally explore the right subtree.
o print post-order traversal: Explore the left subtree. Then explore the right subtree. Finally print out the data.
o max area: return the maximum area in the tree. Use the properties of the tree to make it efficient.
o delete areas greater than value: For a given value all shapes with an area that’s strictly greater than those values should be deleted. Use the principle of a binary search tree to help make it efficient.
· Finally write a test file that demonstrates THE POWER OF TREES!!! SHAPES!!! By reading a shape file.
SHAPE FILE:
HINTS:
Inheritance and polymorphism makes this problem not so difficult, so interfaces and base classes are a good idea.
Yes there will be many different class files.
Recursion is your friend.
Example of Results:
Welcome to the shape tree tester!
Reading from file
Rectangle 5 4
Circle 4
Right Triangle 3 2
Rectangle 2 7
Circle 8
Right Triangle 5 6
Rectangle 9 2
Circle 2
Rectangle 5 5
Right Triangle 9 9
Circle 7
Not properly formatted line. Yes you should check for these. Not intentionally causing a kerfuffle
Rectangle 3 8
Circle 9
Right Triangle 2 2
Printing pre-order
Rectangle Side 1: 5.0 Side 2: 4.0 Area: 20.0
Right Triangle Side 1: 3.0 Side 2: 2.0 Area: 3.0
Right Triangle Side 1: 2.0 Side 2: 2.0 Area: 2.0
Rectangle Side 1: 2.0 Side 2: 7.0 Area: 14.0
Circle Radius: 2.0 Area: 12.566370614359172
Right Triangle Side 1: 5.0 Side 2: 6.0 Area: 15.0
Rectangle Side 1: 9.0 Side 2: 2.0 Area: 18.0
Circle Radius: 4.0 Area: 50.26548245743669
Rectangle Side 1: 5.0 Side 2: 5.0 Area: 25.0
Rectangle Side 1: 3.0 Side 2: 8.0 Area: 24.0
Right Triangle Side 1: 9.0 Side 2: 9.0 Area: 40.5
Circle Radius: 8.0 Area: 201.06192982974676
Circle Radius: 7.0 Area: 153.93804002589985
Circle Radius: 9.0 Area: 254.46900494077323
Printing in-order
Right Triangle Side 1: 2.0 Side 2: 2.0 Area: 2.0
Right Triangle Side 1: 3.0 Side 2: 2.0 Area: 3.0
Circle Radius: 2.0 Area: 12.566370614359172
Rectangle Side 1: 2.0 Side 2: 7.0 Area: 14.0
Right Triangle Side 1: 5.0 Side 2: 6.0 Area: 15.0
Rectangle Side 1: 9.0 Side 2: 2.0 Area: 18.0
Rectangle Side 1: 5.0 Side 2: 4.0 Area: 20.0
Rectangle Side 1: 3.0 Side 2: 8.0 Area: 24.0
Rectangle Side 1: 5.0 Side 2: 5.0 Area: 25.0
Right Triangle Side 1: 9.0 Side 2: 9.0 Area: 40.5
Circle Radius: 4.0 Area: 50.26548245743669
Circle Radius: 7.0 Area: 153.93804002589985
Circle Radius: 8.0 Area: 201.06192982974676
Circle Radius: 9.0 Area: 254.46900494077323
Printing post-order
Right Triangle Side 1: 2.0 Side 2: 2.0 Area: 2.0
Circle Radius: 2.0 Area: 12.566370614359172
Rectangle Side 1: 9.0 Side 2: 2.0 Area: 18.0
Right Triangle Side 1: 5.0 Side 2: 6.0 Area: 15.0
Rectangle Side 1: 2.0 Side 2: 7.0 Area: 14.0
Right Triangle Side 1: 3.0 Side 2: 2.0 Area: 3.0
Rectangle Side 1: 3.0 Side 2: 8.0 Area: 24.0
Right Triangle Side 1: 9.0 Side 2: 9.0 Area: 40.5
Rectangle Side 1: 5.0 Side 2: 5.0 Area: 25.0
Circle Radius: 7.0 Area: 153.93804002589985
Circle Radius: 9.0 Area: 254.46900494077323
Circle Radius: 8.0 Area: 201.06192982974676
Circle Radius: 4.0 Area: 50.26548245743669
Rectangle Side 1: 5.0 Side 2: 4.0 Area: 20.0
The max area is: 254.46900494077323
Deleting Rectangle Side 1: 2.0 Side 2: 7.0 Area: 14.0
Printing in-order
Right Triangle Side 1: 2.0 Side 2: 2.0 Area: 2.0
Right Triangle Side 1: 3.0 Side 2: 2.0 Area: 3.0
Circle Radius: 2.0 Area: 12.566370614359172
Right Triangle Side 1: 5.0 Side 2: 6.0 Area: 15.0
Rectangle Side 1: 9.0 Side 2: 2.0 Area: 18.0
Rectangle Side 1: 5.0 Side 2: 4.0 Area: 20.0
Rectangle Side 1: 3.0 Side 2: 8.0 Area: 24.0
Rectangle Side 1: 5.0 Side 2: 5.0 Area: 25.0
Right Triangle Side 1: 9.0 Side 2: 9.0 Area: 40.5
Circle Radius: 4.0 Area: 50.26548245743669
Circle Radius: 7.0 Area: 153.93804002589985
Circle Radius: 8.0 Area: 201.06192982974676
Circle Radius: 9.0 Area: 254.46900494077323
Deleting values larger than 30
Printing in order
Right Triangle Side 1: 2.0 Side 2: 2.0 Area: 2.0
Right Triangle Side 1: 3.0 Side 2: 2.0 Area: 3.0
Circle Radius: 2.0 Area: 12.566370614359172
Right Triangle Side 1: 5.0 Side 2: 6.0 Area: 15.0
Rectangle Side 1: 9.0 Side 2: 2.0 Area: 18.0
Rectangle Side 1: 5.0 Side 2: 4.0 Area: 20.0
Rectangle Side 1: 3.0 Side 2: 8.0 Area: 24.0
Rectangle Side 1: 5.0 Side 2: 5.0 Area: 25.0
Explanation / Answer
public class BinarySearchTree { public static Node root; public BinarySearchTree(){ this.root = null; } public boolean find(int id){ Node current = root; while(current!=null){ if(current.data==id){ return true; }else if(current.data>id){ current = current.left; }else{ current = current.right; } } return false; } public boolean delete(int id){ Node parent = root; Node current = root; boolean isLeftChild = false; while(current.data!=id){ parent = current; if(current.data>id){ isLeftChild = true; current = current.left; }else{ isLeftChild = false; current = current.right; } if(current ==null){ return false; } } //if i am here that means we have found the node //Case 1: if node to be deleted has no children if(current.left==null && current.right==null){ if(current==root){ root = null; } if(isLeftChild ==true){ parent.left = null; }else{ parent.right = null; } } //Case 2 : if node to be deleted has only one child else if(current.right==null){ if(current==root){ root = current.left; }else if(isLeftChild){ parent.left = current.left; }else{ parent.right = current.left; } } else if(current.left==null){ if(current==root){ root = current.right; }else if(isLeftChild){ parent.left = current.right; }else{ parent.right = current.right; } }else if(current.left!=null && current.right!=null){ //now we have found the minimum element in the right sub tree Node successor = getSuccessor(current); if(current==root){ root = successor; }else if(isLeftChild){ parent.left = successor; }else{ parent.right = successor; } successor.left = current.left; } return true; } public Node getSuccessor(Node deleleNode){ Node successsor =null; Node successsorParent =null; Node current = deleleNode.right; while(current!=null){ successsorParent = successsor; successsor = current; current = current.left; } //check if successor has the right child, it cannot have left child for sure // if it does have the right child, add it to the left of successorParent. // successsorParent if(successsor!=deleleNode.right){ successsorParent.left = successsor.right; successsor.right = deleleNode.right; } return successsor; } public void insert(int id){ Node newNode = new Node(id); if(root==null){ root = newNode; return; } Node current = root; Node parent = null; while(true){ parent = current; if(idRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.