Hello all, I need a bit of help with this program, I need to make my code create a complete binary tree using an array. As well as create a driver class that displays the tree graphically

Here is what I have so far with my code

Tree Code:

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
  
public class BTree {
	
	private char data;
	private BTree left, right; 
	
	public BTree(initalData, initalLeft, initalRight){
		data = initalData;
		left = initalLeft;
		right = initalRight;
		
	}
	public BTree insert(BTree node, int data) {
		if (node == null) {
			node = new BTree(data, null, null);
		} else {
			if (data < node.getData()) {
				// insert left
				node.left = insert(node.getLeft(), data);
			} else {
				// insert right
				node.right = insert(node.getRight(), data);
			}
		}
		return node;
	}
	private int getData()// Gets the data
	{
		return data;
	}
	private BTree getLeft() //gets the data from the left child
	{
		return left;
	}
	private BTree getRight() //gets data from right child
	{
		return right;
	}
	
   public int getLeftmostData( )
	   {
	      if (left == null)
	         return data;
	      else
	         return left.getLeftmostData( );
	   }
   public int getRightmostData( )
   {
      if (right == null)
         return data;
      else
         return right.getRightmostData( );
   }
   
   
   public void inorderPrint( )
   {
      if (left != null)
         left.inorderPrint( );
      System.out.println(data);
      if (right != null)
         right.inorderPrint( );
   }  
   
   public boolean isLeaf( )
   {
      return (left == null) && (right == null);                                               
   } 
   
   public void preorderPrint( )
   {
      System.out.println(data);
      if (left != null)
         left.preorderPrint( );
      if (right != null)
         right.preorderPrint( );
   } 
   
   public void postorderPrint( )
   {
      if (left != null)
         left.postorderPrint( );
      if (right != null)
         right.postorderPrint( );
      System.out.println(data);
   }   
   
   public void print(int depth)
   {
      int i;
   
      // Print the indentation and the data from the current node:
      for (i = 1; i <= depth; i++)
         System.out.print("    ");
      System.out.println(data);

      // Print the left subtree (or a dash if there is a right child and no left child)   
      if (left != null)
         left.print(depth+1);
      else if (right != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }

      // Print the right subtree (or a dash if there is a left child and no left child)  
      if (right != null)
         right.print(depth+1);
      else if (left != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }
   }
   
   public BTree removeLeftmost( )
   {
      if (left == null)
         return right;
      else
      {
         left = left.removeLeftmost( );
         return this;
      }
   }
   
   public BTree removeRightmost( )
   {
      if (right == null)
         return left;
      else
      {
         right = right.removeRightmost( );
         return this;
      }
	   
   }
   public void setData(char newData)   
   {
      data = newData;
   }       
   
   public void setLeft(BTree newLeft)
   {                    
      left = newLeft;
   }
   
   public void setRight(BTree newRight)
   {                    
      right = newRight;
   }  
   
   public static BTree treeCopy(BTree source)
   {
      BTree leftCopy, rightCopy;

      if (source == null)
         return null;
      else
      {
         leftCopy = treeCopy(source.left);
         rightCopy = treeCopy(source.right);
         return new BTree(source.data, leftCopy, rightCopy);
      }
   }
   
   public static int treeSize(BTree root)
   {
      if (root == null)
         return 0;
      else
         return 1 + treeSize(root.left) + treeSize(root.right);
   }  
   
   public static int countTarget(BTree root, int target) 
// The tree is a binary search tree. 
{ 
	   if (root == null)
	         return 0;
	   else if (root.data == target)
	         return 1 + countTarget(root.left,target) + countTarget(root.right,target);
	   else return 0 + countTarget(root.left,target) + countTarget(root.right,target);

Driver Class:

public class BTreeDriver {
	public static void main(String[] args) {
		BTree tree[] = new BTree[10];
		for(int i=0;i<5;i++)
		{
			tree[i]= new BTree(i,null,null);
		}
		
		for(int j=0;j<3;j++)
		{
			tree[j].setLeft(tree[j+1]);
			tree[j].setRight(tree[j+2]);
		}	
		System.out.println(BTree.treeSize(tree[0]));     
	    tree[0].print(0)  ;                         
		System.out.println(BTree.countTarget(tree[0],4));
	}

}

Hey, could you elaborate on the issue you are having?

Never mind, I fixed the problem myself.

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.