answer the following in Java (IntelliJ)
The file that is attached to this assignment is a zip file of a BST implementation. You need to finish the implementation of swapNodes(int firstValue, int secondValue), isValid(), fixTree().
swapNodes will take two values, you will find the two nodes that contain these values and swap them.
isValid will check if the tree is a valid BST and follows the rules
fixTree will find the nodes that aren't correct and swap them back
Turn in the fully implemented version of the BinarySearchTree.java file, no need to submit the entire zip file,
App CLASS :
package app;
public class App {
public static void main(String[] args) throws Exception {
BinarySearchTree bst = new BinarySearchTree();
// populating the BST
bst.add(8);
bst.add(6);
bst.add(10);
bst.add(2);
bst.add(7);
bst.add(4);
bst.add(1);
bst.add(6);
bst.add(3);
bst.add(5);
bst.add(9);
bst.add(11);
bst.add(14);
bst.add(12);
bst.add(20);
// Print the nodes in the BST in order
System.out.println(bst.toString());
// should print true
System.out.println(bst.isValid());
// ---- EXAM ---- implement functions in the BST so this works ----
// swap the nodes that contain 10 and 2
bst.swapNodes(10, 2);
// now this will print out of order
System.out.println(bst.toString());
// should print false
System.out.println(bst.isValid());
// this will fix the tree
bst.fixTree();
// now this will print back in order
System.out.println(bst.toString());
// should print true again
System.out.println(bst.isValid());
// ---- END OF EXAM ----
}
}
BinarySearch Tree Class ( THE ONE TO BE SUBMITTED )
package app;
public class BinarySearchTree {
// just a root variable
Node root;
/**
* Empty constructor is all we need for now
*/
public BinarySearchTree() {
this.root = null;
}
/**
* Adds the data to the tree, duplicates are not allowed
*
* @param data value that you want to insert into the tree
*/
public void add(int data) {
this.root = this.addNode(root, data);
}
/**
* You need to implement this for the exam, you need to swap the two values, so
* find the node that contains the first value and find the node that contains
* the second value, then swap them.
*
* @param firstValue the value of the first node to swap
* @param secondValue the value of the second node to swap
* @return true if the swap was successful and false if it wasn't (e.g. one of
* the values wasn't there)
*/
public boolean swapNodes(int firstValue, int secondValue) {
return false;
}
/**
* You need to implement this for the exam, you need to check if the tree is
* constructed in a valid way, e.g. if a child of a node violates the BST rules
* it is invalid and returns false
*
* @return true if the tree is valid and false if it isn't
*/
public boolean isValid() {
return false;
}
/**
* You need to implement this for the exam, you need to find the two nodes that
* violate the BST and call swapNodes on them
*
* @return true if the tree is valid and false if it isn't
*/
public boolean fixTree() {
return false;
}
/**
* Recursive function to find where to insert a node, no duplicates
*
* @param current the node that we are comparing to
* @param data the data we want to insert into tree
* @return the modified node, not the inserted node
*/
private Node addNode(Node current, int data) {
// time to insert node
if (current == null) {
return new Node(data);
}
// compare the data to the current node to see which way to traverse
if (data < current.data) {
current.left = this.addNode(current.left, data);
} else if (data > current.data) {
current.right = this.addNode(current.right, data);
}
// if the data is already there, just return current
return current;
}
@Override
public String toString() {
String result = this.inOrderTraversal(this.root);
return result.trim();
}
private String inOrderTraversal(Node current) {
StringBuilder strBldr = new StringBuilder();
// check if we have anything to add to the string
if (current != null) {
// go left first because this is inorder
strBldr.append(this.inOrderTraversal(current.left));
// no print the current node
strBldr.append(current.data + " ");
// go right last because inorder
strBldr.append(this.inOrderTraversal(current.right));
}
return strBldr.toString();
}
}
public class node:
package app;
public class Node {
// the two children of this node
Node left;
Node right;
// we are just storing ints in this node
Integer data;
// just one constructor, I don't want to create empty nodes
public Node(Integer data) {
this.data = data;
}
}
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec faci
sec
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, cosectetur adipiscing elit. Nam lacinia puUnlock access to this and over
10,000 step-by-step explanations
Have an account? Log In
sectetur adipiscing elit. Nam lacinia pulvinar to
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce du