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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:18:41+00:00 2026-06-01T16:18:41+00:00

I was trying to implement a simple Binary Search Tree for practice. I tried

  • 0

I was trying to implement a simple Binary Search Tree for practice. I tried to just add values and print the values in the nodes. However, I am not getting the proper ascending order of values in the nodes. Here is what I have:

struct Node
{
    int data;
    Node* leftN;
    Node* rightN;

};

typedef Node* Node_ptr;
Node_ptr head;

//INSERT_VALUE FUNCTION
Node* insert_value(Node_ptr leaf, int key)
{
    //Root case when there is no set value yet  
    if(leaf == NULL)
    {
        leaf = new Node;
        head = leaf;
        cout << "Make the first node" << endl;
        leaf->data = key;
        leaf->leftN = NULL;
        leaf->rightN = NULL;
        return leaf;
    }   
    //Left child Node
    if(key < leaf->data)
    {
        //Search for a spot in the tree to add a Node (left value < root value < right value)
        //This is only true for the left child Node
        if(leaf->leftN != NULL)
            insert_value(leaf, key);
        //We have found a spot in the tree to add a new Node and add the value of key
        else 
        {
            cout << "Insert-left" << endl;
            leaf->leftN = new Node;
            leaf = leaf->leftN;
            leaf->data = key;
            leaf->leftN = NULL;
            leaf->rightN = NULL;
            return leaf;
        }
    }

    //Right child Node
    else if(key >= leaf->data)
    {
        //Search for a spot to add a new Node in the tree (only amongst the right child Nodes)
        if(leaf->rightN != NULL)
            insert_value(leaf, key);    
        //Once we have found a spot to add a new Node, append the new Node
        else
        {
            cout << "Insert-right" << endl;
            leaf->rightN = new Node;
            leaf = leaf->rightN;    
            leaf->data = key;
            leaf->leftN = NULL;
            leaf->rightN = NULL;
            return leaf;
        }
    }   
}

//PRINT FUNCTION
void printTree(Node_ptr leaf)
{
    if(leaf == NULL)
        return;
    printTree(leaf->leftN);
    cout << "Data element: " << leaf->data << endl;
    printTree(leaf->rightN);
}

//MAIN
int main()
{
    Node_ptr root = NULL;
    int i;

    //initialize values
    for(i = 1; i < 12; i+=2)
        root = insert_value(root, i);
    root = head;
    for(i = 0; i < 11; i+=2)
        root = insert_value(root, i);
    root = head;
    printTree(root);

    root = head;
    cout << "Head Node: " << root->data << endl;

    return 0;
}

When I printed the results, this is what I got:
0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 11 and the value of the head node is 1

  • 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-01T16:18:43+00:00Added an answer on June 1, 2026 at 4:18 pm

    Because you are calling the insertion as:

        root = insert_value(root, i);
    

    the location at which you insert is always using a subtree starting at the last insertion. Except the time that you re-start to add the odd numbers, when you start inserting at the head.

    If you create a class BinarySearchTree that contains a head pointer, and an insert method taking an int value that calls Node::insert( head, value ), then you can just call insert on that class, without passing it a node, and it can always see to it that the insertions use the root of the tree for the start of the recursion.

    Just me, but I would have a constructor for Node that takes an int and initializes the pointers to NULL. That way you don’t have to do that in the insert method.

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

Sidebar

Related Questions

Alright, so I've been trying to implement a simple binary search tree that uses
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
Just trying to implement a simple audit logging solution with Grails 2.0.2 and it
I'm trying to implement a simple fragment in android. I've tried various approaches and
I'm trying to implement a simple horizontal navigation menu that just shows a single
I was trying to write a method which set values in a binary search
Trying to implement a simple print in SL4. I have a DataGrid that I
I've been trying to implement simple low level keyhook using JNI and all went
I'm getting acquanted with Core Animation and trying to implement simple movement along a
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).

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.