-
-
Save anonymous/4391b3eb256b1299778bc2c376211241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//Constructors and destructors | |
ETreeNode::ETreeNode(char op, ETreeNode* lchild, ETreeNode* rchild): | |
m_bIsOperator(true), m_chOp(op), m_pLeftChild(lchild), m_pRightChild(rchild){ | |
} //constructor | |
ETreeNode::ETreeNode(unsigned int val): | |
m_bIsOperator(false), m_nValue(val), m_pLeftChild(NULL), m_pRightChild(NULL){ | |
} //constructor | |
ETreeNode::~ETreeNode(){ | |
delete m_pLeftChild; | |
delete m_pRightChild; | |
} //destructor | |
///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//Tree traversal functions | |
/// \brief Preorder traversal. | |
/// Perform a preorder traversal starting at the current node | |
/// and append the result to a string. | |
/// \param s String to append result of traversal to. | |
/// \param n Length of s. | |
/// \param len Maximum length of s. | |
/// \return false if the postfix expression fits into s, otherwise true. | |
bool ETreeNode::preorder(char* s, int& n, const int len){ | |
bool error = false; | |
//YOUR CODE GOES HERE | |
if(m_bIsOperator) | |
{ //operator node | |
s[n++] = m_chOp; //append operator character to s | |
s[n++] = '\0'; //zero terminator | |
} //if | |
else | |
{ //number node | |
char buffer[32]; //temporary character buffer | |
sprintf(buffer, "%d", m_nValue); //print number into buffer | |
strcat((char*)(s + n), buffer); //append buffer to s | |
} //else | |
return error; | |
} //preorder | |
/// \brief Postorder traversal. | |
/// Perform a postorder traversal starting at the current node | |
/// and append the result to a string. | |
/// \param s String to append result of traversal to. | |
/// \param n Length of s. | |
/// \param len Maximum length of s. | |
/// \return false if the postfix expression fits into s, otherwise true | |
bool ETreeNode::postorder(char* s, int& n, const int len){ | |
bool error = false; | |
//YOUR CODE GOES HERE | |
return error; | |
} //postorder | |
/// \brief Inorder traversal. | |
/// Perform a inorder traversal starting at the current node | |
/// and append the result to a string. Fully parenthesize while you're doing it. | |
/// \param s String to append result of traversal to. | |
/// \param n Length of s. | |
/// \param len Maximum length of s. | |
/// \return false if the postfix expression fits into s, otherwise true. | |
bool ETreeNode::inorder(char* s, int& n, const int len){ | |
bool error = false; | |
//YOUR CODE GOES HERE | |
return error; | |
} //inorder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ETreeNode{ | |
private: | |
char m_chOp; ///< Character representing an operator. | |
unsigned int m_nValue; ///< A number. | |
bool m_bIsOperator; ///< Whether this node represents an operator. | |
ETreeNode* m_pLeftChild; ///< Pointer to left child. | |
ETreeNode* m_pRightChild; ///< Pointer to right child. | |
public: | |
ETreeNode(char op, ETreeNode* lchild, ETreeNode* rchild); ///< Constructor for operator node. | |
ETreeNode(unsigned int val); ///< Constructor for number node. | |
~ETreeNode(); ///< Destructor. | |
bool preorder(char* s, int& n, const int len); ///< Preorder traversal. | |
bool postorder(char* s, int& n, const int len); ///< Postorder traversal. | |
bool inorder(char* s, int& n, const int len); ///< Inorder traversal. | |
}; //ETreeNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arithmetic Expression: (3+4)/(5+6*7) | |
Infix: | |
Prefix: / | |
Postfix: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment