0

I am writing a program for my java class that displays a binary tree. However one of the problems I'm running into is that the program creates an infinite number of copies of itself when it's first executed. I'm fairly sure I know where the problem is, I just have no idea how to fix it. Any help is appreciated, thanks!

I think the problem is at the end of this constructor:

public Window_Controller() throws HeadlessException {

            addMouseListener(this);
        addWindowListener(close);

        setTitle("Binary Trees - Alpha Stage");
        setSize(1200, 700);
        setLocation(40, 0);
        setVisible(true);

        incrementButton = new Button("+1", Color.white, deLoc - 47, y, 45);
        decrementButton = new Button("-1", Color.white, deLoc, y, 45);
        fullButton = new Button("Full Binary Tree", Color.cyan, x, y, 110);
        completeButton = new Button("Complete Binary Tree", Color.green, x + 130, y, 150);
        // I think this is the problem
        fullTree = new Full_Binary_Tree();
    }

Because this class extends the Window_Controller class:

public class Full_Binary_Tree extends Window_Controller
{

    /**
     * 
     */
    private static final long   serialVersionUID    = -3599016597017756766L;

    BinaryNode rootNode;
    BinaryNode current;
    BinaryNode leftNode;
    BinaryNode rightNode;

    /**
     * @throws HeadlessException
     */
    public Full_Binary_Tree() throws HeadlessException
    {

    }

    public void add(int value)
    {
        BinaryNode aNode = new BinaryNode(value);
        aNode.setLeftNode(rootNode);
        rootNode = aNode;
        current = rootNode;
    }

    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        if(rootNode.getRightNode() == null) {
            g2d.drawLine(rootNode.getX() + 25, rootNode.getY() + 25, rootNode.getX() + 65, rootNode.getY() + 87);
            g2d.setColor(nullColor);
            g2d.setFont(nullFont);
            g2d.drawString("NULL", rootNode.getX() + 60, rootNode.getY() + 100);
            g2d.setFont(defaultFont);
            g2d.setColor(Color.black);
        }

        if(rootNode.getLeftNode() == null) {
            g2d.drawLine(rootNode.getX() + 25, rootNode.getY() + 25, rootNode.getX() - 15, rootNode.getY() + 87);
            g2d.setColor(nullColor);
            g2d.setFont(nullFont);
            g2d.drawString("NULL", rootNode.getX() - 42, rootNode.getY() + 100);
            g2d.setFont(defaultFont);
            g2d.setColor(Color.black);
        }

        if(current != null) {
            current.paint(g2d);
        }



    }
}

I think what's happening is when the main() method calls the constructor and it reaches that line it calls the constructor in Full_Binary_Tree, which will call the constructor for the Window_Constructor class. Of course I could be wrong and the problem is completely unrelated to this.

3
  • 1
    You're correct - recursive object instantiation. Why do you even need the Window_Controller class?
    – home
    Apr 27, 2013 at 20:40
  • 1
    Yes that is the problem..But why are you calling fullTree = new Full_Binary_Tree(); within Window_Controller() constructor? Why not create a separate method within Window_Controller class where you create the object of FullTree and call that method via the object of Window_Controller in your program ?
    – Vishal K
    Apr 27, 2013 at 20:41
  • @home I'm using the Window_Controller class to create the window and buttons on it, the Full_Binary_Tree class handles the creation of the binary tree. @VishalK I'll try that and see if it works.
    – Robert
    Apr 27, 2013 at 20:50

1 Answer 1

0

Instead of creating Full_Binary_Tree object within Window_Controller constructor you should create separate method within Window_Controller class where you create the Full_Binary_Tree object. For example your code should be something like this:

public class Window_Controller
{
....
 public Window_Controller() throws HeadlessException {

            addMouseListener(this);
        addWindowListener(close);

        setTitle("Binary Trees - Alpha Stage");
        setSize(1200, 700);
        setLocation(40, 0);
        setVisible(true);

        incrementButton = new Button("+1", Color.white, deLoc - 47, y, 45);
        decrementButton = new Button("-1", Color.white, deLoc, y, 45);
        fullButton = new Button("Full Binary Tree", Color.cyan, x, y, 110);
        completeButton = new Button("Complete Binary Tree", Color.green, x + 130, y, 150);
        // I think this is the problem
        //fullTree = new Full_Binary_Tree();//Don't create object of Full_Binary_Tree here.
    }
  public synchronized void createFullBinaryTreeInstance()
  {
    if (fullTree == null)
    fullTree = new Full_Binary_Tree();
  }
}

Now, When you are creating Window_Controller object call createFullBinaryTreeInstance() method explicity after the creation of object . Like:

Window_Controller wController = new Window_Controller();
wController.createFullBinaryTreeInstance();
2
  • I could also use this to create a tree when a button is pressed with a method like this: private void fullAction(){ createFullBinaryTreeInstance(); } right?
    – Robert
    Apr 27, 2013 at 21:28
  • Yes, You could but You must call it using the instance of Window_Controller class. Like private void fullAction(){ wController.createFullBinaryTreeInstance(); } . Where wController is instance variable within the class where fullAction method is defined.
    – Vishal K
    Apr 27, 2013 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.