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...
: 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.
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,
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume...
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter the time as a 4-digit number with no colon. Define an exception class called InvalidTimeFormatException. If the user enters an invalid time lime 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon. SAMPLE OUTPUT: Enter time in 24-hour notation: 1614 That is the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT