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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:46:31+00:00 2026-06-17T04:46:31+00:00

I have a binary tree program in C++ ,which parses a string of characters

  • 0

I have a binary tree program in C++ ,which parses a string of characters and then forms a binary tree depending on it.I have a problem with transmitting parameters to my functions . I tried reading tutorial on passing arguments to functions in c ,and changed my code ,but it doesn’t seem to work.I would like someone to help me fix the passing of arguments.

The header :

     #define NULL 0

 struct TreeEl {

   char inf;
struct TreeEl * st, * dr;

};

typedef struct TreeEl root;


root* add(root *r, char st[], int &pos, int &n);
root* create(root *r, char st[100], int n);

void visit(root*);
void preorder(root *r, void visit(root*));
void inorder(root *r, void visit(root*));
void postorder(root *r, void visit(root*));

And the code :

    #include "arbore_binar.h"
#include <stdio.h>



using namespace std;

void visit(root *r)
    {

    printf("Node %c",r->inf);

    }


root* add(root *r, char st[], int &pos, int &n)
{

int done=0;

do
{
    pos++;
    printf(" procesing character:,%c \n",st[pos]);
    switch (st[pos])
    {
        case '(':
        {
            add(r->st, st, pos, n);
            break;
        }
        case ')':
        {
            done=1;
            break;
        }
        case ',':
        {
            add(r->dr, st, pos, n);
            break;
        }
        case '$':
        {
            if (st[pos+1]==',')
                done=1;
            if (st[pos+1]==')')
                done=1;
            break;
        }
        default:
        {
            if ((st[pos]>=65)&&(st[pos]<=90))
             {
                 printf(" Added: ,%c \n" ,st[pos]);

                 root *p;
                 p = new root;
                 p->inf=st[pos];
                 p->st=NULL;
                 p->dr=NULL;
                 r=p;
                 if (st[pos+1]==',')
                    done=1;
                 if (st[pos+1]==')')

                    done=1;




             }
             else
                printf("Error,unknown character: %d ",st[pos]);
        }
    }
} while ((done==0)&&(pos<n));
return r;
}

root* create(root *r, char st[100], int n)
{

    int pos=-1;
    root* nod = add(r, st, pos, n);
    return nod;
}
void preorder(root *v, void visit(root*))
{
  if (v == NULL)

      return;


else {
visit(v);
preorder(v->st, visit);
preorder(v->dr, visit);
}
}
void inorder(root *v, void visit(root*))
{
  if (v == NULL) return;
else {
inorder(v->st, visit);
visit(v);
inorder(v->dr, visit);
}
}
void postorder(root *v, void visit(root*))
{
  if (v == NULL) return;
else {
postorder(v->st, visit);
postorder(v->dr, visit);
visit(v);
}
}



void print(root* x)
{
    printf("%c" , x->inf ,"  " );
}

int main()
{
//char a[100] = "A(B(J,$),C(X,D(E,F($,Y))))";
//char a[100] = "A(B,C(X,D(E,F($,Y))))";
char a[100] = "A(B(C(M,$),D),E(F(X,$),G($,Y)))";
root *r;
r=NULL;
r=create(r, a, 31);

printf("Preorder traversal:" );
preorder(r, print);
printf("\n");
printf("Inorder traversal: ");
inorder(r, print);
printf("\n");
printf("Postorder traversal: ");
postorder

(r, print);
printf(“\n”);

getchar();
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-17T04:46:33+00:00Added an answer on June 17, 2026 at 4:46 am

    The trouble you are facing is related to the passing of arguments through by-value and by-reference semantics. In C, all variables are passed by value. Pointers are often referred to as being passed “by-reference”, but they are not. Instead they enable the modification of other values, in a way which is similar to by-reference semantics.

    The issue is that you have the add function:

    void create(root *r, char st[100], int n);
    

    create takes a root* as its first argument. Thus, it can make changes to the memory pointed to by this parameter. After execution of the function, the memory pointed to by r may be different, but r itself will have the same value – that is, the pointer variable r will always point to the same place.

    Why is this relevant? Look at your main function and note that the initial value of r is NULL. After passing r to the create function, r must still be NULL. Therefore, you never have a pointer to the tree at all!

    This probably does not seem like the case to you because in the create function, you assign a value to r (specifically the line r=p). Unfortunately, that line only modifies the function-local copy of r, so the changes are not reflected in main.

    How could you fix this? Instead of trying to modify the main-local copy of r within the create function, you could change the create function to return root* instead of void. Then, in main, change your calling of the function to this:

    r = create(r, a, 31);
    

    Provided create returns a pointer to the root of the tree, you will now be able to maintain a reference to your tree everywhere, and it should work properly with your other functions.

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

Sidebar

Related Questions

i need some help with this problem. I have a binary tree stucture which
I have a binary tree of functions and terminal values. I'd like to print
Say I have a binary tree which contains pointers at each node going to
Im having a problem with python.. I have a binary tree node type: class
I have a binary tree based mathematical expression parser I built, which works great
I have a Binary-Tree class template with several public and private member functions. When
Hello I have a small binary tree program in c that I'm working on
So I have a completed program that adds values into a binary tree. It
I have two functions in a class that creates a binary tree: void Btree::insertNode(node*
I have a binary tree, that I am searching: TreeNode<Vessel*>* node = this->tree_->search(PotatoFace); string

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.