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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:57:53+00:00 2026-06-10T09:57:53+00:00

I am following a book ,Problem Solving & Program Design in C, to learn

  • 0

I am following a book ,Problem Solving & Program Design in C, to learn C. In this book, they gave all necessary parts to build a binary search tree….
But, My implementation didn’t work. Here is insertion part;

void
add_to_t(tree_node_t *oldTreep, // input/output - binary search tree
        tree_element_t ele)       // input - element to add
{
    oldTreep = tree_insert(oldTreep, ele);
}
tree_node_t * tree_insert(tree_node_t *oldTreep, tree_element_t ele)
{
    if(oldTreep == NULL){
        oldTreep = TYPED_ALLOC(tree_node_t);
        strcpy(oldTreep->element.name, ele.name);
        strcpy(oldTreep->element.sName, ele.sName);
        oldTreep->element.seatClass = ele.seatClass;
        oldTreep->leftp = NULL;
        oldTreep->rightp = NULL;
    }
    else if (strcmp(oldTreep->element.name, ele.name)==0){
        /* duplicate key - no insertion */
    }
    else if (strcmp(oldTreep->element.name, ele.name)>0){
        oldTreep->rightp = tree_insert(oldTreep->rightp, ele);
    }
    else
    {
        oldTreep->leftp = tree_insert(oldTreep->leftp, ele);
    }
    return(oldTreep);

}

My scan_passenger funvtion(I am passing ele from result of this function call);

void scan_passenger(tree_element_t *pass)
{
    char passName[10], passSname[10];
    int classNum;
    printf("\nEnter the Name of passenger to add the binary search tree> ");
    scanf("%s", passName);
    printf("Enter the Surname of passenger to add the binary search tree> ");
    scanf("%s", passSname);
    printf("Enter the class number of passenger to add the binary search tree> ");
    scanf("%d", &classNum);
    strcpy(pass->name, passName);
    strcpy(pass->sName, passSname);
    pass->seatClass = classNum;
}

And my typdefs and headers if needs;

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define TYPED_ALLOC(type) (type *)malloc(sizeof (type))
typedef struct tree_element_s {
    char name[10];
    char sName[10];
    int seatClass;
}tree_element_t;

typedef struct tree_node_s {
    tree_element_t element;
    struct tree_node_s *leftp, *rightp;
}tree_node_t;

My problem is that it doesn’t create a root of binary search tree. When I try to add a new element to heap, it seems, it creates a new node. When I traced my code, it seems every instance of this functions returns NULL. I am trying to say every time when I call tree_insert, it goes first if statement(Thinks root is NULL)…
Sorry for my bad English. And I may made some mistake when talking about coding terminology(it may because, I returned to study C from that book after absence of 1 year.So I may mixed them)
Thanks in advance.

  • 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-10T09:57:54+00:00Added an answer on June 10, 2026 at 9:57 am

    In add_to_t, you update oldTreep, but since it is a local variable, the new value gets lost as soon as you leave the function.

    You could, e.g., return the new value of oldTreep to the caller, which updates his oldTreep with the return value

    tree_node_t
    add_to_t(tree_node_t *oldTreep, // input/output - binary search tree 
            tree_element_t ele)       // input - element to add 
    { 
        return tree_insert(oldTreep, ele); 
    } 
    
    ...
    
    myRootOfTheTree = add_to_t (myRootOfTheTree, element);
    

    Another solution is to always have one dummy element in the tree.

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

Sidebar

Related Questions

I have been working on the following problem from this book . A certain
Info: I'm currently trying to learn template metaprogramming (by following this book ). One
I'm trying to build a program to solve a problem in a text book
I'm following Agile web development with rails book. I have a problem. this is
I face the following problem function book($memberid, $classid){ if (!book){ // update the db
i'm trying to learn codeigniter (following a book) but don't understand why the web
i have following problem from book introduction algorithm second edition by MIT university problem
Writing a simple example from Odersky's book resulted in the following problem: // AbstractElement.scala
In the book Introduction to Algorithms, second edition, there is the following problem: Suppose
I'm just learning java and following a book. I have a program written via

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.