zoukankan      html  css  js  c++  java
  • 二叉树加括号求值

    #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;
    }
    

  • 相关阅读:
    oracle课堂笔记---第十九天
    oracle课堂笔记--第十八天
    oracle课堂随笔--第十七天
    oracle课堂随笔--第十六天
    oracle课堂随笔--第十五天
    oracle课堂随笔--第十四天
    oracle课堂笔记--第十三天
    oracle课堂随笔--第十一天
    Beta冲刺预备
    个人作业——软件产品分析
  • 原文地址:https://www.cnblogs.com/wangjianupc/p/10587208.html
Copyright © 2011-2022 走看看