Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:49:54+00:00 2026-06-01T07:49:54+00:00

My code seems to do infinite recursion when I invoke it with trees of

  • 0

My code seems to do infinite recursion when I invoke it with trees of a greater depth.
I tried to do it without recursion, and well thats kinda difficult to do since i need to manipulate the tree itself, anyways here’s is m code. Thanks

Problem is with the printPostOrder method, it gets to a leaf node and the goes back and forth printing that leaf node and its parent until the stack overflows

I’d rather I post my full code so that you can get what I’m I trying to do.

Node insertion is like this

import java.util.*;

public class IPG {

    class Node {

        private int arity;
        private String label;
        private int value;
        private Node[] children;
        protected int visit = 0;
        protected boolean isVisited = false;

        public Node(int arity, String label) {
            this.arity = arity;
            this.label = label;
            this.children = new Node[3];
        }

        public Node(Node another) {
            this.label = another.label;
            this.arity = another.arity;
        }

        @Override
        public String toString() {
            return label;
        }

        String getLabel() {
            return label;
        }

        void insertChild(int pos, Node n) {
            if (pos < arity) {
                children[pos] = n;
            }
        }

        Node getChildAt(int i) {
            return children[i];
        }

        void setValue(int value) {
            this.value = value;
        }

        int getArity() {
            return arity;
        }

        void replace(Node another) {
            this.arity = another.arity;
            this.children = another.children;
            this.label = another.label;
        }

    }

    private Node[] functions = { new Node(2, "AND"), new Node(2, "OR"),
            new Node(1, "NOT"), new Node(3, "IF") };

    private Node[] terminals = { new Node(0, "A0"), new Node(0, "D1"),
            new Node(0, "D0"), new Node(0, "A1"), new Node(0, "D2"),
            new Node(0, "D3"), new Node(0, "A2"), new Node(0, "D4"),
            new Node(0, "D5"), new Node(0, "D6"), new Node(0, "D7") };

    private Random random = new Random();

    private int multiplexerType = 3;

    public Node getTerminal() {
        return terminals[random.nextInt(multiplexerType)];
    }

    public Node getFunction() {

        return functions[random.nextInt(3)];
    }

    public Node getAnyNode() {
        return random.nextInt(2) == 1 ? getFunction() : getTerminal();
    }

    public Node generateGrow(int depth) {
        Node root;

        if (depth > 1)
            root = getAnyNode();

        else
            root = getTerminal();

        for (int i = 0; i < root.getArity(); i++)
            root.insertChild(i, generateGrow(depth - 1));

        return root;
    }

    public Node generateFull(int depth) {
        Node root;

        if (depth > 1)
            root = getFunction();

        else
            root = getTerminal();

        for (int i = 0; i < root.getArity(); i++)
            root.insertChild(i, generateFull(depth - 1));

        return root;
    }

    public void printPostOrder() {
        Node root = generateFull(3);
        printPostOrder(root);
    }

    private void printPostOrder(Node n) {
        if (n == null) {
            return;
        } else {
            System.out.println(n + " ");

            printPostOrder(n.children[0]);
            printPostOrder(n.children[1]);
            printPostOrder(n.children[2]);
        }
    }

    public static void main(String[] args) {
        new IPG().printPostOrder();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:49:56+00:00Added an answer on June 1, 2026 at 7:49 am

    The problem is that when you are generating your functions array, you are only creating one node of each type, and then getFunction() re-uses these same nodes. What happens is that, if for example, you insert an “AND” node and its child is also an “AND” node, a cycle is created because they are actually the same object.

    It’s an easy fix, you just need to make sure to create a new node with each call of getFunction(), for example like so:

    private String [] functionNames = {"AND", "OR", "NOT", "IF"};
    private int [] functionArities = {2,2,1,3};
    
    public Node getFunction() {
        int index = random.nextInt(3); // If you want to use "IF" nodes too this
                                       // needs to be nextInt(4)
        return new Node(functionArities[index],functionNodes[index]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which is seems to be lead to the infinite
It seems to me that it would work perfectly well to do tail-recursion optimization
The code seems not working. // $counter is an instance of Zend_Db_Table_Abstract $counter->update(array('hits' =>
My code seems to run but it does not display the result any result
The following code seems to be just too much, for getting a single count
The below code seems like it should work; however, the blob in the database
The following code seems not to work, even though the file appears to be
The following piece of code seems to unreliably execute and after and undeterministic time
I notice that modern C and C++ code seems to use size_t instead of
Hey guys, the following snippet of jQuery code seems to work fine in Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.