I have a homework. My teacher requires me to do a project which use Link base binary tree. I do almost all needed function but still remain print method. It throws StackOverFlow every time I complile project. I know since the print method but, I have a base case to break. Why still have this problem?

public class TestTrees {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /**
         * Tree:
         *          (R)
         *      (A')   (`B)
         *  (C') (`D)
         *       (E') (`F)
         *            (G')
         */
        BTNode<String> btRoot = new BTNode<String>("R");
        BTNode<String> bt1 = new BTNode<String>("A");
        BTNode<String> bt2 = new BTNode<String>("B");
        BTNode<String> bt3 = new BTNode<String>("C");
        BTNode<String> bt4 = new BTNode<String>("D");
        BTNode<String> bt5 = new BTNode<String>("E");
        BTNode<String> bt6 = new BTNode<String>("F");
        BTNode<String> bt7 = new BTNode<String>("G");
        
        btRoot.setLeft(bt1);
        btRoot.setRight(bt2);
        
        bt1.setLeft(bt3);
        bt1.setRight(bt4);
        bt4.setLeft(bt5);
        bt4.setRight(bt6);
        //bt6.setLeft(bt7);
        
        LBinaryTree<String> binaryTree = new LBinaryTree<String>(btRoot);
        
        System.out.println("Empty Tree? "+binaryTree.isEmpty());
        System.out.println("Tree depth: "+binaryTree.depth());
        System.out.println("Tree leaves: "+binaryTree.countLeaves());
        System.out.println("Internal Path length: "+binaryTree.iPathLength(0));
        System.out.print("Tree print pre-Order: ");
        binaryTree.print(-1);
        System.out.print("\nTree print in-Oder: ");
        binaryTree.print(0);
        System.out.print("\nTree print post-Order: ");
        binaryTree.print(1);
    }
}
public class BTNode<T> {
    /**
     * @see Generic
     */
    private T data;
    private BTNode left, right;

    /**
     * Constructor of BTNode
     * @param data generic type
     */
    BTNode(T data) {
        this.data = data;
        //this.left = this.right = null;
    }

    public T getData() {
        return data;
    }

    public BTNode getLeft() {
        return left;
    }

    public BTNode getRight() {
        return right;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setLeft(BTNode left) {
        this.left = left;
    }

    public void setRight(BTNode right) {
        this.right = right;
    }
    
    public boolean isLeave(){
        return data != null &&left == null && right == null;
    }
    /**
     * Print data of the node
     */
    public void print() {
        System.out.print(data);
    }
}
public class LBinaryTree<T> extends Tree {

    private BTNode<T> root;

    public LBinaryTree(BTNode<T> root) {
        this.root = root;
    }

    @Override
    public int size() {
        if (!this.isEmpty()) {
            return 1 + new LBinaryTree<T>(root.getLeft()).size() 
                    + new LBinaryTree<T>(root.getRight()).size();
        }
        else
            return 0;
    }

    @Override
    public int countLeaves() {
        if (this.isEmpty())
          return 0;
        else if (root.isLeave())
             return 1;
        else return
                (new LBinaryTree<T>(root.getLeft()).countLeaves()
                + new LBinaryTree<T>(root.getRight()).countLeaves());
    }

    @Override
    public boolean isEmpty() {
        return root == null;
    }

    @Override
    
    public void print(int order) {
        if (root != null)
        {
             switch(order)
             {
                  case 1:
                    preOrder(this.root);
                    break;
                  case 0:
                    inOrder(this.root);
                    break;
                  case -1:
                    postOrder(this.root);
                    break;
             }
        }
    }
    
   private void preOrder(BTNode<T> root)
   {
        while (root != null)
        {
          System.out.print(root.getData()+ " ");
          preOrder(this.root.getLeft());
          preOrder(this.root.getRight());
        }
   }
   
   private void inOrder(BTNode root)
   {
        while (root != null)
        {
          inOrder(this.root.getLeft());
          System.out.print(this.root.getData());
          inOrder(this.root.getRight());
        }
   }
   
   public void postOrder(BTNode root)
   {
        while (root != null)
        {
          postOrder(this.root.getLeft());
          postOrder(this.root.getRight());
          System.out.print(this.root.getData());
        }
   }

   public int depth() {
        int leftTreeDepth,rightTreeDepth;
        if (root == null || root.isLeave())
             return 0;
        else
             return (1+ Math.max(leftTreeDepth = new LBinaryTree<T>(root.getLeft()).depth(),
                                 rightTreeDepth = new LBinaryTree<T>(root.getRight()).depth()));
    }
    /**
     * Internal Path Length of Tree
     * @return integer number
     * @see http://mathworld.wolfram.com/InternalPathLength.html
     */
   public int iPathLength(int level) {
        if(root.isLeave())
             return level+1;
        else
          return (level + new LBinaryTree<T>(root.getLeft()).iPathLength(level + 1)
                + new LBinaryTree<T>(root.getRight()).iPathLength(level + 1));
    }
}

Please help me :((

It throws StackOverFlow every time I complile project.

Do you mean you get the error when you execute the program.
Compiling it should not give a stack overflow.

The program is probably in a recursive loop and not stopping until it runs out of memory.
Add some println statements to the code to show where the code is executing and to show the values of the variables that control its recursion. The output will help you understand what the code is doing.

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.