• 表达式树的创建与输出


    题目要求:(点击图片查看)

    题目要求:根据前序序列建立表达式数并输出表达式。

    这里可以巧妙的使用递归算法解决问题。

    这里主要是必须要理清操作符和操作数的关系。所有操作数都为叶子节点,操作符为双亲节点或者根节点。遇到'#'符号停止递归。

    来自参考:https://blog.csdn.net/qq_41061455/article/details/80553648

    源码与注释:

    #include <bits/stdc++.h>
    //#include <iostream>
    //#include <stdio.h>
    
    using namespace std;
    string s;
    
    typedef struct Node{
        int data;
        struct Node* left;
        struct Node* right;
    }Node;
    
    void Build(Node* &tree){//建立二叉树
        cin>>s;
        int i=0,sn=0,len=0;
        if(s[0]=='#')   tree=NULL;          //空叶子
        else{                               //节点操作判断
            if(s[0]=='+')   sn=-1;
            else if(s[0]=='-')  sn=-2;
            else if(s[0]=='*')  sn=-3;
            else if(s[0]=='/')  sn=-4;
            else{                           //叶子节点操作数计算
                len=s.size();
                //printf("%d
    ",len);       //输出字符串s的长度
                while(i<len){
                    sn=sn*10+(s[i]-'0');
                    i++;
                }
            }
            tree=new Node;
            tree->data=sn;
            Build(tree->left);          //递归建立左子树
            Build(tree->right);         //递归建立右子树
        }
    }
    
    void Print_Tree(Node* tree){        //打印表达式
        if(tree){
            if(tree->data>=0)   printf("%d",tree->data);   //如果遇到叶子节点直接输出
            else{
                printf("(");                                //打印左括号
                Print_Tree(tree->left);                     //打印左子树
                if(tree->data==-1) printf("+");             //打印操作符
                else if(tree->data==-2) printf("-");
                else if(tree->data==-3) printf("*");
                else if(tree->data==-4) printf("/");
                Print_Tree(tree->right);                    //打印右子树
                printf(")");                                //打印右括号
            }
        }
    }
    
    int main()
    {
        //char ch;
        while(cin>>s)         //输入string字符串,默认空格结束
        {
            //cout<<s;
            int i=0,sn=0,len=0;
            if(s[0]=='+')   sn=-1;          //操作符判断
            else if(s[0]=='-')  sn=-2;
            else if(s[0]=='*')  sn=-3;
            else if(s[0]=='/')  sn=-4;
            else{                           //操作数判断
                len=s.size();
                //printf("%d
    ",len);
                while(i<len){
                    sn=sn*10+(s[i]-'0');
                    i++;
                }
            }
            Node* tree=new Node;
            tree->data=sn;          //叶子节点
            Build(tree->left);      //递归建立左子树
            Build(tree->right);     //递归建立右子树
            Print_Tree(tree);       //打印表达式
            printf("
    ");           //输出空行
        }
        return 0;
    }
  • 相关阅读:
    动态加载JS脚本【转】
    定义并且立即执行JS匿名函数拾遗
    javascript操作ASCII码与字符对转
    win7的mklink命令
    [Yii Framework] How to get the current static page name?
    [Ubuntu] 利用Ubuntu光盘破解win7用户登录 Crark the win7 user via Ubuntu live CD
    [Ubuntu] reload the .bashrc file without logout nor restart.
    [Ubuntu] the permissions of lampp mysql and phpmyadmin
    [Zend PHP5 Cerification] Some note when studying
    [eZ publish] How to modify the $view_parameters valus in the template.
  • 原文地址:https://www.cnblogs.com/jxxclj/p/9251865.html
Copyright © 2020-2023  润新知