Questions about binary tree recursive functions.We have created the following test programs.The compilation works well, but I haven't done what I want to do.When compiled, ()
appears more than expected.How can I see ((A+B)*(C-D))
and ((A-B)+C)*(D/E))
?
If ptr can erase the last character of output when it reaches nullptr, it seems like it can be solved, but I don't even know how to erase the last character.If you can solve this problem, please take care of me.
main.cpp
int main(intargc, const char*argv[]){
std::cout<<"Testing Base Parse Tree"<<std::endl<<std::endl;
std::string expression1 = "AB+CD-*";
std::string expression2 = "AB-C+DE/*";
ParseTree1 (expression1);
std::cout<<"Input is AB+CD-*"<<std::endl;
std::cout<<"In Order should be ((A+B)*(C-D)) and is"<ptree1.inOrder()<<std::endl;
ParseTree2 (expression2);
std::cout<<"Input is AB-C+DE/*"<<std::endl;
std::cout<<"In Order output should be ((A-B)+C)*(D/E)) and is"<<ptree2.inOrder()<<std:::endl;
std::cout<<"Done with Parse Tree test"<<std::endl<<std::endl;
return 0;
}
ParseTree.cpp
#include "ParseTree.h"
# include <string>
Stack:: Stack() {head=nullptr;}
Stack:: to Stack()
{
while(head!=nullptr)
{
pop();
}
}
void Stack::push (Node*value)
{
Link*temp = new Link (value);
temp->setNext(head);
head = temp;
}
void Stack::pop()
{
Link* temp=head;
head=head->getNext();
delete temp;
}
Node* Stack::top()
{
return head ->getValue();
}
bool Stack:empty()
{
if(head==nullptr)
{
return true;
}
return false;
}
ParseTree:: ParseTree (string expression)
{
if(expression=="")
{
root=nullptr;
}
else{
root=doParse(expression);
}
}
ParseTree::~ParseTree()
{
// call recDelete
recDelete (root);
}
// delete nodes recurring
void ParseTree::recDelete(Node*ptr)
{
if(ptr!=nullptr)
{
recDelete(ptr->getLeft());
recDelete(ptr->getRight());
delete ptr;
}
}
Node* ParseTree::doParse (string expression)
{
Stack theStack;
for(inti=0;i<expression.length();i++){
character=expression[i];
if(isOperand(letter)==true){
theStack.push (new Node (letter));
} else{
Node* temp = new Node (letter);
temp->setRight(theStack.top());
theStack.pop();
temp->setLeft(theStack.top());
theStack.pop();
theStack.push(temp);
}
}
return theStack.top();
}
bool ParseTree::isOperand(charletter)
{
if(letter=='+'||letter=='-'||letter=='*'||letter=='/'|letter=='('||letter==')')
{
return false;
}
return true;
}
bool ParseTree::isOperator(charletter)
{
if(letter=='+'||letter=='-'||letter=='*'||letter=='/')
{
return true;
}
return false;
}
US>string ParseTree::inOrder()
{
return recInOrder (root);
}
US>string ParseTree:recInOrder (Node*ptr)
{
US>//#string output=";
if(ptr==nullptr)
{
return output;
}
output=output+"("+recInOrder(ptr->getLeft()));
output = output + ptr->getValue();
output = output + recInOrder(ptr->getRight()) + ");
return output;
}
ParseTree.h
# include <iostream>
using std::string;
const char LPAREN='(';
const char RPAREN=')';
US>class Node }
private:
char value;
Node* left;
Node* right;
public:
// constructor
Node(char value) {left=right=nullptr;this->value=value;}
// setter
void setLeft(Node*left) {this->left=left;}
void setRight(Node*right) {this->right=right;}
// getter
Node* getLeft() {return left;}
Node* getRight() {return right;}
chargeValue() {return value;}
};
classLink {
private:
Node* value;
Link*next;
public:
Link(Node*value, Link*next=nullptr) {this->value=value;this->next=next;}
void setNext(Link*next) {this->next=next;}
Link*getNext() {return next;}
Node* getValue() {return value;}
};
US>class Stack {
private:
Link*head;
public:
Stack();
~Stack();
void push (Node*value);
void pop();
Node*top();
boolean empty();
};
US>class ParseTree {
private:
Node* root;
public:
ParseTree (string expression);
~ParseTree();
void recDelete (Node*ptr);
Node* doParse (string expression);
bool isOperand (char letter);
bool is Operator (char letter);
US>string inOrder();
string recInOrder (Node*ptr);
};
This is probably because single elements such as A
and B
always have (
, )
.
string ParseTree::recInOrder(Node*ptr)
in ParseTree.cpp:
output=output+"("+recInOrder(ptr->getLeft()));
output = output + ptr->getValue();
output = output + recInOrder(ptr->getRight()) + ");
Why don't we print (
,) only when there are elements on the left and right sides like this?
if(ptr->getLeft()!=nullptr){
output=output+"("+recInOrder(ptr->getLeft()));
}
output = output + ptr->getValue();
if(ptr->getRight()!=nullptr){
output = output + recInOrder(ptr->getRight()) + ");
}
© 2024 OneMinuteCode. All rights reserved.