Creating a tree with double pointer and structure

I am trying to create a tree. The basic idea is whenever I see '+', '-', '/' or '*', that variable become the parent of the main tree. For example, if user input 1 + 1 , the tree should look

1
2
  +
1   1

Please take a look at my insert function. I have written a sudo code but I am not sure how to code it. The am not comfortable using double pointer. If you can, please explain them line by line so I can follow. Thanks

Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  
%{
#include <stdio.h>
#define true 1
#define false 0
int lookahead;
int lookahead1;
int error;
int count;
%}

%%
[(] {return(111);}
[)] {return(222);}
[0-9] {return(333);}
[a-zA-Z] {return(444);}
[\+] {return(33);}
[\*] {return(44);}
[\/] {return(55);}
[\-] {return(66);}
[ \n\t] {return(0);}

%%
struct node
{
	char  a;
	struct node * left;
	struct node * right;
};


struct node *root, * current;
void insert( struct node ** tree, struct node *item)
{

	printf("%c", item -> a);
//	printf("%c", &(*tree) -> left);
	if (!(*tree))
	{
		*tree = item;
		return;
	}
	if ( item-> a == '+' || item-> a == '-' || item->a == '*' || item->a == '/' )
	{
		item -> left = tree;
		break;	
			
	/*	struct node ** temp;
		temp= tree;
		*tree = item;
		(*tree) -> left = *temp;
	*/		
		//insert (&(*tree) -> left, item);
	}
	else if ( (*tree) -> left == NULL )
	{
		insert (&(*tree) -> left, item);
	}
	else
	{
		insert (&(*tree) -> right, item);
	}
	
}
Goal ()
{
	return Expr(); 
}

Expr()
{
	if ( Term() )
		return ExprP();
	else
		return false;
}

ExprP()
{
	if ( lookahead == 33 || lookahead == 66 )
	{	
		current = malloc(sizeof(struct node));
		current -> a = yytext[0];
		current -> left = NULL;
		current -> right = NULL;
		insert(&root, current);		
		lookahead = yylex();
		if ( Term() )
			return ExprP();
		else 
			return false;
	}
	else if ( lookahead == 222 ||  lookahead == 0  )
		return true;
	else
		return false;
}

Term()
{
	if (Factor())
	{
		return TermP();
	}
	else
		return false;
}

TermP()
{
	if ( lookahead == 44 || lookahead == 55 )
	{
		current = malloc(sizeof(struct node));
		current -> a = yytext[0];
		current -> left = NULL;
		current -> right = NULL;
		insert(&root, current);		
		lookahead = yylex();
		if ( Factor() )
		{
			return TermP();
		}
		else
			return false;	
	}
	else if ( lookahead == 33 || lookahead == 222 || lookahead == 0 || lookahead == 66 )
		return true;
	else
		return false;	
}

Factor()
{
	if (lookahead == 111 )
	{	
		lookahead = yylex();
		if ( !Expr() )
			return false;
		if ( lookahead != 222 )
			return false;
		lookahead = yylex();

			return true;		
	}
	else if ( lookahead == 333 )
		{
		current = malloc(sizeof(struct node));
		current -> a = yytext[0];
		current -> left = NULL;
		current -> right = NULL;
		insert(&root, current);
			lookahead = yylex();
			return true;
		}
	else
		return false;
}

int yywrap(void)
{
return 1;
}


void printout(struct node * tree, int h) {
   if(tree->right) printout(tree->right, h+1);
	int i ;
	for ( i = 0; i < h ; i++)
		printf("   ");
   printf("%c\n",tree->a);
   if(tree->left) printout(tree->left, h+1);
}



int main()
{
	root = NULL; 
	lookahead = yylex();
	if ( Goal() )
		printf ("\n\nThe string you entered successfully accepted\n\n");
	else
	{
		printf("\n\nThe string you entered failed to accept\n\n");
		printf("%s\n", yytext);
	}

	printout(root, 0);

return 0;
}
Topic archived. No new replies allowed.