i have questions abou the algorithm that i have got here:
public class BTreeEx3 {
public static void main(String[] args) {
BTree tree=new BTree();
tree.add(5);
tree.add(6);
tree.add(1);
tree.add(19);
tree.add(3);
tree.add(10);
tree.add(2);
tree.print();
}
}
class BTree{
private Node head; // why is the head private?
public void add(int info){
Node node=new Node(info);
if(head==null){
head=node;
return;
}
Node current=head;
while (true) { // when will the loop stop
if(info<current.info){
if (current.left==null) {
current.left=node;
return; // why do i need to use the return statements?
}
else
current = current.left;
}
else {
if(info>current.info){
if (current.right==null) {
current.right=node;
return;
}
else
current = current.right;
}
}
}
}
//toLinkedList()
public void print(){
print(head);
}
private void print(Node node){ // why does this method has to be private?
if(node==null)
return;
print(node.left);
System.out.println(node.info);
print(node.right);
}
class Node{
int info;
public Node(int info) {
super();
this.info = info;
}
Node left;
Node right;
}
}