Question

In: Computer Science

Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...

Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.

Solutions

Expert Solution

  • in this program we convert the input prefix notation into a binary tree
  • the tree is implemented using a linked list of node structure it have both left and right link
  • left operand in left child and right operand in right child
  • after tree creation we check it by printing the infix notation and postfix notation from the tree we built from the prefix notation.

the code is given below

#include <iostream>

using namespace std;
//structure for node of a tree
typedef struct node{
    char data;
    struct node*left,*right;
}node;
//recursive functon for building tree
char* buildtree(node** x,char * y)
{
    if(* y=='\0')
    return '\0';
    while(1)
    {
        char* a='\0';
        if(*x==NULL)
        {
            node* newnode=(node*)malloc(sizeof(node));
            newnode->data=*y;
            newnode->left=NULL;
            newnode->right=NULL;
            *x=newnode;
        }
        else
        {
            if((*y>='a' && *y<='z')|| (*y>='0'&&*y<='9'))
            return y;
        
        a=buildtree(&(*x)->left,y+1);
        a=buildtree(&(*x)->right,a+1);
        return a;
    
        }   
    }
}
//function for infix notation
void infix(node *x)
{
    if (x==NULL)
    return ;
    else
    {
        infix(x->left);
        cout<<(x->data);
        infix(x->right);
        
    }
}
//function for postfix notation
void postfix(node *x)
{
    if (x==NULL)
    return ;
    else
    {
        postfix(x->left);
        postfix(x->right);
        cout<<(x->data);
        
    }
}
//function that converts prefix to binary tree  and show its infix and postfix from the tree itself
void prefixtotree(char prefix[])
{ 
    node*s = NULL;
    buildtree(&s,prefix);
    cout<<"infix expression from the tree is :";infix(s);
    cout<<"\npostfix expression from  the tree is:";postfix(s);
}

int main()
{
   
    cout<<"enter the prefix notation:";
    char prefix[50];
    cin>>prefix;
    prefixtotree(prefix);
    

    return 0;
}


Related Solutions

Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below: // Writing a program that converts from 24-hour notation to 12-hour notation.// #include...
Polish notation, also known as Polish prefix notation or simply prefix notation, is a form of...
Polish notation, also known as Polish prefix notation or simply prefix notation, is a form of notation for logic, arithmetic, and algebra. Its distinguishing feature is that it places operators to the left of their operands. The expression for adding the numbers 1 and 2, in prefix notation, is written as “+ 12” rather than “1 + 2”. In more complex expressions, the operands may be nontrivial expressions including operators of their own. For instance, the expression that would be...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *,...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.).
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
Write a Java program which takes a String representing an arithmetic expression as an input and...
Write a Java program which takes a String representing an arithmetic expression as an input and displays whether or not the expression is balanced. If the expression is not balanced (i.e. the wrong # of parentheses or in the wrong order) it will display an error message. For Example: Input an expression: ( ( 2 + 4 ) * 2 ) Expression is balanced Input an expression: ( 5 * 7 – 6 ) ) Error: Expression is not balanced.......
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts and outputs the equivalent weight in pounds. Format your output to 3 decimal places. Your output should look like this 53.000 Kg is equivalent to 123.459 Ibs (Note 1Kg = 2.2046226218488 lbs) Takes in the price of an item on an online store in pound sterling, converts and outputs the equivalent price in U.S dollars. Format your output to 2 decimal places. Your output...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT