place the operators and operands in an expression tree. Then write the
expression in infix notation with full parentheses and print its final value.
Assume that the only operators are binary, the variables are single letters, the
constants are single digits and that there are no blanks. The user should input
the values for the single letter variables.
// prefix binaryTree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"bintree.h"
#include <iostream>
#include<string>
using namespace std;
void construct(bintree* ,string);
int _tmain(int argc, _TCHAR* argv[])
{
string prefix;
bintree* tree= new bintree;
cout<<"please enter the expression You want to evaluate: "<<endl;
cin>>prefix;
int size= (prefix.length());
construct (tree, prefix);
return 0;
}
void construct(bintree* r, string exp)
{
int i=1;
if (exp[0]=='+' || exp[0]=='-'|| exp[0]=='*' || exp[0]=='/')
{
r->retrieveroot();
r->insertroot(exp[0]);
cout<<"Root: "<<r->retrieverootinfo()<<endl;
}
r->retrieveleft();
r->insertroot(exp[i]);
cout<<"Left: "<<r->retrieverootinfo()<<endl;
r->retrieveright();
r->insertroot(exp[i]);
cout<<"Right: "<<r->retrieverootinfo()<<endl;
if (isdigit(exp[i]))
{
construct (r->retrieveright(), exp);
}
else
{
construct (r->retrieveleft(), exp);
}
}
This is my code for taking the prefix from the user.. but it gives out the correct root and zeros for the left and right children
Can anyone help pleasee ??!!

New Topic/Question
Reply




MultiQuote




|