0% found this document useful (0 votes)
192 views29 pages

CS 302 Project

This document describes a class template called ExprTree that represents mathematical expressions as binary trees. It contains documentation for the ExprTree class and its nested ExprTreeNode class. The ExprTree class contains functions for building, evaluating, clearing and traversing an expression tree, as well as getting its string representation. Many functions use private helper functions to recursively operate on the tree.

Uploaded by

sampe12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views29 pages

CS 302 Project

This document describes a class template called ExprTree that represents mathematical expressions as binary trees. It contains documentation for the ExprTree class and its nested ExprTreeNode class. The ExprTree class contains functions for building, evaluating, clearing and traversing an expression tree, as well as getting its string representation. Many functions use private helper functions to recursively operate on the tree.

Uploaded by

sampe12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

PA06

Samuel Gbafa

Generated by Doxygen 1.7.6.1


Wed Oct 8 2014 19:04:28

Contents

Main Page

Class Index

2.1

Class List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

File Index

3.1

File List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Class Documentation
4.1

ExprTree< DataType > Class Template Reference


4.1.1

4.1.2

. . . . . . . . . . .

Constructor & Destructor Documentation . . . . . . . . . . . . .

4.1.1.1

ExprTree . . . . . . . . . . . . . . . . . . . . . . . .

4.1.1.2

ExprTree . . . . . . . . . . . . . . . . . . . . . . . .

4.1.1.3

ExprTree . . . . . . . . . . . . . . . . . . . . . . .

Member Function Documentation

. . . . . . . . . . . . . . . .

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

Member Data Documentation . . . . . . . . . . . . . . . . . . . 18


4.1.3.1

4.2

ExprTree< DataType >::ExprTreeNode Class Reference . . . . . . . . 18


4.2.1

Constructor & Destructor Documentation . . . . . . . . . . . . . 18


4.2.1.1

4.2.2

root . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

ExprTreeNode . . . . . . . . . . . . . . . . . . . . . 18

Member Data Documentation . . . . . . . . . . . . . . . . . . . 18


4.2.2.1

dataItem . . . . . . . . . . . . . . . . . . . . . . . . 18

4.2.2.2

left . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4.2.2.3

right . . . . . . . . . . . . . . . . . . . . . . . . . . 18

File Documentation

21

5.1

config.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 21


5.1.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

debug.cpp File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 21


5.2.1

Function Documentation . . . . . . . . . . . . . . . . . . . . . 22
5.2.1.1

boolTest . . . . . . . . . . . . . . . . . . . . . . . . 22

5.2.1.2

count . . . . . . . . . . . . . . . . . . . . . . . . . . 22

5.2.1.3

print . . . . . . . . . . . . . . . . . . . . . . . . . . 22

5.3

ExpressionTree.cpp File Reference . . . . . . . . . . . . . . . . . . . . 22

5.4

ExpressionTree.h File Reference . . . . . . . . . . . . . . . . . . . . . 22


5.4.1

Detailed Description

. . . . . . . . . . . . . . . . . . . . . . . 22

5.5

ExpressionTree_withShow.cpp File Reference . . . . . . . . . . . . . . 22

5.6

ptest.cpp File Reference


5.6.1

. . . . . . . . . . . . . . . . . . . . . . . . . 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

show8.cpp File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 23

5.8

test8.cpp File Reference


5.8.1

. . . . . . . . . . . . . . . . . . . . . . . . . 23

Function Documentation . . . . . . . . . . . . . . . . . . . . . 23
5.8.1.1

dummy . . . . . . . . . . . . . . . . . . . . . . . . . 23

5.8.1.2

main . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

Chapter 3

File Index
3.1

File List

Here is a list of all files with brief descriptions:


config.h . . . . . . . . . . . . . . . . . . . . . . . . . . . .
debug.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . .
ExpressionTree.cpp . . . . . . . . . . . . . . . . . . . . . .
ExpressionTree.h
A program to make an Expression into a tree format
ExpressionTree_withShow.cpp . . . . . . . . . . . . . . . .
ptest.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . .
show8.cpp . . . . . . . . . . . . . . . . . . . . . . . . . .
test8.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . 21
. . . . . . . . . 21
. . . . . . . . . 22
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

22
22
23
23
23

File Index

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

Chapter 4

Class Documentation
ExprTree< DataType > Class Template Reference

4.1

#include <ExpressionTree.h>

Classes
class ExprTreeNode

Public Member Functions

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

Private Member Functions

bool isLeaf (ExprTreeNode base) const


void build_Helper (ExprTreeNode &base)
void showHelper (ExprTreeNode p, int level) const
void expression_Helper (ExprTreeNode base) const
DataType evaluate_Helper (ExprTreeNode base) const

Class Documentation

void clear_Helper (ExprTreeNode base) const


void copy_Helper (ExprTreeNode &base, ExprTreeNode other)
void commute_Helper (ExprTreeNode base)
bool equivalent_Helper (ExprTreeNode base, ExprTreeNode other) const

Private Attributes
ExprTreeNode root

template<typename DataType> class ExprTree< DataType >

4.1.1

Constructor & Destructor Documentation

4.1.1.1

template<typename DataType > ExprTree< DataType >::ExprTree ( )

4.1.1.2

template<typename DataType > ExprTree< DataType >::ExprTree ( const


ExprTree< DataType > & source )

4.1.1.3

template<typename DataType > ExprTree< DataType >::ExprTree ( )

4.1.2

Member Function Documentation

4.1.2.1

template<typename DataType > void ExprTree< DataType >::build ( )

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

