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 6621713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:18:13+00:00 2026-05-25T21:18:13+00:00

I have a question regarding preorder traversal of binary search tree. I know what

  • 0

I have a question regarding preorder traversal of binary search tree. I know what the algorithm has to be like, its pretty simple:

void preOrder(Node node) {
    print(node);

    if (node.left() != null)
        preOrder(node.left());
    if (node.right() != null)
        preOrder(node.right());
}

For some reason, my function prints out only the left side subtree of the root node, and it prints out the lowest node twice. I ran a search method on one of the items on the right side and it returned true so I assume my insertion is working properly. Why is this happening?

My code is below. The public method calls the private one. In the private one, the first two if-statements are there to print the left and right nodes connected to that node. The last two do the actual recursive algorithm.

public void print() {

    if (root == null)
        System.out.println("Tree is empty");
    else
        print(root);
}


private void print(NodeBST node) {

        printOut(node);

        if (node.left() != null) {
            System.out.print("Left: ");
            printOut(node.left());
        }

        else
            System.out.println("No left");

        if (node.right() != null) {
            System.out.print("Right: ");
            printOut(node.right());
        }

        else
            System.out.println("No right");

        System.out.println("");

        if (node.left() != null) {
            node = node.left();
            print(node);
        }

        if (node.right() != null) {
            node = node.right();
            print(node);
        }
    }
  • 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-05-25T21:18:14+00:00Added an answer on May 25, 2026 at 9:18 pm

    Well one bug you have is that you overwrite node on the line just before the first print(node) and then reuse the modified version again straight afterwards. Presumably you want node to be the original value when doing the if(node.right() != null) test?

    You can avoid this by e.g. just calling print(node.left()); in the first if.

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

Sidebar

Related Questions

I have a question regarding some CSS that I'm sure has a simple solution,
Have a question regarding something which has been bugging me for some time now.I'm
I have a question regarding Antlr, I am building a simple parser with it
I have question regarding polymorphism about its assignment statement, for Example this is The
I have a question regarding Git. The place where I work has 2 developers:
I have a question regarding the std::sort algorithm. Here is my test code: struct
I have a question regarding editing a UITableView. I want to know how to
I have a question regarding WordPress, specifically version 3.0 and newer. Does anyone know
I have a question regarding the Impersonation on WCF. I'd like to connect to
Hi I have question regarding cin and buffer. I want to make a simple

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.