I am writing a program that will evaluate expressions using binary trees. Most of the code has been provided, I just have to write the code for the class functions as listed in the header file. However, I am really new to recursion and trees...and this program uses public functions and private helper functions, which I am completely lost in. Here is the header file: -
struct CTNode
-
{
-
NodeType type;
-
int operand;
-
CTNode *left, *right;
-
};
-
-
class CalcTree
-
{
-
public:
-
CalcTree(); //default constructor
-
CalcTree( CalcTree& otherTree ); //copy constructor
-
~CalcTree(); //destructor
-
-
void setRoot( NodeType type_, int operand_ );
-
//construct a node with the given characteristics and place it at the root of this tree
-
-
void setLeft( CalcTree& otherTree );
-
//place a copy of the parameter tree as the left subtree of this tree
-
-
void setRight( CalcTree& otherTree );
-
//place a copy of the parameter tree as the right subtree of this tree
-
-
void printIN( );
-
//member function to print the stored expression in this tree using INFIX notation
-
-
void printPOST( );
-
//member function to print the stored expression in this tree using POSTFIX notation
-
-
float evaluate( );
-
//member function to evaluate the stored expression and return an answer
-
//assumes that the expression tree is well-formed
-
//the client must make sure the expression tree is well fromed before calling this function
-
-
private:
-
CTNode *copyHelper( CTNode *thatroot ); //recursive helper for copy constructor and "set" functions
-
//constructs an exact copy of the tree rooted at "thatroot" and returns a pointer to it
-
-
void destHelper( CTNode *thisroot ); //recursive helper for destructor
-
//completely deallocates all dynamic memory in the tree rooted at "thisroot"
-
-
void printINhelper( CTNode *thisroot ); //recursive helper for INFIX printing
-
//prints the expression tree rooted at "thisroot" in INFIX order
-
-
void printPOSThelper( CTNode *thisroot ); //recursive helper for POSTFIX printing
-
//prints the expression tree rooted at "thisroot" in POSTFIX order
-
-
float evalHelper( CTNode *thisroot ); //recursive helper for expression evaluation
-
//evaluates the expression tree rooted at "thisroot" and returns the answer
-
//returns a float so that integer division is avoided
-
-
CTNode *root; //pointer to the root node of this expression tree
-
};
-
-
#endif
-
If I am not mistaken, the public functions are quite simple, and primarily call the helper functions to do the work, right? I am just kind of lost with the helper functions and the whole concept of recursion. Here are some the functions I already have: -
CalcTree::destHelper(CTNode *thisroot){
-
-
if(root==NULL) return;
-
-
//if the current root has any subtrees, we must delete them first to prevent memory leak
-
if(( root->left != NULL ) || ( root->right != NULL ))
-
{
-
if( root->left != NULL ) //if there is a left subtree
-
{destHelper(root->left); //recursively deallocate it
-
root->left=NULL;} //set its pointer to NULL
-
if( root->right != NULL ) //if there is a right subtree
-
{destHelper(root->right); //recursively deallocate it
-
root->right=NULL;} //set its pointer to NULL
-
}
-
-
//at this point we are sure this root has no subtrees so we can delete it
-
delete root;
-
}
-
-
CalcTree::printINhelper(CTNode *thisroot){
-
//prints the expression tree rooted at "thisroot" in INFIX order
-
if ( thisroot != NULL ) { // Otherwise, there's nothing to print
-
printINhelper( thisroot->left ); // Print items in left subtree.
-
cout << thisroot->operand << " "; // Print the root item.
-
printINhelper( thisroot->right ); // Print items in right subtree.
-
}
-
-
}
-
Am I even doing this right? I am so lost. Any help or guidance would be greatly appreciated!
2 14648
I am writing a program that will evaluate expressions using binary trees. Most of the code has been provided, I just have to write the code for the class functions as listed in the header file. However, I am really new to recursion and trees...and this program uses public functions and private helper functions, which I am completely lost in.
I hear ya.
I was searching online for some help with (my guess is) the same exact problem. I can help you as far as my broken code can help you. I notice that you just modified the example code, but that won't work because of the CTNode struct in this class. My code doesn't work yet either, but I may be able to point you in the right direction. For example: here is my printINhelper... - void CalcTree::printINhelper(CTNode *thisroot){
-
if(thisroot!=NULL)
-
{
-
cout<<"(";
-
if(thisroot->left!=NULL){
-
printINhelper(thisroot->left);
-
}
-
if(thisroot->type != OPR){
-
if(thisroot->type == ADD) cout<<"+";
-
if(thisroot->type == SUB) cout<<"-";
-
if(thisroot->type == MUT) cout<<"*";
-
if(thisroot->type == DIV) cout<<"/";
-
} else cout<<thisroot->operand;
-
if(thisroot->right!=NULL) {
-
printINhelper(thisroot->right);
-
}
-
cout<<")";
-
}
-
}
-
I am fully aware that it's wrong, but I did notice that you didn't compensate for the NodeType type aspect of the struct.
Like I said, I can't get it working either, so if you figure something out, let me know. I'm in section 401 with Neil, you?
I finally figured out the whole recursive helper concept late last night...and finished everything with Neil during his office hours today. I'm just getting started on my intermediate drivers, but if you need any help with the program, just let me know :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jerry Khoo |
last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am
> cs students and are now facing difficult problems in understanding
> what a binary tree is, how it works, and the algorithm...
|
by: Jerry Khoo |
last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am
cs students and are now facing difficult problems in understanding
what a binary tree is, how it works, and the algorithm to...
|
by: jova |
last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so
can someone please explain.
Thank You.
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working,...
|
by: pembed2003 |
last post by:
Hi,
I have a question about how to walk a binary tree. Suppose that I have
this binary tree:
8
/ \
5 16
/ \ / \
3 7 9 22
/ \ / \ / \
|
by: Peter |
last post by:
Hi,
I have a Class-Tree based on "Command-Pattern"
eg.
public class Calculator
{ ....
}
public abstract class command
|
by: jamestuck21 |
last post by:
Hi,
I'm trying to work out a binary division problem
1100 / 101010101010111
Here is what I have so far but I'm not sure if I'm doing it correctly
and I'm suppose to continue the division...
|
by: hn.ft.pris |
last post by:
I have the following code:
Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template...
|
by: Vinodh |
last post by:
Started reading about Binary Trees and got the following questions in
mind. Please help.
Definition of a Binary Tree from "Data Structures using C and C++ by
Tanenbaum" goes like this,
"A...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |