|
| 1 | +//ExpressionTree.cpp : Program for Expression Tree. |
| 2 | + |
| 3 | +#include<iostream> |
| 4 | +#include<string> |
| 5 | +#include<stack> |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +class Node |
| 9 | +{ |
| 10 | + public: |
| 11 | + char info; |
| 12 | + Node *lchild; |
| 13 | + Node *rchild; |
| 14 | + |
| 15 | + Node(char ch) |
| 16 | + { |
| 17 | + info = ch; |
| 18 | + lchild = NULL; |
| 19 | + rchild = NULL; |
| 20 | + } |
| 21 | + |
| 22 | +};//End of class Node |
| 23 | + |
| 24 | +class ExpressionTree |
| 25 | +{ |
| 26 | + private: |
| 27 | + Node *root; |
| 28 | + bool isOperator(char ch); |
| 29 | + void preorder(Node *ptr); |
| 30 | + void postorder(Node *ptr); |
| 31 | + void inorder(Node *ptr); |
| 32 | + void display(Node *ptr, int level); |
| 33 | + int evaluate(Node *ptr); |
| 34 | + void destroy(Node *ptr); |
| 35 | + |
| 36 | + public: |
| 37 | + ExpressionTree(); |
| 38 | + ~ExpressionTree(); |
| 39 | + void buildTree(string postfix); |
| 40 | + void prefix(); |
| 41 | + void postfix(); |
| 42 | + void parenthesizedInfix(); |
| 43 | + void display(); |
| 44 | + int evaluate(); |
| 45 | + |
| 46 | +};//End of class ExpressionTree |
| 47 | + |
| 48 | +ExpressionTree::ExpressionTree() |
| 49 | +{ |
| 50 | + root = NULL; |
| 51 | +}//End of ExpressionTree() |
| 52 | + |
| 53 | +ExpressionTree::~ExpressionTree() |
| 54 | +{ |
| 55 | + destroy(root); |
| 56 | + root = NULL; |
| 57 | +}//End of ExpressionTree() |
| 58 | + |
| 59 | +void ExpressionTree::destroy(Node *ptr) |
| 60 | +{ |
| 61 | + if(ptr == NULL) |
| 62 | + return; |
| 63 | + destroy(ptr->lchild); |
| 64 | + destroy(ptr->rchild); |
| 65 | + |
| 66 | + delete ptr; |
| 67 | +}//End of destroy() |
| 68 | + |
| 69 | +bool ExpressionTree::isOperator(char c) |
| 70 | +{ |
| 71 | + if(c=='+' || c=='-' || c=='*' || c=='/') |
| 72 | + return true; |
| 73 | + return false; |
| 74 | +}//End of isOperator(); |
| 75 | + |
| 76 | +void ExpressionTree::buildTree(string postfix) |
| 77 | +{ |
| 78 | + stack<Node *> treeStack; |
| 79 | + Node *t; |
| 80 | + |
| 81 | + for(int i=0; i<postfix.length(); i++) |
| 82 | + { |
| 83 | + if(isOperator(postfix[i])) |
| 84 | + { |
| 85 | + t = new Node(postfix[i]); |
| 86 | + t->rchild = treeStack.top(); |
| 87 | + treeStack.pop(); |
| 88 | + t->lchild = treeStack.top(); |
| 89 | + treeStack.pop(); |
| 90 | + treeStack.push(t); |
| 91 | + } |
| 92 | + else //operand |
| 93 | + { |
| 94 | + t = new Node(postfix[i]); |
| 95 | + treeStack.push(t); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + root = treeStack.top(); |
| 100 | + treeStack.pop(); |
| 101 | + |
| 102 | +}//End of buildTree() |
| 103 | + |
| 104 | +void ExpressionTree::preorder(Node *ptr) |
| 105 | +{ |
| 106 | + if(ptr == NULL) //Base case |
| 107 | + return; |
| 108 | + |
| 109 | + cout << ptr->info; |
| 110 | + preorder(ptr->lchild); |
| 111 | + preorder(ptr->rchild); |
| 112 | +}//End of preorder() |
| 113 | + |
| 114 | +void ExpressionTree::prefix() |
| 115 | +{ |
| 116 | + preorder(root); |
| 117 | + cout << "\n"; |
| 118 | +}//End of prefix() |
| 119 | + |
| 120 | +void ExpressionTree::postorder(Node *ptr) |
| 121 | +{ |
| 122 | + if(ptr == NULL) //Base case |
| 123 | + return; |
| 124 | + |
| 125 | + postorder(ptr->lchild); |
| 126 | + postorder(ptr->rchild); |
| 127 | + cout << ptr->info; |
| 128 | +}//End of postorder() |
| 129 | + |
| 130 | +void ExpressionTree::postfix() |
| 131 | +{ |
| 132 | + postorder(root); |
| 133 | + cout << "\n"; |
| 134 | +}//End of postfix() |
| 135 | + |
| 136 | +void ExpressionTree::inorder(Node *ptr) |
| 137 | +{ |
| 138 | + if(ptr == NULL) //Base case |
| 139 | + return; |
| 140 | + |
| 141 | + if(isOperator(ptr->info)) |
| 142 | + cout << "("; |
| 143 | + |
| 144 | + inorder(ptr->lchild); |
| 145 | + cout << ptr->info; |
| 146 | + inorder(ptr->rchild); |
| 147 | + |
| 148 | + if(isOperator(ptr->info)) |
| 149 | + cout << ")"; |
| 150 | + |
| 151 | +}//End of inorder() |
| 152 | + |
| 153 | +void ExpressionTree::parenthesizedInfix() |
| 154 | +{ |
| 155 | + inorder(root); |
| 156 | + cout << "\n"; |
| 157 | +}//End of parenthesizedInfix() |
| 158 | + |
| 159 | +void ExpressionTree::display(Node *ptr, int level) |
| 160 | +{ |
| 161 | + if(ptr == NULL) |
| 162 | + return; |
| 163 | + |
| 164 | + display(ptr->rchild, level+1); |
| 165 | + cout << "\n"; |
| 166 | + |
| 167 | + for(int i=0; i<level; i++) |
| 168 | + cout << " "; |
| 169 | + cout << ptr->info; |
| 170 | + |
| 171 | + display(ptr->lchild, level+1); |
| 172 | + |
| 173 | +}//End of display() |
| 174 | + |
| 175 | +void ExpressionTree::display() |
| 176 | +{ |
| 177 | + display(root, 0); |
| 178 | + cout << "\n"; |
| 179 | +}//End of display() |
| 180 | + |
| 181 | +int ExpressionTree::evaluate(Node *ptr) |
| 182 | +{ |
| 183 | + if(!isOperator(ptr->info)) |
| 184 | + return ptr->info - 48; |
| 185 | + |
| 186 | + int leftValue = evaluate(ptr->lchild); |
| 187 | + int rightValue = evaluate(ptr->rchild); |
| 188 | + |
| 189 | + if(ptr->info == '+') |
| 190 | + return leftValue + rightValue; |
| 191 | + else if(ptr->info == '-') |
| 192 | + return leftValue - rightValue; |
| 193 | + else if(ptr->info == '*') |
| 194 | + return leftValue * rightValue; |
| 195 | + else if(ptr->info == '/') |
| 196 | + return leftValue / rightValue; |
| 197 | + |
| 198 | +}//End of evaluate() |
| 199 | + |
| 200 | +int ExpressionTree::evaluate() |
| 201 | +{ |
| 202 | + if(root == NULL) |
| 203 | + return 0; |
| 204 | + |
| 205 | + return evaluate(root); |
| 206 | +}//End of evaluate() |
| 207 | + |
| 208 | +int main() |
| 209 | +{ |
| 210 | + ExpressionTree expTree; |
| 211 | + |
| 212 | + string postfix = "54+12*3*-"; |
| 213 | + |
| 214 | + expTree.buildTree(postfix); |
| 215 | + expTree.display(); |
| 216 | + |
| 217 | + cout << "Prefix : "; |
| 218 | + expTree.prefix(); |
| 219 | + |
| 220 | + cout << "Postfix : "; |
| 221 | + expTree.postfix(); |
| 222 | + |
| 223 | + cout << "Infix : "; |
| 224 | + expTree.parenthesizedInfix(); |
| 225 | + |
| 226 | + cout << "Evaluated Value : " << expTree.evaluate() << "\n"; |
| 227 | + |
| 228 | + return 0; |
| 229 | +}//End of main() |
| 230 | + |
0 commit comments