Converting Infix to Postfix with Binary Tree

I am trying to do an infix for a postfix converter. I already searched for the code on google but most of the algorithms were glassy or used a lot of regex or difficult to understand but I want to do it with a binary tree.

here is what i already did:

Node class:

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 the simple auto property.

and my BST class which has Insert, Postfix, Prefix and Infix methods (I also have search, delete, etc., but we don't need them for my question)

BST Class:

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 working for numbers like:
If we want to write Inorder, Preorder and post order below the tree

enter image description here

Pre-order: 5, 3, 2, 4, 7, 6, 12
Afterword: 2, 4, 3, 6, 12, 7, 5
Inorder: 2, 3, 4, 5, 6, 7, 12

The 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 an infix for a postfix converter, but I don’t know how to implement the insert method for this and how to handle the operators and operands in order. and another problem is parentheses.
would be grateful if it could help me with some code.

Example with a tree:

enter image description here

Mathblog has a postfix converter prefix, you can check it. http://www.mathblog.dk/tools/infix-postfix-converter/

+3


source to share


1 answer


It looks like the easiest way to convert an expression from infix to postfix notation is to use the standard stack-based algorithm (it has linear time complexity, so it's optimal) and then build a tree (building a tree from postfix expression is simple because all the operators are found in the right order).



0


source







All Articles