[英]How to evaluate binary tree (inorder)?
I am trying to evaluate a binary tree (inorder). 我正在尝试评估二叉树(顺序)。 The result always remains the same or giving an unexpected answer.
结果始终保持不变或给出了意外的答案。 I do not know where the problem is, can anyone help me?
我不知道问题出在哪里,有人可以帮助我吗? First I have converted postfix expression to expression tree than evaluate expression tree.
首先,我将后缀表达式转换为表达式树,而不是评估表达式树。 I shall be very thankful.
我将非常感激。
The result is unexpected when I run this program. 当我运行该程序时,结果是意外的。 This the header file.
这个头文件。
#define POST2EXPTREE_H_INCLUDED
#define MAX 100
struct node
{
char ch;
struct node *left;
struct node *right;
} *stack[MAX];
typedef struct node node;
void push(node *str);
node *pop();
void convert(char exp[]);
void display(node *temp);
#endif
#include <stdio.h>
#include <stdlib.h>
#include"post2expTree.h"
#define SIZE 100
int top = -1;
void push(node *str)
{
if (top >= MAX-1)
printf("Stack is Full ");
else
{
stack[top] = str;
top++;
}
}
node *pop()
{
node *exp;
if (top < 0)
printf("Stack is Empty ");
else
exp = stack[--top];
return exp;
}
void convert(char exp[])
{
node *op1, *op2;
node *temp;
int i;
for (i=0;exp[i]!='\0';i++)
if (exp[i] >= 'a'&& exp[i] <= 'z'|| exp[i] >= 'A' && exp[i] <= 'Z' ||isalnum(exp[i]))
{
temp = (node*)malloc(sizeof(node));
temp->ch = exp[i];
temp->right = NULL;
temp->left = NULL;
push(temp);
}
else if (exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/' || exp[i] == '%'
|| exp[i] == '^')
{
op1 = pop();
op2 = pop();
temp = (node*)malloc(sizeof(node));
temp->ch = exp[i];
temp->right = op1;
temp->left = op2;
push(temp);
}
}
void display(node *temp)
{
if (temp != NULL)
{
display(temp->left);
printf("%c", temp->ch);
display(temp->right);
}
}
int evaluate(node *temp)
{
int left,right,value;
if ((temp->ch) >= 0 || (temp->ch <=9))
{
return temp->ch;
}
else
{
left = evaluate(temp -> left);
right = evaluate(temp -> right);
switch(temp->ch)
{
case '+':
value = left + right;
break;
case '-':
value = left - right;
break;
case '*':
value = left * right;
break;
case '/':
value = left / right;
break;
case '%':
value = left % right;
break;
case '^':
value = left ^ right;
break;
}
temp->ch = value;
}
return value;
}
int top = -1; void push(node *str) { if (top >= MAX-1) printf("Stack is Full "); else { stack[top] = str; top++; } }
top
is initialized to -1
, and the very first push()
wrongly accesses stack[-1]
; top
被初始化为-1
,并且第一个push()
错误地访问stack[-1]
; correct that by changing the else
block to stack[++top] = str;
else
块更改为stack[++top] = str;
来纠正该错误stack[++top] = str;
. node *pop() { node *exp; if (top < 0) printf("Stack is Empty "); else exp = stack[--top]; return exp; }
top
is decremented before accessing the stack
off by one; top
正在访问之前递减stack
通过一个断开; correct that by changing the else
block to exp = stack[top--];
else
块更改为exp = stack[top--];
来更正该exp = stack[top--];
. int evaluate(node *temp) { int left,right,value; if ((temp->ch) >= 0 || (temp->ch <=9)) { return temp->ch;
Here you forgot that you store the operands as single digits '0'
to '9'
, not as values 0
to 9
. 在这里,您忘记了将操作数存储为一位数字
'0'
至'9'
,而不是值0
至9
。 Correct that by changing to: 通过更改为:
int left, right, value = temp->ch-'0'; if (value >= 0 && value <= 9) { return value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.