简体   繁体   English

使用二叉树将中缀转换为后缀

[英]Convert Infix to Postfix with Binary Tree

I'm trying to make an infix to postfix converter.我正在尝试为后缀转换器制作中缀。 I have already searched for code in Google but most of the algorithms were with stack or using lot's of regular expression or hard to understand, but I want to make this with Binary Tree.我已经在 Google 中搜索过代码,但大多数算法都使用堆栈或使用大量正则表达式或难以理解,但我想用二叉树来实现。

here is what i have already did:这是我已经做过的:

Class Node:类节点:

public class Node
{
    private object element;
    private Node left;
    private Node right;
    private Node parent;

    public Node()
    {
    }

    public Node(object x, Node r, Node l)
    {
        element = x;
        right = r;
        left = l;
    }

    public object GetElement()
    {
        return element;
    }

    public void SetElement(object x)
    {
        element = x;
    }

    public Node GetLeft()
    {
        return left;
    }

    public void SetLeft(Node l)
    {
        left = l;
    }

    public Node GetRight()
    {
        return right;
    }

    public void SetRight(Node r)
    {
        right = r;
    }

    public Node GetParent()
    {
        return parent;
    }

    public void SetParent(Node p)
    {
        parent = p;
    }
}

Sorry for using get/set instead of simple auto property.很抱歉使用 get/set 而不是简单的自动属性。

and my BST class that has Insert, Postfix, Prefix and Infix method (i have also writen search, deletemin, etc but we don't need them for my question)和我的 BST 类,它有 Insert、Postfix、Prefix 和 Infix 方法(我也写了 search、deletemin 等,但我的问题不需要它们)

BST Class: BST类:

public class BinarySearchTree
{
    //Node r: root.
    public void Insert(Node r, Node p, object x)
    {
        if (r == null)
        {
            r = new Node(x, null, null);
            r.SetParent(p);
            if ((int)x < (int)p.GetElement())
                p.SetLeft(r);
            else if ((int)x > (int)p.GetElement())
                p.SetRight(r);
        }
        if ((int) x < (int) r.GetElement())
            Insert(r.GetLeft(), r, x);
        else if ((int) x > (int) r.GetElement())
            Insert(r.GetRight(), r, x);
    }

    public void PreOrder(Node p)
    {
        if (p != null)
        {
            Console.WriteLine(p.GetElement());
            PreOrder(p.GetLeft());
            PreOrder(p.GetRight());
        }
    }

    public void Inorder(Node p)
    {
        if (p != null)
        {
            Inorder(p.GetLeft());
            Console.WriteLine(p.GetElement());
            Inorder(p.GetRight());
        }
    }

    public void Postorder(Node p)
    {
        if (p != null)
        {
            Postorder(p.GetLeft());
            Postorder(p.GetRight());
            Console.WriteLine(p.GetElement());
        }
    }
}

My insert method is work for numbers for example:我的插入方法适用于数字,例如:
If we want to write Inorder, Preorder and post order of below tree如果我们想写下树的中序、预序和后序

在此处输入图像描述

Preorder: 5, 3, 2, 4, 7, 6, 12预购:5、3、2、4、7、6、12
Postorder: 2, 4, 3, 6, 12, 7, 5后序:2、4、3、6、12、7、5
Inorder: 2, 3, 4, 5, 6, 7, 12顺序:2、3、4、5、6、7、12

Main method for this example:本例的主要方法:

private static void Main()
{
    BinarySearchTree bst = new BinarySearchTree();
    Node r = new Node(5, null, null);
    bst.Insert(r, null, 3);
    bst.Insert(r, null, 7);
    bst.Insert(r, null, 4);
    bst.Insert(r, null, 12);
    bst.Insert(r, null, 2);
    bst.Insert(r, null, 6);
    bst.Inorder(r);
    Console.WriteLine();
    bst.Postorder(r);
    Console.WriteLine();
    bst.PreOrder(r);
}

now I want to make a infix to postfix converter but don't know how to implement insert method for that, and how to handle operator and operands in order.现在我想为后缀转换器制作一个中缀,但不知道如何为此实现插入方法,以及如何按顺序处理运算符和操作数。 and another problem is parentheses.另一个问题是括号。
would be grateful if help me with some code.如果帮助我提供一些代码,将不胜感激。

An example with tree:树的一个例​​子:

在此处输入图像描述

mathblog has a infix to postfix converter, you can check yours with it. mathblog 有一个中缀到后缀转换器,你可以用它检查你的。
http://www.mathblog.dk/tools/infix-postfix-converter/ http://www.mathblog.dk/tools/infix-postfix-converter/

看起来将表达式从中缀转换为后缀表示法的最简单方法是使用标准的基于堆栈的算法(它具有线性时间复杂度,因此它是最优的)然后构建一棵树(从后缀表达式构造一棵树很简单因为所有运算符的顺序都正确)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM