{"id":16163,"date":"2023-05-05T11:26:54","date_gmt":"2023-05-05T11:26:54","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=16163"},"modified":"2023-05-05T11:26:54","modified_gmt":"2023-05-05T11:26:54","slug":"expression-tree-in-data-structure","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/","title":{"rendered":"Expression Tree in Data Structure"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg\" alt=\"\" \/><\/p>\n<p>A data structure called an expression tree is used to describe expressions that take the shape of trees. After creating an expression&#8217;s expression tree, we may execute inorder traversal to produce an infix expression. Postfix expressions can also be produced by traversing the expression tree in postorder. In this blog, we&#8217;ll talk about expression trees in data structures and how stacks may be used to create an expression tree from a given expression.<\/p>\n<h2>What is an Expression Tree in Data Structure?<\/h2>\n<p>A mathematical expression can be expressed as a binary tree using expression trees. Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.<\/p>\n<h3>Properties of Expression Tree in Data Structure<\/h3>\n<p>Let&#8217;s go through some expression tree properties.<\/p>\n<ul>\n<li>The operands are always represented by the leaf nodes. These operands are always used in the operations.<\/li>\n<li>The operator at the root of the tree is always given top priority.<\/li>\n<li>When compared to the operators at the bottom of the tree, the operator at the bottom is always given the lowest priority.<\/li>\n<li>Because the operand is always present at a depth of the tree, it is given the highest priority of all operators.<\/li>\n<li>The expression tree can be traversed to evaluate prefix expressions, postfix expressions, and infix expressions.<\/li>\n<\/ul>\n<p>In summary, the value present at the depth of the tree has the highest priority when compared to the other operators located at the top of the tree. The expression tree is immutable, and once built, we cannot change or modify it further, so to make any changes, we must completely construct the new expression tree. <\/p>\n<p>The given expression can be evaluated using the expression tree in data structure.<\/p>\n<pre><code> a + (b * c) + d * (e + f)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869312-1-01%20%2885%29.png\" alt=\"\" \/><\/p>\n<h3>Uses of an Expression Tree in Data Structure<\/h3>\n<p>The following is the primary application of these expression trees in data structure:<\/p>\n<ul>\n<li>\n<p>It evaluates, analyses, and modifies diverse phrases. It can also be used to determine the associativity of each operator in an expression.<br \/>\n<strong>As an example:<\/strong><br \/>\nThe + operator is left-associative, while the \/ operator is right-associative. The expression trees helped to solve the conundrum of this associativity. <\/p>\n<\/li>\n<li>\n<p>A context-free grammar is used to create these expression trees.<\/p>\n<\/li>\n<li>\n<p>It is a key component in compiler design and is part of the semantic analysis step.<\/p>\n<\/li>\n<li>\n<p>The expression trees are mostly used to create complex expressions that can be quickly evaluated.<\/p>\n<\/li>\n<\/ul>\n<h3>Construction of an Expression Tree in Data Structure<\/h3>\n<p>A stack is used to build an expression tree. For each character, we cycle through the input expressions and do the following. <\/p>\n<ul>\n<li>If a character is an operand, add it to the stack.<\/li>\n<li>If a character is an operator, pop both values from the stack and make both its children and push the current node again.<\/li>\n<\/ul>\n<p>Finally, the stack&#8217;s lone element will be the root of an expression tree.<\/p>\n<p>Let us understand this above process using a postfix expression. The implementation of the expression tree is described for the below expression.<\/p>\n<pre><code>a b + c *<\/code><\/pre>\n<ul>\n<li>\n<p><strong>Step1 :<\/strong> The first two symbols are operands, for which we generate a one-node tree and push a reference to the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869342-1-02%20%2843%29.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p><strong>Step2 :<\/strong> Next, read a&#8217;+&#8217; symbol to pop two pointers to the tree, form a new tree, and push a pointer to it onto the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869345-1-03%20%2829%29.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p><strong>Step3 :<\/strong> In the next stage &#8216;c&#8217; is read, we build a single node tree and store a pointer to it on the stack<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869346-1-04%20%2822%29.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p><strong>Step4 :<\/strong> Finally, we read the last symbol&#8217;<em> &#8216;, pop two tree pointers, and build a new tree with a,&#8217;<\/em>&#8216;as root, and a pointer to the final tree remains on the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869346-1-05%20%2816%29.png\" alt=\"\" \/><\/p>\n<\/li>\n<\/ul>\n<h3>Algorithm of an Expression Tree in Data Structure<\/h3>\n<p>Let ET be the expression tree<\/p>\n<pre><code>If  ET is not null then\n      If ET.value is operand then  \n                return  ET.value\n      A = solve(ET.left)\n      B = solve(ET.right)\n\n      return calculate(A, B, ET.value) \n\n\/\/ calculate() function applies operator 'ET.value' on A and B, and returns the value<\/code><\/pre>\n<h3>Code Implementation for Construction of an Expression Tree<\/h3>\n<p>Let us now discuss the code implementation of an expression tree for the expression :<br \/>\nA + B * C \/ D<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_16126 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_16126 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_16126 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_16126 .wpsm_nav-tabs > li.active > a, #tab_container_16126 .wpsm_nav-tabs > li.active > a:hover, #tab_container_16126 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_16126 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_16126 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_16126 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_16126 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_16126 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_16126 .wpsm_nav-tabs > li > a:hover , #tab_container_16126 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_16126 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_16126 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_16126 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_16126 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_16126 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_16126 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_16126 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_16126 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_16126 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_16126 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_16126 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_16126\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_16126\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_16126_1\" aria-controls=\"tabs_desc_16126_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_16126_2\" aria-controls=\"tabs_desc_16126_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_16126_3\" aria-controls=\"tabs_desc_16126_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_16126_4\" aria-controls=\"tabs_desc_16126_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_16126\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_16126_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct node {\r\n    char data;\r\n    struct node* left;\r\n    struct node* right;\r\n};\r\nstruct node* create_node(char data) {\r\n    struct node* new_node = (struct node*)malloc(sizeof(struct node));\r\n    new_node-&gt;data = data;\r\n    new_node-&gt;left = NULL;\r\n    new_node-&gt;right = NULL;\r\n    return new_node;\r\n}\r\n\r\nstruct node* construct_tree(char postfix[]) {\r\n    struct node* stack[100];\r\n    int top = -1;\r\n    int i = 0;\r\n    while (postfix[i] != '&#92;0') {\r\n        char ch = postfix[i];\r\n        if (ch &gt;= 'A' &amp;&amp; ch &lt;= 'Z') {\r\n            struct node* new_node = create_node(ch);\r\n            stack[++top] = new_node;\r\n        } else {\r\n            struct node* new_node = create_node(ch);\r\n            new_node-&gt;right = stack[top--];\r\n            new_node-&gt;left = stack[top--];\r\n            stack[++top] = new_node;\r\n        }\r\n        i++;\r\n    }\r\n    return stack[top--];\r\n}\r\n\r\nvoid inorder(struct node* root) {\r\n    if (root) {\r\n        inorder(root-&gt;left);\r\n        printf(\"%c \", root-&gt;data);\r\n        inorder(root-&gt;right);\r\n    }\r\n}\r\n\r\nint main() {\r\n    char postfix[] = \"ABC*+D\/\";\r\n    struct node* root = construct_tree(postfix);\r\n    printf(\"Inorder traversal of expression tree:&#92;n\");\r\n    inorder(root);\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_16126_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\n#include &lt;stack&gt;\r\n\r\nusing namespace std;\r\n\r\nstruct node {\r\n    char data;\r\n    node* left;\r\n    node* right;\r\n};\r\n\r\nnode* create_node(char data) {\r\n    node* new_node = new node();\r\n    new_node-&gt;data = data;\r\n    new_node-&gt;left = nullptr;\r\n    new_node-&gt;right = nullptr;\r\n    return new_node;\r\n}\r\n\r\nnode* construct_tree(char postfix[]) {\r\n    stack&lt;node*&gt; s;\r\n    int i = 0;\r\n    while (postfix[i] != '&#92;0') {\r\n        char ch = postfix[i];\r\n        if (ch &gt;= 'A' &amp;&amp; ch &lt;= 'Z') {\r\n            node* new_node = create_node(ch);\r\n            s.push(new_node);\r\n        } else {\r\n            node* new_node = create_node(ch);\r\n            new_node-&gt;right = s.top();\r\n            s.pop();\r\n            new_node-&gt;left = s.top();\r\n            s.pop();\r\n            s.push(new_node);\r\n        }\r\n        i++;\r\n    }\r\n    return s.top();\r\n}\r\n\r\nvoid inorder(node* root) {\r\n    if (root) {\r\n        inorder(root-&gt;left);\r\n        cout &lt;&lt; root-&gt;data &lt;&lt; \" \";\r\n        inorder(root-&gt;right);\r\n    }\r\n}\r\n\r\nint main() {\r\n    char postfix[] = \"ABC*+D\/\";\r\n    node* root = construct_tree(postfix);\r\n    cout &lt;&lt; \"Inorder traversal of expression tree:\" &lt;&lt; endl;\r\n    inorder(root);\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_16126_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\n\r\nclass Node {\r\n    char data;\r\n    Node left, right;\r\n    Node(char data) {\r\n        this.data = data;\r\n        left = right = null;\r\n    }\r\n}\r\n\r\nclass ExpressionTree {\r\n    static Node constructTree(char postfix[]) {\r\n        Stack&lt;Node&gt; stack = new Stack&lt;&gt;();\r\n        int i = 0;\r\n        while (i &lt; postfix.length) {\r\n            char ch = postfix[i];\r\n            if (Character.isLetter(ch)) {\r\n                Node new_node = new Node(ch);\r\n                stack.push(new_node);\r\n            } else {\r\n                Node new_node = new Node(ch);\r\n                new_node.right = stack.pop();\r\n                new_node.left = stack.pop();\r\n                stack.push(new_node);\r\n            }\r\n            i++;\r\n        }\r\n        return stack.pop();\r\n    }\r\n\r\n    static void inorder(Node root) {\r\n        if (root != null) {\r\n            inorder(root.left);\r\n            System.out.print(root.data + \" \");\r\n            inorder(root.right);\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        char postfix[] = \"ABC*+D\/\";\r\n        Node root = constructTree(postfix);\r\n        System.out.println(\"Inorder traversal of expression tree:\");\r\n        inorder(root);\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_16126_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">class Node:\r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.left = None\r\n        self.right = None\r\n\r\ndef construct_tree(postfix):\r\n    stack = []\r\n    for ch in postfix:\r\n        if ch.isalpha():\r\n            new_node = Node(ch)\r\n            stack.append(new_node)\r\n        else:\r\n            new_node = Node(ch)\r\n            new_node.right = stack.pop()\r\n            new_node.left = stack.pop()\r\n            stack.append(new_node)\r\n    return stack.pop()\r\n\r\ndef inorder(root):\r\n    if root:\r\n        inorder(root.left)\r\n        print(root.data, end=\" \")\r\n        inorder(root.right)\r\n\r\npostfix = \"ABC*+D\/\"\r\nroot = construct_tree(postfix)expression tree in data structures\r\nprint(\"Inorder traversal of expression tree:\")\r\ninorder(root)\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_16126 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_16126 a\"),jQuery(\"#tab-content_16126\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output :<\/strong><\/p>\n<pre><code>Inorder traversal of expression tree:\nA B C * + D \/<\/code><\/pre>\n<p><strong>Conclusion<\/strong><br \/>\nWe went over the expression tree in a data structure in detail in this article.  What is an expression tree in data structures?  how to build an expression tree in a data structure. Along with the code, we discussed the overall expression tree algorithm.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<p><strong>Q1. What is the use of an expression tree?<\/strong><br \/>\n<strong>Ans.<\/strong> Expression trees are also used in the dynamic language runtime (DLR) to provide interoperability between dynamic languages. NET and to enable compiler writers to emit expression trees instead of Microsoft intermediate language (MSIL).<\/p>\n<p><strong>Q2. What is an expression tree in the C function?<\/strong><br \/>\n<strong>Ans.<\/strong> An expression tree is a special type of binary tree that is used to store algebraic expressions. <\/p>\n<p><strong>Q3. What are expression trees and decision trees?<\/strong><br \/>\n<strong>Ans.<\/strong> A decision tree expression allows the definition of a binary tree of expressions. <\/p>\n<p><strong>Q4. What is an expression in data structure?<\/strong><br \/>\n<strong>Ans.<\/strong> An expression is a collection of operators and operands that represents a specific value. <\/p>\n<p><strong>Q5. What are the disadvantages of expression trees?<\/strong><br \/>\n<strong>Ans.<\/strong> The disadvantage of this approach is that too many nodes are used for expressions that contain one operator several times and simplification of expressions is difficult.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A data structure called an expression tree is used to describe expressions that take the shape of trees. After creating an expression&#8217;s expression tree, we may execute inorder traversal to produce an infix expression. Postfix expressions can also be produced by traversing the expression tree in postorder. In this blog, we&#8217;ll talk about expression trees [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[153],"tags":[],"class_list":["post-16163","post","type-post","status-publish","format-standard","hentry","category-tree"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Expression Tree in Data Structure<\/title>\n<meta name=\"description\" content=\"Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Expression Tree in Data Structure\" \/>\n<meta property=\"og:description\" content=\"Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-05T11:26:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Expression Tree in Data Structure\",\"datePublished\":\"2023-05-05T11:26:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\"},\"wordCount\":846,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg\",\"articleSection\":[\"Trees\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\",\"name\":\"Expression Tree in Data Structure\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg\",\"datePublished\":\"2023-05-05T11:26:54+00:00\",\"description\":\"Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Trees\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/tree\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Expression Tree in Data Structure\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Expression Tree in Data Structure","description":"Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Expression Tree in Data Structure","og_description":"Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.","og_url":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-05-05T11:26:54+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Expression Tree in Data Structure","datePublished":"2023-05-05T11:26:54+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/"},"wordCount":846,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg","articleSection":["Trees"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/","url":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/","name":"Expression Tree in Data Structure","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg","datePublished":"2023-05-05T11:26:54+00:00","description":"Expression trees are binary trees with each leaf node serving as an operand and each internal (non-leaf) node serving as an operator.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1683285869193-Expression%20tree%20in%20data%20structure.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/expression-tree-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Trees","item":"https:\/\/prepbytes.com\/blog\/category\/tree\/"},{"@type":"ListItem","position":3,"name":"Expression Tree in Data Structure"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/16163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=16163"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/16163\/revisions"}],"predecessor-version":[{"id":16165,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/16163\/revisions\/16165"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=16163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=16163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=16163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}