Answer on Question #60485, Programming & Computer Science / C++
Question:
below shows the postfix expression. Draw a binary tree of abc * + de * f + g + and find the following
i. infix expression
ii. prefix expression
From the traversal of predorder, inorder and postorder as above write down the c++ statement for each
i. predorder function
ii. inorder function
Solution:
i. Infix: (a + (b * c)) ? ((d * e) + f) + g)
ii. Prefix: ? + a * b c + + * d e f g
Figure 1. Expression tree
i.predorder function:
void Preorder(tree *root) {
if(root == nullptr) return;
cout << root->value << " ";
Preorder(root->left);
Preorder(root->right);
}ii.inorder function:
void Inorder(tree *root) {
if(root == nullptr) return;
Inorder(root->left);
cout << root->value << " ";
Inorder(root->right);
}https://www.AssignmentExpert.com
Comments