4.1 ExprTree< DataType > Class Template Reference

Returns
None, only updates data members

4.1.2.2

template<typename DataType > void ExprTree< DataType >::build_Helper (


ExprTreeNode & base ) [private]

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

template<typename DataType > void ExprTree< DataType >::clear ( )

This function uses a helper to delete the entire tree.


Precondition
The tree will be filled appropriately with the values and structure of the input expression

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

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

template<typename DataType > void ExprTree< DataType >::clear_Helper (


ExprTreeNode base ) const [private]

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

4.1 ExprTree< DataType > Class Template Reference

4.1.2.5

11

template<typename DataType > void ExprTree< DataType >::commute ( )

This function uses a helper to delete the entire tree.


Precondition
The tree will be filled appropriately with the values and structure of the input expression

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

template<typename DataType > void ExprTree< DataType >::commute_Helper (


ExprTreeNode base ) [private]

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

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

template<typename DataType > void ExprTree< DataType >::copy_Helper (


ExprTreeNode & base, ExprTreeNode other ) [private]

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

4.1 ExprTree< DataType > Class Template Reference

4.1.2.8

13

template<typename DataType > bool ExprTree< DataType >::equivalent_Helper


( ExprTreeNode base, ExprTreeNode other ) const [private]

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

template<typename DataType > DataType ExprTree< DataType >::evaluate ( )


const throw (logic error)

This function uses a helper to evaluate the value of entire tree based on helpers recursive execution.

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

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

template<typename DataType > DataType ExprTree< DataType


>::evaluate_Helper ( ExprTreeNode base ) const [private]

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

4.1 ExprTree< DataType > Class Template Reference

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

template<typename DataType > void ExprTree< DataType >::expression ( )


const

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

template<typename DataType > void ExprTree< DataType


>::expression_Helper ( ExprTreeNode base ) const [private]

This function creates a standard visual of the expression tree from the current node.
Parameters
base The node that will be visualized

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

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

template<typename DataType > bool ExprTree< DataType >::isEquivalent (


const ExprTree< DataType > & source ) const

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

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

4.1 ExprTree< DataType > Class Template Reference

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

template<typename DataType > bool ExprTree< DataType >::isLeaf (


ExprTreeNode base ) const [private]

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

template<typename DataType > ExprTree< DataType > & ExprTree< DataType


>::operator= ( const ExprTree< DataType > & source )

4.1.2.16

template<typename DataType > void ExprTree< DataType >::showHelper (


ExprTreeNode p, int level ) const [private]

4.1.2.17

template<typename DataType > void ExprTree< DataType >::showStructure ( )


const

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

18

Class Documentation

4.1.3

Member Data Documentation

4.1.3.1

template<typename DataType > ExprTreeNode ExprTree< DataType >::root


[private]

The documentation for this class was generated from the following files:
ExpressionTree.h
ExpressionTree.cpp
ExpressionTree_withShow.cpp
show8.cpp

ExprTree< DataType >::ExprTreeNode Class Reference

4.2

Public Member Functions


ExprTreeNode (char elem, ExprTreeNode leftPtr, ExprTreeNode rightPtr)

Public Attributes
char dataItem
ExprTreeNode left
ExprTreeNode right

template<typename DataType> class ExprTree< DataType >::ExprTreeNode

4.2.1

Constructor & Destructor Documentation

4.2.1.1

template<typename DataType > ExprTree< DataType >::ExprTreeNode::ExprTreeNode ( char elem, ExprTreeNode leftPtr, ExprTreeNode rightPtr
)

4.2.2

Member Data Documentation

4.2.2.1

template<typename DataType > char ExprTree< DataType


>::ExprTreeNode::dataItem

4.2.2.2

template<typename DataType > ExprTreeNode ExprTree< DataType


>::ExprTreeNode::left

4.2.2.3

template<typename DataType > ExprTreeNode ExprTree< DataType


>::ExprTreeNode::right

The documentation for this class was generated from the following files:
Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

4.2 ExprTree< DataType >::ExprTreeNode Class Reference

ExpressionTree.h
ExpressionTree.cpp
ExpressionTree_withShow.cpp

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

19

20

Class Documentation

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

Chapter 5

File Documentation
5.1

config.h File Reference

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

debug.cpp File Reference

#include <iostream> #include <string>

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

void boolTest ( string output, bool val )

5.2.1.2

void count ( string output )

5.2.1.3

void print ( string output )

5.3

ExpressionTree.cpp File Reference

#include "ExpressionTree.h"

5.4

ExpressionTree.h File Reference

A program to make an Expression into a tree format.

#include <stdexcept> #include <iostream>

Classes
class ExprTree< DataType >
class ExprTree< DataType >::ExprTreeNode

5.4.1

Detailed Description

A program to make an Expression into a tree format.


Author
Samuel Gbafa
Version
1.0
The program takes a user input in prefix form, and represents it in a Tree, commuting
and evaluating it.
Date
Wednesday, October 8th, 2014

5.5

ExpressionTree withShow.cpp File Reference

#include "ExpressionTree.h"
Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

5.6 ptest.cpp File Reference

5.6

23

ptest.cpp File Reference

#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

template<typename DataType > void dummy ( ExprTree< DataType > copyTree )

5.6.1.2

int main ( )

5.7

show8.cpp File Reference

5.8

test8.cpp File Reference

#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

template<typename DataType > void dummy ( ExprTree< DataType > copyTree )

5.8.1.2

int main ( )

Generated on Wed Oct 8 2014 19:04:28 for PA06 by Doxygen

You might also like