int main() { int result; // for result of expression char OP; // temporary for operator symbol char symbol[100]; // to store expression symbols while (cin >> symbol) { int symboltype = classify(symbol); switch (symboltype) { case oprand: operandstack.push(atoi(symbol)); // oprand is pushed break; // on operand stack case openparen: operatorstack.push('('); // open paren is pushed break; // on operator stack case closeparen: while(true) // pop and apply operators { operatorstack.pop(OP); // until open paren is if (OP=='(') break; // found. Pop it else apply(OP); } break; case oprator: while(true) { // pop and apply if (operatorstack.isempty()) break; // operators operatorstack.gettop(OP); // until stack is if (priority(OP) < priority(symbol[0])) // empty or top break; // has lower operatorstack.pop(OP); // priority than apply(OP); // operator just } // read operatorstack.push(symbol[0]); } } while (operatorstack.pop(OP)) apply(OP); // pop and apply any operators operandstack.pop(result); // top operand is result cout << result << endl; return 0; }
void AVLTree::balance(stack S) // Balance the tree given the stack of pointers { tnode *temp=NULL; for( temp=NULL; !S.isempty() ; temp = S.gettop(),S.pop() ) { if( balance_fact(S.gettop()) <-1 ) { if( balance_fact(temp) > 0 ) { cout<<" Right at " <<temp->data<<" + Left at " <<S.gettop()->data<<" \n"; right_rotate(temp); left_rotate( S.gettop() ); } else { cout<<" Left at "<< S.gettop()->data<<"\n"; left_rotate( S.gettop() ); } } if( balance_fact(S.gettop()) >1 ) { if( balance_fact(temp) < 0 ) { cout<<" Left at " <<temp->data<<" + Right at " <<S.gettop()->data<<" \n"; left_rotate( temp ); right_rotate( S.gettop() ); } else { cout<<" Right at "<< S.gettop()->data<<"\n"; right_rotate( S.gettop() ); } } } return; }