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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:29:55+00:00 2026-06-18T17:29:55+00:00

I would like to ask a question about inserting element into a binary tree,

  • 0

I would like to ask a question about inserting element into a binary tree, I need to insert element into table. However, I think I misunderstood about the pointer or something, that i am not able to create the binary tree.

The insert function is called by another file which contain the main function, therefore insert function is called regularly until all element is inserted. The insert function then calls the sub_insert to insert all the element. When i tried to read the binary tree, its empty. Can anyone suggest what to fix?

   typedef struct node * tree_ptr;
   /*typedef char* Key_Type; */

   struct node {
     Key_Type element; // only data is the key itself
     tree_ptr left, right;
     int depth;
   };

   struct table {
     tree_ptr head; // points to the head of the tree


   };
   struct table head,tail;



   struct node* newNode(Key_Type key){
      struct node* node=(struct node*)malloc(sizeof(struct node));
      node->element=key;
      node->left=NULL;
      node->right=NULL;
      return (node);
   }

   tree_ptr sub_insert(Key_Type key, struct node *node, Table table) {
      printf("reading... %s\n", key);

     if(node==NULL)
       return(newNode(key));

     else
     {
       if(key <= node->element){
         printf("inserted");
         node->left = sub_insert(key, node->left, table);
       }else{
         node->right = sub_insert(key, node->right, table);
       } 
         return node;
     }
   }

   Table insert(Key_Type key, Table table) {

        struct node* root=NULL;
        root=sub_insert(key, root, table);

        return table;
   }
  • 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-18T17:29:57+00:00Added an answer on June 18, 2026 at 5:29 pm

    Like Joachim said, your problem is that you’re always using NULL as a root node:

        struct node* root=NULL;
        root=sub_insert(key, root, table);
    

    I’m guessing, but it seems like you want to use table.head as the starting node:

        root=sub_insert(key, table.head, table);
    

    Dunno whether table is a pointer or not, so I just used dot notation.

    In any case, you absolutely need a valid root node before you traverse with sub_insert(), otherwise all your new nodes will just dangle in memory.

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

Sidebar

Related Questions

i would like to ask a question about @UsesJAXBContext annotation in jax-ws. I try
I would like to ask about the design of table based on it's editability
I would like to ask a question about correct way of canceling socket operations
I would like to ask a question about a new and large scale web
I would like to ask a question about submit process of an iOS application.
I would like to ask a theoritical question about how some web sites work.
I would like to ask something about query using mysql I have this table
Another day another question ;) This time I would like to ask you about
I would like to ask you a question about implementing mutual authentication with Kerberos,
I would like to ask a question about XML document validation against its corresponding

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.