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
}
}