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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:06:22+00:00 2026-05-31T06:06:22+00:00

I have a book structure that contains a book title, url, and student name.

  • 0

I have a book structure that contains a book title, url, and student name. I want to insert this data into a binary tree. I have already coded these functions but am not able to get an output when trying to print the tree.

1) Does my insert function look correct?

2) Does my printTree function look correct?

3) When I call on the printTree function I am passing pointer. If I want to just print from the root down what do I pass as the root?

Relevant code is as follows:

typedef struct book {
    char title[75];
    char url[75];
    char student[50];
} BOOK;

struct tree_node {
    BOOK data;
    struct tree_node *left;
    struct tree_node *right;
};


    int insert_book() {     // insert an new book

        BOOK contact;

        cout << endl << "@Inserting Book.........." << endl;
        cout << "Enter the Book Title: ";
        cin.ignore();
        cin.getline(contact.title, 75);
        cout << "Enter the Book URL: ";
        cin.getline(contact.url, 75);
        cout << "Enter the Student: ";
        cin.getline(contact.student, 50);


        void insert(BOOK, tree_node); 

        return 0;

    }


    struct tree_node * insert(BOOK contact, struct tree_node* node) {

        if(node == NULL)
            return(create_node(contact));

        else if(strcmp(contact.student, node->data.student) <= 0)
            node->left = insert(contact, node->left);

        else if(strcmp(contact.student, node->data.student) >= 0)
            node->right = insert(contact, node->right);

        return (node);
    }

    struct tree_node* create_node(BOOK contact) {

        struct tree_node* node = new(struct tree_node);

        node->data = contact;
        node->left = NULL;
        node->right = NULL;

        return(node);
    } 

    void printTree(struct tree_node* node) { 
        if (node == NULL)
            cout << "Empty!" << endl;
        if(node != NULL) {
            printTree(node->left); 
            printf("%d ", node->data); 
            printTree(node->right);
        }
    }
  • 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-05-31T06:06:23+00:00Added an answer on May 31, 2026 at 6:06 am
    1. Your insert function looks correct, but using both <= and >= is a bit confusing. If the <= condition is true, then only > is the possibility left for the else case. Also I assume your call to insert() was a type-o since you are passing BOOK, and not an instance.
    2. Your print function logic looks correct, but the printf() format specifier is not. you should have something like: printf(“%s”, node->data.title);
    3. The print function you implemented is called “in order traversal”. You should look up level order traversal to see how to print from the root down.

    Also a side note, you are using c++, but coding in a very c style manner. I’d consider reorganizing the code for readability.

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

Sidebar

Related Questions

I have a postgresql database that has this column structure: Author id name Book
I have come to know from book that for declaring a structure variable it
I have a data-structure such that: SecurityPolicy 1<---* SecurityPolicyRule Therefore, a SecurityPolicy can have
I have a core data structure of Books and Bundles. A book can belong
This is a pretty strange problem. I have a book that I have been
I have a JSON structure; { books: [ {id: book1, title: Book One}, {id:
I have read O'Reilly book, in that I came to know this get-put principle
I have an NSDictionary that we will say is like this: key: value: name
I have a book, Essential ActionScript 3 (O'Reilly), to learn about using that language.
I have a book class, then a novel- and a science book class that

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.