<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: thiru2218</title>
    <description>The latest articles on DEV Community by thiru2218 (@thiru2218).</description>
    <link>https://dev.to/thiru2218</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F627291%2Fb52b4c2d-a68f-4096-abf9-ce09c3bc8978.png</url>
      <title>DEV Community: thiru2218</title>
      <link>https://dev.to/thiru2218</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thiru2218"/>
    <language>en</language>
    <item>
      <title>Expression Tree in Python</title>
      <dc:creator>thiru2218</dc:creator>
      <pubDate>Fri, 07 May 2021 15:06:11 +0000</pubDate>
      <link>https://dev.to/thiru2218/expression-tree-in-python-2a4h</link>
      <guid>https://dev.to/thiru2218/expression-tree-in-python-2a4h</guid>
      <description>&lt;p&gt;An &lt;code&gt;Expression tree&lt;/code&gt; is a binary tree in which the &lt;strong&gt;operators&lt;/strong&gt; are stored in the &lt;strong&gt;interior nodes&lt;/strong&gt; and the &lt;strong&gt;operands are stored in the exterior nodes&lt;/strong&gt; which are the &lt;strong&gt;leaves&lt;/strong&gt;. An expression tree can be used to evaluate the expression or for converting an &lt;code&gt;infix&lt;/code&gt; expression to either &lt;code&gt;prefix&lt;/code&gt; or &lt;code&gt;postfix&lt;/code&gt; notation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Infix&lt;/code&gt;&lt;/strong&gt;: An expression where the operator is placed in between the operands. \&lt;br&gt;
&lt;strong&gt;Ex&lt;/strong&gt;: (A+B) * (C-D)  \&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Prefix&lt;/code&gt;&lt;/strong&gt;: An expression where the operator is in the expression before the operands. \&lt;br&gt;
&lt;strong&gt;Ex&lt;/strong&gt;: *+AB-CD&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The expression tree structure is dependent on which the &lt;em&gt;order of the operators is evaluated&lt;/em&gt;. The operator in every internal node is evaluated when each of its left and right subtrees is evaluated. In this manner, the lower an operator is in a subtree, the prior it will be evaluated. The root node contains the &lt;br&gt;
operator to be evaluated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5VjfgoTu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr7mwyhi4jobd7uvoldp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5VjfgoTu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr7mwyhi4jobd7uvoldp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;p&gt;To build expression trees, we use the stack data structure. We iterate over each character when a postfix string expression is passed and determine if it will be pushed on to our stack or not. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; We push it onto the stack if the character is an operand. \&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; We pop two elements from the stack if the character is an operator, make them the character's left and right node child, and push the sequence back into the stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;Repeat the above steps until end of Prefix expression.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Ex:&lt;/strong&gt; &lt;br&gt;
Let’s pass the postfix expression &lt;code&gt;“xy-”&lt;/code&gt; \&lt;br&gt;
The first character is &lt;code&gt;x&lt;/code&gt;; we push it onto the stack. The next character is &lt;code&gt;y&lt;/code&gt;; we also push it onto the stack. The final character is the &lt;code&gt;‘-’&lt;/code&gt; operator; we pop &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; and make them left and right child of &lt;code&gt;‘-’&lt;/code&gt;. So we end up with &lt;code&gt;x - y&lt;/code&gt; as our Infix expression.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program for Expression Tree:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Python program for expression tree
# create a class for an expression tree node
class Expression_tree: 
    # Constructor to create a node
    def __init__(self , value):
        self.value = value
        self.left = None
        self.right = None
# function to check if 'c' is an operator
def isOperator(c):
    if (c == '+' or c == '-' or c == '*'
        or c == '/' or c == '^'):
        return True
    else:
        return False
# function to do inorder traversal
# Process all nodes of a tree by recursively processing the left subtree, the root, and finally the right subtree.
def inorder(t):
    if t is not None:
        inorder(t.left)
        print (t.value, end=" ")
        inorder(t.right)
# Returns root of constructed tree for given postfix expression
def constructTree(postfix):
    stack = []

    # Traverse through every character of input expression
    # Traverse - process every character in a string, from left end to right end. 
    for char in postfix :
       # if operand, push into stack
        if not isOperator(char):
            t = Expression_tree(char)
            stack.append(t)

        # Operator
        else:
            # Pop two top nodes
            t = Expression_tree(char)
            t1 = stack.pop()
            t2 = stack.pop()

            # make them left and right children
            t.right = t1
            t.left = t2

            # Add this subexpression to stack
            stack.append(t)

    # Only element  will be the root of expression tree
    t = stack.pop()

    return t
# Driver program to test above
postfix = "ab*ef-g+i-"
r = constructTree(postfix)
print ("Infix expression: ")
inorder(r)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
    </item>
  </channel>
</rss>
