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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:40:04+00:00 2026-06-18T13:40:04+00:00

hello all I really appreciate any help, thanks. first off I am building a

  • 0

hello all I really appreciate any help, thanks.

first off I am building a binary tree, but cant seem to build it. In the recursive process the insertHelper seems to recognize a larger root in first iteration, but then after does not continue build left and right and nodes connected to it. see
in c++

header:

struct Node {
public:
    int weight;
    Node *right;
    Node *left;
};

class Btree {
    Node *root;
    int insize;
    int taxsize;
    public:
        Btree();
        void insertOne(Node *);
        void insertHelper(Node *, Node *);
        void printTree();
        void ptHelper(Node *);  
};

class:

Btree::Btree() {
    this -> root = NULL;
    this -> insize =0;
    this -> taxsize =0;
}
void Btree::insertOne( Node *n ) {
        if(root==NULL) {
            root = n;
        }else {
            Node *burr;
            burr = root;
            insertHelper( n, burr);
        }
    }
void Btree::insertHelper( Node *n, Node *curr ) {
    if(curr==NULL) {
        curr = n;
    }
    if(n->weight > curr->weight) {
        insertHelper(n, curr->right);
    }
    if(n->weight < curr->weight) {
        insertHelper(n, curr->left);
    }
}
void Btree::printTree() {
    Node *current;
    current = root;
    ptHelper(current);

}
void Btree::ptHelper(Node *m){
    if(m != NULL) {
        cout<<" "<<m->weight<<" ";
        if(m->left != NULL) {
            ptHelper(m->left);
        }
        if(m->right != NULL) {
            ptHelper(m->right);
        }
    }else {
        return;
    }
}

main:

int main()
{
    Btree joe;
    int insize;
    int taxsize;
    cin >> insize;
    for(int i=0; i<insize; i++) {
        int tmp;
        cin >> tmp;
        Node *diamond = new Node();
        diamond->weight = tmp;
        joe.insertOne(diamond);
    }
    joe.printTree();
    return 0;
}
  • 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-18T13:40:05+00:00Added an answer on June 18, 2026 at 1:40 pm

    You didn’t update references to left and right.
    Try this:

    void Btree::insertHelper( Node *n, Node *curr ) {
    
        if(curr==NULL) {
            curr = n;
        }
        //My debug output
        //cout<<"InsertHelper "<<n->weight<< " to " << curr->weight <<" \n";
    
        if(n->weight > curr->weight) {
            if (curr->right == NULL) {
                curr->right = n;
            } else {
                insertHelper(n, curr->right);
            }
        }
        if(n->weight < curr->weight) {
            if (curr->left == NULL) {
                curr->left = n;
            } else {
                insertHelper(n, curr->left);
            }
        }
    }
    

    Note:
    It’s actually better to change interface to Btree::insertOne( int n ) . Then your can manage lifecycle of Nodes inside the BTree

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

Sidebar

Related Questions

I would really appreciate to get your help on this, as I can't seem
hello all i really need your help its my final project in university i
Hello all i'm turning objects on (adding the class .active) and off on a
Hello to all, Double d = 1.000000000000000000000000000000001; System.out.println(d); The code above prints 1.0 but
Hello all im getting really frustrated with my button, how do i make it
First of all, I've never really used APIs before, and I've never used the
Hello All I am using infragsitics tree view control.I have one stored procedure that
Hello all at StackOverflow.. Reasonably new to this site, but I have had nothing
The form and script are show below and I appreciate all help on this.
Hello been looking around online for this for awhile and would really appreciate some

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.