后缀表达式树的Java程序

时间:2016-03-04 23:24:02

标签: java tree postfix-notation

我最近一直在研究java中的树。我在sanfoundry.com上发现这个代码对表达式树来说非常棒。它接受一个前缀然后打印出前缀表达式作为中缀和后缀的外观,然后最终输出答案。我的问题是我试图找出如何简化它只是采取后缀并打印出答案。因此,它不是读取前缀并完成所有操作,而是只读取后缀并打印出答案。以下是我找到的代码。这是一个简单的修复,只是让它做后缀吗?或者更难的事情?

private void YourRadGridView_AddingNewDataItem(object sender, 
    Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
    YourObjectModel newItem = new YourObjectModel();
    newItem.Date = DateTime.Now;
    e.NewObject = newItem;
}

这是主要方法。

class ExpressionTree
{
/** class TreeNode **/
class TreeNode
{    
    char data;
    TreeNode left, right;

    /** constructor **/
    public TreeNode(char data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
} 

/** class StackNode **/
class StackNode
{
    TreeNode treeNode;
    StackNode next;

    /** constructor **/
    public StackNode(TreeNode treeNode)
    {
        this.treeNode = treeNode;
        next = null;
    }
}

private static StackNode top;

/** constructor **/
public ExpressionTree()
{
    top = null;
}

/** function to clear tree **/
public void clear()
{
    top = null;
}

/** function to push a node **/
private void push(TreeNode ptr)
{
    if (top == null)
        top = new StackNode(ptr);
    else
    {
        StackNode nptr = new StackNode(ptr);
        nptr.next = top;
        top = nptr;
    }
}

/** function to pop a node **/
private TreeNode pop()
{
    if (top == null)
        throw new RuntimeException("Underflow");
    else
    {
        TreeNode ptr = top.treeNode;
        top = top.next;
        return ptr;
    }
}

/** function to get top node **/
private TreeNode peek()
{
    return top.treeNode;
}

/** function to insert character **/
private void insert(char val)
{
    try
    {
        if (isDigit(val))
        {
            TreeNode nptr = new TreeNode(val);
            push(nptr);
        }
        else if (isOperator(val))
        {
            TreeNode nptr = new TreeNode(val);
            nptr.left = pop();
            nptr.right = pop();
            push(nptr);
        }
    }
    catch (Exception e)
    {
        System.out.println("Invalid Expression");
    }
}

/** function to check if digit **/
private boolean isDigit(char ch)
{
    return ch >= '0' && ch <= '9';
}

/** function to check if operator **/
private boolean isOperator(char ch)
{
    return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}

/** function to convert character to digit **/
private int toDigit(char ch)
{
    return ch - '0';
}

/** function to build tree from input */
public void buildTree(String eqn)
{
    for (int i = eqn.length() - 1; i >= 0; i--)
        insert(eqn.charAt(i));
}

/** function to evaluate tree */
public double evaluate()
{
    return evaluate(peek());
}

/** function to evaluate tree */
public double evaluate(TreeNode ptr)
{
    if (ptr.left == null && ptr.right == null)
        return toDigit(ptr.data);
    else
    {
        double result = 0.0;
        double left = evaluate(ptr.left);
        double right = evaluate(ptr.right);
        char operator = ptr.data;

        switch (operator)
        {
        case '+' : result = left + right; break;
        case '-' : result = left - right; break;
        case '*' : result = left * right; break;
        case '/' : result = left / right; break;
        default  : result = left + right; break;
        }
        return result;
    }
}

/** function to get postfix expression */
public void postfix()
{
    postOrder(peek());
}

/** post order traversal */
private void postOrder(TreeNode ptr)
{
    if (ptr != null)
    {
        postOrder(ptr.left);            
        postOrder(ptr.right);
        System.out.print(ptr.data);            
    }    
}

/** function to get infix expression */
public void infix()
{
    inOrder(peek());
}

/** in order traversal */
private void inOrder(TreeNode ptr)
{
    if (ptr != null)
    {
        inOrder(ptr.left);
        System.out.print(ptr.data);
        inOrder(ptr.right);            
    }    
}

/** function to get prefix expression */
public void prefix()
{
    preOrder(peek());
}

/** pre order traversal */
private void preOrder(TreeNode ptr)
{
    if (ptr != null)
    {
        System.out.print(ptr.data);
        preOrder(ptr.left);
        preOrder(ptr.right);            
       }    
   }
 }

1 个答案:

答案 0 :(得分:0)

Postfix是一种线性表示法。你根本不需要一棵树来评估它,只是一堆。你是从错误的地方开始的。