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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:10:14+00:00 2026-06-01T00:10:14+00:00

#include<iostream> using namespace std; /*main idea is to construct ordered statistic tree,which is similar

  • 0
#include<iostream>
using namespace std;
/*main  idea  is to construct ordered statistic tree,which is  similar of
binary search tree,width addition of  one key,which shows us it's rank in given  
tree,for this i introduced additional one key-rank
*/
struct node
{
    int val;
    node *left,*right;
    int rank;
    node(int t) { val=t;left=right=NULL;}


};
node *root;
void insert(node *p,int ele)
{
    if(p==NULL){
        p=new node(ele);
        return ;


    }
    else if(ele<p->val)
    {
        insert(p->left,ele);

    }
    else if(ele>p->val)
    {
        insert(p->right,ele);

    }

}
void inorder (node *p)
{
    if(p!=NULL){ inorder(p->left);
    cout<<p->val<<" "<<p->rank;
    inorder(p->right);
    }

}
int count_node(node *t)
{
    int sum=0;
    if(t==NULL) return 0;
    else sum=1;
    if(t->left) sum+=count_node(t->left);
    if(t->right) sum+=count_node(t->right);
    return sum;
}

int main()
{
    root=NULL;
    root->rank=0;
    insert(root,26);
    insert(root,17);
    insert(root,41);
    insert(root,14);
    insert(root,30);
    insert(root,21);
    insert(root,47);
    insert(root,10);
    insert(root,16);
    insert(root,28);
    insert(root,38);
    insert(root,35);
    insert(root,39);
    insert(root,19);
    insert(root,21);
    insert(root,20);
    insert(root,7);
    insert(root,12);
    insert(root,3);
    inorder(root);

    return 0;
}

This code causes an overflow, but I do not understand why, because I have constructed the constructor properly.

  • 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-01T00:10:15+00:00Added an answer on June 1, 2026 at 12:10 am

    The problem is:

    root=NULL;
    root->rank=0;
    

    this cause undefined behavior because you dereference a NULL pointer. Anything can happen.

    Also:

    void insert(node *p,int ele)
    {
        if(p==NULL){
            p=new node(ele);
            return ;
    
    
        }
        //...
    }
    

    This doesn’t modify the original pointer. If you call insert on a NULL pointer, it will be NULL when the function returns. You need to pass it by reference:

    void insert(node *& p,int ele)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
#include <iostream> using namespace std; int main (int argc, char* const argv[]) { int
#include <iostream> using namespace std; int main(void){ int size = -2; int* p =
Example: #include <iostream> using namespace std; int main() { wchar_t en[] = LHello; wchar_t
The following code #include <iostream> using namespace std; int main() { const char* const
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
#include <iostream> using namespace std; int main(int argc, char* argv[]) { int i1 =

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.