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

19. Because the user of this class doesn't need to know about it, and if they did get access to it they would probably screw up the list.
28. When the return statement is executed
32. This stops execution of the method immediately and returns to the wherever the method was called from. This therefore makes it exit the loop.
57. Because its the recursive implementation for the public print() method. Users should call print(), but how that works is private to this class. If they had access to the print(Node node) method who knows what damage they may do?

commented: Thank you for clearing this out for me +0

with the private aspect of the code.

do you think it is just a convention, but not a real "must"..

could i just write a single print method?

Code would still work without "private", but real systems made up of many many classes would become impossible to maintain.
The public print method has no parameters, but the private implementation is recursive and needs a Node parameter, so they should be two different methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.