public static TNode<Character> buildExpTree(String postfixExp)
{
AStack<TNode<Character>> stack=new AStack<TNode<Character>>();
TNode<Character> newNode,newLeft,newRight;
char token;
int i=0;
int n=postfixExp.length();
while(i<n)
{
while(postfixExp.charAt(i)==' '||postfixExp.charAt(i)=='\t')
i++;
if(i==n)
break;
token=postfixExp.charAt(i);
i++;
if(token=='+'||token=='-'||token=='*'||token=='/')
{
newRight=stack.pop();
newLeft=stack.pop();
newNode=new TNode<Character>(token,newLeft,newRight);
stack.push(newNode);
}
else
{
newNode=new TNode<Character>(token);
stack.push(newNode);
}
}
if(!stack.isEmpty())
{
return stack.pop();
}
else
return null;
}
构建表达式树
最新推荐文章于 2021-12-20 22:48:03 发布