二叉树加括号求值

#include <iostream>
using namespace std;
struct TreeNode{
    char value;
    TreeNode *leftChild;
    TreeNode *rightChild;
    TreeNode():leftChild(NULL),rightChild(NULL){}
    TreeNode(char v,TreeNode *l=NULL,TreeNode *r=NULL):value(v),leftChild(l),rightChild(r){}
};
class Tree{
private:
    char endTag;
    TreeNode *root;
    void createTree(TreeNode *&subTree);
    void inOrder(TreeNode *subTree);
    int getCount(TreeNode *subTree);
    TreeNode* parent(TreeNode *subTree,TreeNode *current);
public:
    Tree(char endT);
    void createTree(){
        createTree(root);
    }
    void inOrder(){
        inOrder(root);
    }
    int getCount(){
        return getCount(root);
    }
    TreeNode* parent(TreeNode *current){
        return parent(root,current);
    }
};
Tree::Tree(char endT){
    endTag=endT;
    root=NULL;
}
void Tree::createTree(TreeNode *&subTree){
    char item;
    cin>>item;
    if(item!=endTag){
        subTree=new TreeNode(item);
        createTree(subTree->leftChild);
        createTree(subTree->rightChild);
    }
}
void Tree::inOrder(TreeNode *subTree){
    if(subTree!=NULL){
    if ((subTree->value == '*' || subTree->value == '/')
        && (subTree->leftChild->value == '+' || subTree->leftChild->value == '-')
        &&(subTree->rightChild->value == '-' || subTree->rightChild->value == '+')) {
			int f = 1;
			cout << "(";
			inOrder(subTree->leftChild);
			cout << ")";
			cout << subTree->value;
			if (f == 1) {
				cout << "(";
				f = 0;
			}
			inOrder(subTree->rightChild);
			cout << ")";
     }
	else if ((subTree->value == '*' || subTree->value == '/')
          && (subTree->rightChild->value == '-'|| subTree->rightChild->value == '+')) {
			inOrder(subTree->leftChild);
			cout << subTree->value;
            cout << "(";
            inOrder(subTree->rightChild);
			cout << ")";
	}
    else if ((subTree->value == '*' || subTree->value == '/')
          && (subTree->leftChild->value == '-'|| subTree->leftChild->value == '+')) {
			inOrder(subTree->leftChild);
			cout << subTree->value;
            cout << "(";
            inOrder(subTree->rightChild);
			cout << ")";
	}
	else {

			inOrder(subTree->leftChild);
			cout << subTree->value;
			inOrder(subTree->rightChild);
	}

	}
}
int Tree::getCount(TreeNode *subTree){
    if(subTree!=NULL){
        if(subTree->value=='+'){
            return getCount(subTree->leftChild)+getCount(subTree->rightChild);
        }
        else if(subTree->value=='-'){
            return getCount(subTree->leftChild)-getCount(subTree->rightChild);
        }
        else if(subTree->value=='*'){
            return getCount(subTree->leftChild)*getCount(subTree->rightChild);
        }
        else if(subTree->value=='/'){
            return getCount(subTree->leftChild)/getCount(subTree->rightChild);
        }
        else{
            int temp=subTree->value-'0';
            return temp;
        }
    }
}
TreeNode* Tree::parent(TreeNode *subTree,TreeNode  *current){
    if(subTree==NULL)
        return NULL;
    if(subTree->leftChild==current||subTree->rightChild==current)
        return subTree;
    TreeNode *p=parent(subTree->leftChild,current);
    if(p)
        return p;
    else
        return (parent(subTree->rightChild,current));
}
int main(){
    Tree t('@');
    t.createTree();
    t.inOrder();
    cout<<'='<<t.getCount();
    return 0;
}

posted @ 2018-05-02 20:51  UnderScrutiny  阅读(172)  评论(0编辑  收藏  举报