• 二叉树加括号求值


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

  • 相关阅读:
    购物车
    加载网格X文件代码(Unicode版本)
    金字塔代码
    纹理贴图案例
    绘制立方体
    移动端H5开发问题(html2canvas、video、audio)
    opencvpython图像处理学习笔记1
    jmeter报错o.a.j.JMeter: Uncaught exception问题排查
    记一次测试周报
    PHP 浮点数计算精度问题
  • 原文地址:https://www.cnblogs.com/wangjianupc/p/10587208.html
Copyright © 2020-2023  润新知