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;
}
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:
createtakes aroot*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 byrmay be different, butritself will have the same value – that is, the pointer variablerwill always point to the same place.Why is this relevant? Look at your
mainfunction and note that the initial value ofrisNULL. After passingrto thecreatefunction,rmust still beNULL. Therefore, you never have a pointer to the tree at all!This probably does not seem like the case to you because in the
createfunction, you assign a value tor(specifically the liner=p). Unfortunately, that line only modifies the function-local copy ofr, so the changes are not reflected inmain.How could you fix this? Instead of trying to modify the
main-local copy ofrwithin thecreatefunction, you could change thecreatefunction to returnroot*instead ofvoid. Then, inmain, change your calling of the function to this:Provided
createreturns 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.