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

  • SEARCH
  • Home
  • 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 8646567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:50:40+00:00 2026-06-12T12:50:40+00:00

My inputs results 24, 4, 2, 3, 9, 10, 32 , and I am

  • 0

My inputs results 24, 4, 2, 3, 9, 10, 32, and I am getting following result 2, 3, 4, 24.

I am using a stack. When I have manually checked this program the node is not going through else if at 4 on stack, even if has right sub tree.

public void inorderNonRcursive(Node root){

    Stack s = new Stack();
    Node node = root;
    Node k;
    int c=0;

    if(node != null) {
        s.push(node);    
    }

    while(!s.stack.isEmpty()) {   

        //node=(Node) s.stack.get(s.stack.size()-1);
        System.out.println("first condition" + (node.getleft() != null && (node.getVisited() == false)) + "second condi" + (node.getRight() != null));

        if(node.getleft() != null && (node.getVisited() == false)) {
            node = node.getleft();
            s.push(node);
            System.out.println("   from 1           "+(++c)); 

        } else if(node.getRight() != null) {
            k = s.pop();
            System.out.println(k.getvalue());
            node=node.getRight();
            s.push(node);
            System.out.println("    from 2          "+(++c)); 

        } else {
            k = s.pop();
            System.out.println(k.getvalue());
            System.out.println("        from 3      "+(++c)); 
        }  

    }

}
  • 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-12T12:50:41+00:00Added an answer on June 12, 2026 at 12:50 pm

    To me, there are two problems in the design:

    1. The algorithm seems to be the recursive one adapted for iteration and
    2. The Node class knows about being visited.

    Here is a different solution (you will need to adapt it a bit):

    // Inorder traversal:
    // Keep the nodes in the path that are waiting to be visited
    Stack s = new Stack(); 
    // The first node to be visited is the leftmost
    Node node = root;
    while (node != null)
    {
        s.push(node);
        node = node.left;
    }
    // Traverse the tree
    while (s.size() > 0)
    {
        // Visit the top node
        node = (Node)s.pop();
        System.out.println((String)node.data);
        // Find the next node
        if (node.right != null)
        {
            node = node.right;
            // The next node to be visited is the leftmost
            while (node != null)
            {
                s.push(node);
                node = node.left;
            }
        }
    }
    

    Referring back to my first comments, you don’t say you wrote pseudo code and tested it. I think this a key step in writing new algorithms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following inputs: <input class=input-text type=text name=nombres id=nombres /> <input class=input-text type=text
I have the following HTML: <div class=row1> <input type=radio name=group1 value=0 checked=checked/>Zero <input type=radio
I have written down the following program that uses the quicksort algorithm to sort
Using MVC3 I am trying to accomplish the following. I have a table which
I'm having trouble getting the following code to work correctly. Using an online IEEE-754
Not sure why I am getting just one row with wrong count! Following is
I'm getting an unexpected result for my query in group function. I'm using the
I've been using Selenium IDE and getting some good results. I've done a lot
getting a bit confused over this problem... I have an administrative part of my
select @result=@input.query('*') for xml raw,type Above statement will generate following alert: Msg 6819, Level

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.