Sequential Arithmetic Expression Evaluation
Expression evaluation is probably the most common text book example, and a popular application of those tree walks. Recently, while writing a parallel version for the same I had to quickly jot down a sequential version using stack and post order walk. The tree is implemented as an array that has its index, and index of left and right children. The reason to use it as an array is to facilitate parallel version for the same using multi-threading. Here is the code for the sequential version.
void postorder(INFO curr_index)
{
if(curr_index!=-1)
{
postorder(exp[curr_index].left);
postorder(exp[curr_index].right);
if(exp[curr_index].op==NIL)
s.item[++s.top]=exp[curr_index].c;
else{
switch(exp[curr_index].op)
{
case ADD:
result = s.item[s.top--] + s.item[s.top--];
s.item[++s.top] = result;
break;
case SUB:
result = - s.item[s.top--] + s.item[s.top--];
s.item[++s.top] = result;
break;
case MULT:
result = s.item[s.top--] * s.item[s.top--];
s.item[++s.top] = result;
break;
}/*switch*/
}/*else*/
}/*if*/
}/*postorder*/
About this entry
You’re currently reading “Sequential Arithmetic Expression Evaluation,” an entry on Technical Musings
- Published:
- March 11, 2008 / 8:35 pm
- Category:
- Graphs, Sequential, Tree
- Tags:
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]