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

Could someone explain the code below (give a full explanation) how the code work

ID: 3641119 • Letter: C

Question

Could someone explain the code below (give a full explanation) how the code works, how is implemented, what the purpose or purposes of it.. (life safer will be given for the best explanation)

package BinarySearchTree;

public class BinarySearchTree {
private Node root;

public Node getRoot() {
return root;
}

public void setRoot(Node root) {
this.root = root;
}

public void insert(Node z) {
insertBranch(z);
}

public void insertBranch(Node z) {

Node y = null;
Node x = root;

while (x != null) {
y = x;

if (z.getKey() < x.getKey()) {
x = x.getLeftChild();
} else {
x = x.getRightChild();
}
}

z.setParent(y);

if (y == null) {
root = z;
} else if (z.getKey() < y.getKey()) {
y.setLeftChild(z);
} else {
y.setRightChild(z);
}
}

private void printInOrder(Node node) {
if (node != null) {
printInOrder(node.getLeftChild());
System.out.print(node.getKey() + " ");
printInOrder(node.getRightChild());
}
}

public void printInOrder() {
printInOrder(root);
}

public Node find(Comparable x, Node t) {
while (t != null) {
Integer i = new Integer(t.getKey());
if (x.compareTo(i) < 0)
t = t.getLeftChild();
else if (x.compareTo(i) > 0)
t = t.getRightChild();
else
return t; // Match
}

return null; // Not found
}

}

Explanation / Answer

package BinarySearchTree; //class definition public class BinarySearchTree { //private pointer var Node private Node root; //getter public Node getRoot() { return root; } //setter public void setRoot(Node root) { this.root = root; } //inert function to put a given node in he correct place in the tree public void insert(Node z) { insertBranch(z); } public void insertBranch(Node z) { //declare two helper variables, y -previous node, x - current node Node y = null; Node x = root; //go all the way until you reach null - that's where you want to insert while (x != null) { //store previous val y = x; //go left or right based on value of z if (z.getKey()