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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:56:59+00:00 2026-06-17T18:56:59+00:00

class Node { Node parent; Node left; Node right; int value; Node (int value)

  • 0
class Node {
    Node parent;
    Node left;
    Node right;
    int value;

    Node (int value) {
        this.value = value;
        this.parent = null;
        this.left = null;
        this.right = null;
    }
}

class TreeofNodes {
    Node insert (Node root, Node node) {
        if (root == null)
        {
            root = node;
            return root;
        }

        Node cur    = root;
        Node father = root;

        while(cur != null)
        {
            father = cur;
            if (cur.value > node.value)
                cur = cur.left;
            else
                cur = cur.right;
        }
        if(father.value > node.value)
            father.left  = node;
        else
            father.right = node;

        node.parent = father;

        return node;
    }

    void Inorder (Node n) {
        if (n == null)
           return;
        Inorder (n.left);
        System.out.print (n.value + " ");
        Inorder (n.right);
    }
}

class binarySearchTree {

public static void main (String [] args) {

    int A[] = {6, 8, 5, 3, 7, 9, 1};

    TreeofNodes obj = new TreeofNodes ( );

    Node root = null;
    Node n = new Node (A[0]);
    root = obj.insert (root, n);

    Node Croot = root;

    for (int i = 1; i < 7; i++)
    {
        n = new Node (A[i]);
        Croot = obj.insert (Croot, n);
    }

    System.out.println ("============ Inorder ============");
    obj.Inorder (root);
  }
}

When I call Inorder method, I expect the output to be:

 1   3   5   6   7   8   9

But it is

 6   3   7   1   9   5   8

I suspect my insert method is wrong.

Can someone tell me where I am wrong and how can I solve it?

  • 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-17T18:57:00+00:00Added an answer on June 17, 2026 at 6:57 pm

    Your insert function returns node at the end instead of root. Change:

        return node;
    

    to

        return root;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my code: class Node{ int value; Node left; Node right; Node parent;
I have a recursive model like this: public class Node { public int Id
Suppose I have objects that have a parent-child relationship like this: public class Node
I have a class like the following: class node { public: node* parent; std::list<node*>
I have a class: class node { public: node& parent; } I want to
public class Node { private Node nextNode; @Override public int hashCode() { //How to
I have this code for BinaryTree creation and traversal class Node { Integer data;
class Node { public: Node *parent; // used during the search to record the
I have a class called node. I want to store each nodes parent within
Here is my Node class. class Node { private: public: T data; Node<T>* left;

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.