CS 302 Project
CS 302 Project
Samuel Gbafa
Contents
Main Page
Class Index
2.1
Class List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Index
3.1
File List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Documentation
4.1
4.1.2
. . . . . . . . . . .
4.1.1.1
ExprTree . . . . . . . . . . . . . . . . . . . . . . . .
4.1.1.2
ExprTree . . . . . . . . . . . . . . . . . . . . . . . .
4.1.1.3
ExprTree . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
4.1.2.1
build . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.2.2
build_Helper . . . . . . . . . . . . . . . . . . . . . .
4.1.2.3
clear . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.2.4
clear_Helper . . . . . . . . . . . . . . . . . . . . . . 10
4.1.2.5
commute . . . . . . . . . . . . . . . . . . . . . . . . 11
4.1.2.6
commute_Helper . . . . . . . . . . . . . . . . . . . . 11
4.1.2.7
copy_Helper . . . . . . . . . . . . . . . . . . . . . . 12
4.1.2.8
equivalent_Helper . . . . . . . . . . . . . . . . . . . 13
4.1.2.9
evaluate . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.2.10 evaluate_Helper . . . . . . . . . . . . . . . . . . . . 14
4.1.2.11 expression . . . . . . . . . . . . . . . . . . . . . . . 15
ii
CONTENTS
4.1.2.12 expression_Helper . . . . . . . . . . . . . . . . . . . 15
4.1.2.13 isEquivalent . . . . . . . . . . . . . . . . . . . . . . 16
4.1.2.14 isLeaf . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.2.15 operator= . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.2.16 showHelper
. . . . . . . . . . . . . . . . . . . . . . 17
4.1.2.17 showStructure . . . . . . . . . . . . . . . . . . . . . 17
4.1.3
4.2
4.2.2
root . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
ExprTreeNode . . . . . . . . . . . . . . . . . . . . . 18
dataItem . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2.2.2
left . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2.2.3
right . . . . . . . . . . . . . . . . . . . . . . . . . . 18
File Documentation
21
5.1
5.2
Define Documentation . . . . . . . . . . . . . . . . . . . . . . 21
5.1.1.1
LAB8_TEST1
. . . . . . . . . . . . . . . . . . . . . 21
5.1.1.2
LAB8_TEST2
. . . . . . . . . . . . . . . . . . . . . 21
5.1.1.3
LAB8_TEST3
. . . . . . . . . . . . . . . . . . . . . 21
Function Documentation . . . . . . . . . . . . . . . . . . . . . 22
5.2.1.1
boolTest . . . . . . . . . . . . . . . . . . . . . . . . 22
5.2.1.2
count . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.2.1.3
print . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.3
5.4
Detailed Description
. . . . . . . . . . . . . . . . . . . . . . . 22
5.5
5.6
. . . . . . . . . . . . . . . . . . . . . . . . . 23
Function Documentation . . . . . . . . . . . . . . . . . . . . . 23
5.6.1.1
dummy . . . . . . . . . . . . . . . . . . . . . . . . . 23
Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen
CONTENTS
iii
5.6.1.2
main . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.7
5.8
. . . . . . . . . . . . . . . . . . . . . . . . . 23
Function Documentation . . . . . . . . . . . . . . . . . . . . . 23
5.8.1.1
dummy . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.8.1.2
main . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 1
Main Page
This program will prompt the user for a mathematical expression, and turn it into a Tree
with an equivalent value and structure. It uses recursion and a class to do this.
Main Page
Chapter 2
Class Index
2.1
Class List
Here are the classes, structs, unions and interfaces with brief descriptions:
ExprTree< DataType > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
ExprTree< DataType >::ExprTreeNode . . . . . . . . . . . . . . . . . . . . 18
Class Index
Chapter 3
File Index
3.1
File List
. . . . . . . . . 21
. . . . . . . . . 21
. . . . . . . . . 22
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
22
22
23
23
23
File Index
Chapter 4
Class Documentation
ExprTree< DataType > Class Template Reference
4.1
#include <ExpressionTree.h>
Classes
class ExprTreeNode
ExprTree ()
ExprTree (const ExprTree &source)
ExprTree & operator= (const ExprTree &source)
ExprTree ()
void build ()
void expression () const
DataType evaluate () const throw (logic_error)
void clear ()
void commute ()
bool isEquivalent (const ExprTree &source) const
void showStructure () const
Class Documentation
Private Attributes
ExprTreeNode root
4.1.1
4.1.1.1
4.1.1.2
4.1.1.3
4.1.2
4.1.2.1
This function uses a helper to create the entire tree based on helpers prompts and
recursive execution.
Precondition
The tree will be empty
Postcondition
The tree will be filled appropriately with the values and structure of the input expression
Exceptions
None
Note
Uses a helper function to do the assignments
Returns
None, only updates data members
4.1.2.2
This function prompts the user for input for the current node, then continues to build the
tree. If the input is an operator, it then creates a new node and builds the left, then right.
If the input is a number, it creates a new node for it
Parameters
base The node that needs to be built
Precondition
The tree can exist or not
Postcondition
A tree as been built
Exceptions
None
Note
Uses recursion on the children in a pre-order traversal to propogate the effect
through the entire tree, if the input is an operator
Returns
None
4.1.2.3
10
Class Documentation
Postcondition
The tree will have no data and will be cleared with no memory leaks
Exceptions
None
Note
Uses a helper function to do the deletion
Returns
None, only updates data members
4.1.2.4
This function removes the children of the current node, then removes itself.
Parameters
base The node that will have its children and self deleted
Precondition
The current node will not be NULL.
Postcondition
The nodes in the tree will be de-allocated from that point in the tree downward
Exceptions
None
Note
Uses recursion on the children in a post-order traversal to propogate the effect
through the entire tree
Returns
None, only changes data members
4.1.2.5
11
Postcondition
The tree will have no data and will be cleared with no memory leaks
Exceptions
None
Note
Uses a helper function to do the deletion
Returns
None, only updates data members
4.1.2.6
This function swaps the values of the left and right nodes recursively to commute the
entire tree.
Parameters
base The node that will have its children swapped
Precondition
The current node will not be NULL.
Postcondition
Both children will have swapped values
Exceptions
None
12
Class Documentation
Note
Uses recursion on the children in a pre-order traversal to propogate the effect
through the entire tree
Returns
None, only changes data members
4.1.2.7
This function copies a tree from the current node and onwards. If the source isnt on a
NULL pointer, it copies the value of the source into a new node (passed by reference)
then tries to copy the children by calling itself upon them, until it finds a NULL termination
in the source tree.
Parameters
base The node that will be assigned and built upon
other The source node that will be copied from
Precondition
The current node will be NULL, and the other node will not be NULL
Postcondition
The nodes in the tree will be allocated and assigned from that point in the tree
downward, in the structure defined by the source tree
Exceptions
None
Note
Uses recursion on the children in a pre-order traversal to propogate the effect
through the entire tree
Returns
None, only changes data members, and the by-reference node parameter
4.1.2.8
13
This function checks if the current sub-tree is equivalent to the other by the rules of
commutativity. If both nodes are NULL, it assumes equivalence. If both nodes are not
NULL, it can proceed to check further. If the nodes have the same value, it can proceed
to check further. If the nodes are numbers, it assumes equivalence. Otherwise, it checks
the rules of operator commutativity: If the operator is addition or multiplication, it returns
true when the children are equivalently swapped perfectly or otherwise equivalent. Otherwise, the children must be completely equivalent for the operators to be equivalent
to each other. All other cases return false.
Parameters
base The node that will be checked against the source
other The node that serves as the point of reference
Precondition
The current node will represent a valid tree, and the source node will represent a
valid tree.
Postcondition
The nodes and sub-trees will be evaluated for equivalence under the rules of commutativity
Exceptions
None
Note
Uses recursion on the children in a post-order traversal to propagate the effect
through the entire tree
Returns
A boolean representing if the two sub-trees are equivalent or not
4.1.2.9
This function uses a helper to evaluate the value of entire tree based on helpers recursive execution.
14
Class Documentation
Precondition
The tree will be filled appropriately with the values and structure of the input expression
Postcondition
The value of the tree and intended expression will be output to screen
Exceptions
logic_error if a division by 0 occurs
Note
Uses a helper function to do the iteration and output
Returns
The value of the expression tree, as the data type specified
4.1.2.10
This function checks the current nodes value, then either returns it or evaluates it based
on the children to eventually get the value of the entire trees expression. If the current
node is an operator, it assumes that there are 2 children to be operated on, and calls
itself on them to get their value. If the current node is a number, it assumes that there is
nothing under them, and returns their value alone.
Parameters
base The node that will be evaluated
Precondition
The current node will not be NULL.
Postcondition
The value of the tree will be evaluated from that point in the tree downward
Exceptions
None
15
Note
Uses recursion on the children in a post-order traversal to propogate the effect
through the entire tree
Returns
The arithmetic result of the tree
4.1.2.11
This function uses a helper to write the trees expression in standard notation based on
helpers recursive execution.
Precondition
The tree will be filled appropriately with the values and structure of the input expression
Postcondition
The tree will be output to screen with a similar structure
Exceptions
None
Note
Uses a helper function to do the iteration and output
Returns
None
4.1.2.12
This function creates a standard visual of the expression tree from the current node.
Parameters
base The node that will be visualized
16
Class Documentation
Precondition
The current node will not be NULL.
Postcondition
The tree will be output in an American format
Exceptions
None
Note
Uses recursion on the children in an in-order traversal to propogate the effect
through the entire tree
Returns
None
4.1.2.13
This function checks if the other tree is equivalent by the rules of commutativity to the
calling tree. It uses a helper to do this.
Parameters
source The other tree to check
Precondition
The tree will be filled appropriately with the values and structure of the input expression
Postcondition
Both trees will be checked for equivalence by the helper.
Exceptions
None
17
Note
Uses a helper function to do the iteration and checks per node
Returns
boolean value demonstrating if the trees are the same under the rules of commutativity.
4.1.2.14
This function determines if the node passed to it is at the bottom of the tree (a leaf)
Parameters
base The node to be checked
Precondition
A tree exist
Postcondition
No changes have occurred
Exceptions
None
Note
None
Returns
True if both child Nodes are Null, False otherwise
4.1.2.15
4.1.2.16
4.1.2.17
18
Class Documentation
4.1.3
4.1.3.1
The documentation for this class was generated from the following files:
ExpressionTree.h
ExpressionTree.cpp
ExpressionTree_withShow.cpp
show8.cpp
4.2
Public Attributes
char dataItem
ExprTreeNode left
ExprTreeNode right
4.2.1
4.2.1.1
template<typename DataType > ExprTree< DataType >::ExprTreeNode::ExprTreeNode ( char elem, ExprTreeNode leftPtr, ExprTreeNode rightPtr
)
4.2.2
4.2.2.1
4.2.2.2
4.2.2.3
The documentation for this class was generated from the following files:
Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen
ExpressionTree.h
ExpressionTree.cpp
ExpressionTree_withShow.cpp
19
20
Class Documentation
Chapter 5
File Documentation
5.1
Defines
#define LAB8_TEST1 0
#define LAB8_TEST2 1
#define LAB8_TEST3 1
5.1.1
Define Documentation
5.1.1.1
#define LAB8_TEST1 0
Expression Tree class (Lab 8) configuration file. Activate test #N by defining the corresponding LAB8_TESTN to have the value 1.
5.1.1.2
#define LAB8_TEST2 1
5.1.1.3
#define LAB8_TEST3 1
5.2
Functions
void count (string output)
void print (string output)
void boolTest (string output, bool val)
22
File Documentation
5.2.1
Function Documentation
5.2.1.1
5.2.1.2
5.2.1.3
5.3
#include "ExpressionTree.h"
5.4
Classes
class ExprTree< DataType >
class ExprTree< DataType >::ExprTreeNode
5.4.1
Detailed Description
5.5
#include "ExpressionTree.h"
Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen
5.6
23
#include <iostream>
ExpressionTree.cpp"
#include <stdexcept>
#include "-
Functions
template<typename DataType >
void dummy (ExprTree< DataType > copyTree)
int main ()
5.6.1
Function Documentation
5.6.1.1
5.6.1.2
int main ( )
5.7
5.8
#include <iostream>
#include <stdexcept>
ExpressionTree.cpp" #include "config.h"
#include "-
Functions
template<typename DataType >
void dummy (ExprTree< DataType > copyTree)
int main ()
5.8.1
Function Documentation
5.8.1.1
5.8.1.2
int main ( )