{"id":224,"date":"2018-10-29T12:19:39","date_gmt":"2018-10-29T12:19:39","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=224"},"modified":"2018-10-30T12:28:09","modified_gmt":"2018-10-30T12:28:09","slug":"converting-from-prefix-notation-to-abstract-syntax-trees","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/converting-from-prefix-notation-to-abstract-syntax-trees\/","title":{"rendered":"Converting from prefix notation to Abstract Syntax Trees"},"content":{"rendered":"<p>To convert from prefix notation to an AST, we simply follow this algorithm:<\/p>\n<ol>\n<li>The operator becomes a parent<\/li>\n<li>The first argument becomes the left child<\/li>\n<li>The second argument becomes the right child<\/li>\n<\/ol>\n<p>If either or both of those arguments are expressions, then we just do the same thing for them. For example, if we have the expression\u00a0<strong>(+ (- 4 1) 3)<\/strong>, then the\u00a0<strong>+\u00a0<\/strong>will be the parent, and the left child will be the AST made up from\u00a0<strong>(- 4 1)<\/strong>, so\u00a0<strong>&#8211;<\/strong> is the parent and 4 and 1 the children. Finally, we have\u00a0<strong>3<\/strong> as the right hand child of\u00a0<strong>+.<\/strong><\/p>\n<div id=\"attachment_215\" style=\"width: 183px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-215\" class=\"size-full wp-image-215\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/10\/AST-Intro-2.png\" alt=\"\" width=\"173\" height=\"193\" \/><p id=\"caption-attachment-215\" class=\"wp-caption-text\">The AST for (+ (- 4 1) 3)<\/p><\/div>\n<p>What if we want to go the opposite way and create a prefix expression from an AST? We just start with the root note and apply this algorithm:<\/p>\n<ol>\n<li>Write out the operator<\/li>\n<li>If the first child is an operator, apply the algorithm to that child, otherwise write it out<\/li>\n<li>If the second child is an operator, apply the algorithm to that child, otherwise write it out<\/li>\n<\/ol>\n<p>Using the AST above, we would first encounter\u00a0<strong>+<\/strong>, followed by\u00a0<strong>&#8211;\u00a0<\/strong>meaning we would apply the algorithm to that subtree, giving us (<strong>+ (- 4 1) ..)<\/strong>. Next we would apply the final step to\u00a0<strong>3<\/strong>, which is not an operator, so we write it out.<\/p>\n<p>The exercises below will give you a chance to practice this. Type in your prefix expression and it will calculate the corresponding AST and infix expression.<\/p>\n\n<!-- iframe plugin v.6.0 wordpress.org\/plugins\/iframe\/ -->\n<iframe loading=\"lazy\" src=\"https:\/\/my.compclassnotes.com\/canonical\/4fe5a030-7f9b-4de9-943f-ac6e45155bb7\" width=\"800\" height=\"800\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"yes\" class=\"iframe-class\" frameborder=\"0\"><\/iframe>\n\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To convert from prefix notation to an AST, we simply follow this algorithm: The operator becomes a parent The first argument becomes the left child The second argument becomes the right child If either or both of those arguments are <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/converting-from-prefix-notation-to-abstract-syntax-trees\/\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-224","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":9,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/224\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/224\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}