Question

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.

Answer #1

code in c++ for prefix to binary tree (code to copy)

#include<bits/stdc++.h>

using namespace std;

// class for tree node

class TreeNode{

public:

char value;

TreeNode *left;

TreeNode *right;

TreeNode(char data){

value=data;

left=NULL;

right=NULL;

}

};

// this function returns if a character is an operator

bool isOperator(char ch){

if (ch == '+' || ch == '-' ||

ch == '*' || ch == '/' ||

ch == '^')

return true;

return false;

}

//inorder traversal of the tree

void inorder(TreeNode* root){

if(root==NULL){

return;

}

inorder(root->left);

cout<<root->value<<" ";

inorder(root->right);

}

int constructTreeFromPrefix(TreeNode** root, string prefix, int
cur_pos=0){

if(cur_pos==prefix.size()){

//this is the end of the expression

return cur_pos;

}

//recursively build the expression tree

while(1){

int new_pos;

if(*root==NULL){

//create a new node with the current character in data

TreeNode* newNode = new TreeNode(prefix[cur_pos]);

*root=newNode;

}else{

//check if this character is an operand

if(!isOperator(prefix[cur_pos])){

return cur_pos;

}

//build the left tree

new_pos = constructTreeFromPrefix(&(*root)->left, prefix,
cur_pos+1);

//build the right tree

new_pos =constructTreeFromPrefix(&(*root)->right, prefix,
new_pos+1);

return cur_pos;

}

}

}

// Driver program to test above

int main()

{

TreeNode* root = NULL;

string prefix = "*+ab-cd";

constructTreeFromPrefix(&root, prefix);

cout<<"Tree is created. Inorder traversal of tree: ";

inorder(root);

return 0;

}

code screenshot

Console Output Screenshot

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/

In this lab, you will write a program that creates a binary
search tree based on user input. Then, the user will indicate what
order to print the values in. **Please write in C code**
Start with the bst.h and bst.c base code provided to you. You
will need to modify the source and header file to complete this
lab.
bst.h:
#ifndef BST_H
#define BST_H
typedef struct BSTNode
{
int value;
struct BSTNode* left;
struct BSTNode* right;
} BSTNode;
BSTNode*...

Consider a binary search tree where each tree node v has a field
v.sum which stores the sum of all the keys in the subtree rooted at
v. We wish to add an operation SumLE(K) to this binary search tree
which returns the sum of all the keys in the tree whose values are
less than or equal to K.
(a) Describe an algorithm, SumLE(K), which returns the sum of
all the keys in the tree whose values are less...

C programing
Write a function that takes an array of integers and applies
another function (f(x)= 3x+4) on each element and then stores the
result in another array and finally return it once it is done.

C++ Write a function that takes in 3 arguments: a sorted array,
size of the array, and an integer number. It should return the
position where the integer value is found. In case the number does
not exist in that array it should return the index where it should
have been if it were present in this sorted array. Use pointer
notation of arrays for this question.
c++
code

(C++) Write a function called
triple that takes an array of integers as a
parameter as well as the length of the array. It should triple each
value in the array. The function should not return anything. (Note
that the contents of the array WILL be modified.)

The Binary Search Tree implementation for bst.zip. The code in
the destructor of the BST class is empty. Complete the destructor
so the memory allocated for each node in the BST is freed. Make a
couple of different trees in your main method or in a function to
test the destructor (the program should not crash upon
exiting).
bst.zip (includes the following files below in
c++):
bst.h:
#pragma once
#include
#include "node.cpp"
using namespace std;
template
class BST
{
public:...

Explain your code with comments. Solve in C++.
Write a function named myFunc3() that
takes a 2D integer array NUMBERS[][50], and it
size n and m.
Then the function will print the sum of each row in one line.

In C programming language write a function that takes two arrays
as input m and n as well as their sizes: size_m and size_n,
respectively.
Then it checks for each element in m, whether it exists in n.
The function should update a third array c such that for each
element in m:
the corresponding position/index in c should be either
1, if this element exists in m, or
0, if the element does not exist in n

Write a C++ program that converts time of day from a 24-hour
notation to a 12-hour notation. For example, it should convert
14:25 to 2:25 PM.
(A) The user provides input as two integers separated by ‘:’.
The following function prototype should capture the user inputs as
described below:
void input(int& hours24, int& minutes);
//Precondition: input(hours, minutes) is called with
//arguments capable of being assigned values.
//Postcondition:
// user is prompted for time in 24 hour format:
// HH:MM, where...

in Java
In this exercise, you'll write a Java version of the
infix-to-postfix conversion algorithm. These same mechanisms can be
used as a part of writing a simple compiler.
Write class InfixToPostfixConverter co convert
an ordinary infix arithmetic expression (assume a valid expression
is entered) with single-digit integers (to make
things easier) such as
(6 + 2) • 5 - 8 / 4
to a postfix expression. The postfix version (no parentheses are
needed) of this infix expression is
6...

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 8 minutes ago

asked 22 minutes ago

asked 29 minutes ago

asked 29 minutes ago

asked 50 minutes ago

asked 51 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago