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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:53:01+00:00 2026-06-07T09:53:01+00:00

//binary_tree.h file typedef struct node node; struct node { node():left(0), right(0), value(-1){}; ~node(){if(left) delete

  • 0
//binary_tree.h file
typedef struct node node;

struct node
{   node():left(0), right(0), value(-1){};
    ~node(){if(left) delete left; if(right) delete right;};
    node *left;
    node *right;
    int value;
};

inline void insert_node(node **root, node *new_node)
{
    assert(new_node != NULL);
    if(*root == NULL)
    {
        *root = new_node;
    }
    else
    {
        node *itr = *root;
        while(1)
        {
            if(itr->value > new_node->value)
                itr = itr->left;
            else
                itr = itr->right;
            if(!itr)
            {
                itr = new_node;
                break;
            }
        }
    }
}

inline void inorder_print(node *root)
{
    if(!root) return;
    inorder_print(root->left);
    printf("%d\n", root->value);
    inorder_print(root->right);
}

//main.cpp file
#include "binary_tree.h"

int main()
{
    node *node1 = new node();
    node *node2 = new node();
    node *node3 = new node();
    node *node4 = new node();
    node *node5 = new node();

    node1->value = 5;
    node2->value = 10;
    node3->value = 3;
    node4->value = 1;
    node5->value = 4;   

    node *binary_tree = NULL;

    insert_node(&binary_tree, node1);
    insert_node(&binary_tree, node2);
    insert_node(&binary_tree, node3);
    insert_node(&binary_tree, node4);
    insert_node(&binary_tree, node5);

    assert(binary_tree != NULL);
    inorder_print(binary_tree);

    return 0;
}

I have a very simple program and I want to create a binary tree and print the tree.
Hoever the code segment shown below doesn’t change the tree structure.

        node *itr = *root;
        while(1)
        {
            if(itr->value > new_node->value)
                itr = itr->left;
            else
                itr = itr->right;
            if(!itr)
            {
                itr = new_node;
                break;
            }
        }

inorder_print function always prints ‘5’

The problem is to use ‘itr’ variable. I don’t really see how I could do this without using a local variable nor changing the pointer to the root.

  • 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-07T09:53:03+00:00Added an answer on June 7, 2026 at 9:53 am

    Your insertion routine will only insert a node into the root.

            if(!itr)
            {
                itr = new_node;
                break;
            }
    

    Since itr is a local variable, new_node did not actually get inserted. You can correct this by making itr a pointer to a pointer like root.

        node **itr = root;
        while(1)
        {
            if((*itr)->value > new_node->value)
                itr = &(*itr)->left;
            else
                itr = &(*itr)->right;
            if(!*itr)
            {
                *itr = new_node;
                break;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary tree where each node can have a value. I want
I have a binary tree class that is created with a root node and
#include <fstream> #include <iostream> using namespace std; bool find_in_file(char*); void insert_in_file(char*); inline bool isNull(char*
void BinaryTree::InitializeFromFile(string Filename){ ifstream inFile; treenode* Freq[256]; inFile.open(Filename.c_str(), fstream::binary); if(inFile.fail()){ cout<<Error in opening file
Say I have a binary tree which contains pointers at each node going to
I can't figure out how to write a Binary Search Tree to file recursively.
I am trying to build a mathematical model of the availability of a file
I have an app that loads records from a binary log file and displays
The implementation of file compressors that I saw was always compressing arrays of bytes.
So for my assignment, I am supposed to implement a Node class that just

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.