///习题6.2 由二叉树得到中缀表达式,添括号
int comp (char opt1,char opt2 )
{
if (opt1 == '*' || opt1 == '/')
{
if (opt2 == '*' || opt2 == '/' )
return 0 ;
else if (opt2 == '+' || opt2 == '-' )
{
return 1 ;
}
else
return 2;
}
else
{
if (opt2 == '*' || opt2 == '/' )
return 0 ;
else if (opt2 == '+' || opt2 == '-' )
{
return 0;
}
else
return 2;
}
}
void print(BiTree T)
{
if (T != null)
{
if (T -> lchild == null && T -> rchild == null)
{
cout<< T -> data ;
}
else
{
if (comp (T -> data , T -> lchild -> data) == 1)
{
cout <<"(" ;
}
print (T -> lchild ) ;
if (comp (T -> data , T -> lchild -> data) == 1)
{
cout <<")" ;
}
cout << T -> data ;
if (comp (T -> data , T -> rchild -> data) == 1)
{
cout <<"(" ;
}
print (T -> rchild ) ;
if (comp (T -> data , T -> rchild -> data) == 1)
{
cout <<")" ;
}
}
}
}