Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

- implement the expression tree adt using c++ programming

- implement the Expression Tree ADT (80 points)

- implement the following operations:

- constructor, copy constructor, assignment operator, destructor

- build, expression, evaluate

- clear

#include "ExpressionTree.h"

template <typename DataType>

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

{

}

template <typename DataType>

ExprTree<DataType>::ExprTree ()

{

}

template <typename DataType>

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

{

}

template <typename DataType>

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

{

}

template <typename DataType>

void ExprTree<DataType>::copyHelper(ExprTreeNode *&p)

{}

template <typename DataType>

ExprTree<DataType>::~ExprTree ()

{

}

template <typename DataType>

void ExprTree<DataType>::build ()

{

}

#include ExpressionTree.h

template <>

void ExprTree<float>::buildHelper(ExprTreeNode*& p)

{}

template <>

void ExprTree<bool>::buildHelper(ExprTreeNode*& p)

{}

template <typename DataType>

void ExprTree<DataType>::expression () const

{

}

template <typename DataType>

void ExprTree<DataType>::exprHelper(ExprTreeNode* p) const

{}

template <typename DataType>

DataType ExprTree<DataType>::evaluate() const throw (logic_error)

{

   DataType temp;

   return temp;

}

template <>

float ExprTree<float>::evalHelper(ExprTreeNode* p) const

{

   float temp;

   return temp;

}

template <>

bool ExprTree<bool>::evalHelper(ExprTreeNode* p) const

{

   bool temp;

   return temp;

}

template <typename DataType>

void ExprTree<DataType>::clear ()

{

}

template <typename DataType>

void ExprTree<DataType>::clearHelper(ExprTreeNode *p)

{}

template <typename DataType>

void ExprTree<DataType>::commute()

{

}

template <typename DataType>

void ExprTree<DataType>::commuteHelper(ExprTreeNode* p)

{}

template <typename DataType>

bool ExprTree<DataType>::isEquivalent(const ExprTree& source) const

{

}

template <typename DataType>

bool ExprTree<DataType>::isEquivalentHelper(const ExprTreeNode* x,

   const ExprTreeNode* y) const

{}

template <typename DataType>

bool ExprTree<DataType>::isEmpty() const

{

   return false;

}

template <typename DataType>

void ExprTree<DataType>::showStructure() const

// Outputs an expression tree. The tree is output rotated counter-

// clockwise 90 degrees from its conventional orientation using a

// "reverse" inorder traversal. This operation is intended for testing

// and debugging purposes only.

{

   // No isEmpty function in this class. Add a private one if you wish.

   if (root == 0)

       cout << "Empty tree" << endl;

   else

   {

       cout << endl;

       showHelper(root, 1);

       cout << endl;

   }

}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template <typename DataType>

void ExprTree<DataType>::showHelper(ExprTreeNode *p, int level) const

// Recursive helper for the showStructure() function. Outputs the

// subtree whose root node is pointed to by p. Parameter level is the

// level of this node within the expression tree.

{

   int j; // Loop counter

   if (p != 0)

   {

       showHelper(p->right, level + 1); // Output right subtree

       for (j = 0; j < level; j++) // Tab over to level

           cout << "\t";

       cout << " " << p->dataItem; // Output dataItem

       if ((p->left != 0) && // Output "connector"

           (p->right != 0))

           cout << "<";

       else if (p->right != 0)

           cout << "/";

       else if (p->left != 0)

           cout << "\\";

       cout << endl;

       showHelper(p->left, level + 1); // Output left subtree

   }

}

#Include "ExpressionTree.h"

///////////////////////////////////////////////

//--------------------------------------------------------------------

//

// Laboratory 8 ExpressionTree.h

//

// Class declarations for the linked implementation of the

// Expression Tree ADT -- including the recursive helpers for the

// public member functions

//

// Instructor copy with the recursive helper function declarations.

// The student version does not have those, but has a place to write

// the declarations in the private section.

//

//--------------------------------------------------------------------

#ifndef EXPRESSIONTREE_H

#define EXPRESSIONTREE_H

#include <stdexcept>

#include <iostream>

using namespace std;

template <typename DataType>

class ExprTree {

public:

// Constructor

ExprTree ();

ExprTree(const ExprTree& source);

ExprTree& operator=(const ExprTree& source);

// Destructor

~ExprTree ();

// Expression tree manipulation operations

void build ();

void expression () const;

DataType evaluate() const throw (logic_error);

void clear (); // Clear tree

void commute();

bool isEquivalent(const ExprTree& source) const;

   bool isEmpty() const;

// Output the tree structure -- used in testing/debugging

void showStructure () const;

private:

class ExprTreeNode {

public:

// Constructor

ExprTreeNode ( char elem,

   ExprTreeNode *leftPtr, ExprTreeNode *rightPtr );

// Data members

char dataItem; // Expression tree data item

ExprTreeNode *left, // Pointer to the left child

   *right; // Pointer to the right child

};

// Recursive helper functions for the public member functions -- insert

// prototypes of these functions here.

void buildHelper(ExprTreeNode*& p);

void exprHelper(ExprTreeNode* p) const;

DataType evalHelper(ExprTreeNode* p) const;

void clearHelper ( ExprTreeNode *p );

void showHelper ( ExprTreeNode *p, int level ) const;

void copyHelper ( ExprTreeNode *&p );

void commuteHelper(ExprTreeNode* p);

bool isEquivalentHelper(const ExprTreeNode* p,

              const ExprTreeNode* q) const;

// Data member

ExprTreeNode *root; // Pointer to the root node

};

#endif       // #ifndef EXPRESSIONTREE_H

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.