zoukankan      html  css  js  c++  java
  • 二叉树叶子数目计算

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<stack>
    using namespace std;

    typedef struct tree{
        char data;
        tree *lchild;
        tree *rchild;
    }Bitree, *pBitree;

    stack<char>sta1;
    stack<char>sta2;


    void creat_tree(pBitree &root)
    {
        char ch = '#';
        if (!sta1.empty())
        {
            ch = sta1.top();
            sta1.pop();
        }
        root = new Bitree;
        root->data = ch;
        if (ch != '+' && ch != '-'  &&  ch != '*'  &&  ch != '/')
        {
            root->rchild = NULL;
            root->lchild = NULL;
            return;
        }
        else
        {
            creat_tree(root->rchild);
            creat_tree(root->lchild);
        }

    }
    int judge(char t)
    {
        char ch = '#';
        ch = sta2.top();
        switch (t)
        {
        case '+':
        case '-':
        {
            if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                return 1;
            else
                return 0;
            break;
        }
        case'*':
        case'/':
        {
            if (ch == '*' || ch == '/')
                return 1;
            else
                return 0;
        }
        }

    }
    void turn_postorder(char *str)    //变为后缀表达式
    {
        char ch = '#';

        for (int i = 0; i < strlen(str); i++)
        {
            if ('0' <= str[i] && str[i] <= '9')
            {
                sta1.push(str[i]);
            }
            else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
            {
                while (!sta2.empty() && judge(str[i]))
                {
                    ch = sta2.top();
                    sta1.push(ch);
                    sta2.pop();
                }
                sta2.push(str[i]);
            }
            else if (str[i] == '(')
            {
                sta2.push(str[i]);
            }
            else if (str[i] == ')')
            {
                while ((ch = sta2.top()) != '(')
                {
                    sta1.push(ch);
                    sta2.pop();
                }
                sta2.pop();
            }
        }
        while (!sta2.empty())
        {
            ch = sta2.top();
            sta2.pop();
            sta1.push(ch);
        }

        /*while (!sta1.empty())
        {
        ch = sta1.top();
        printf("%c", ch);
        sta1.pop();
        }*/
    }

    void LeafCount(pBitree &root,int *&p)
    {
        if (root)
        {
            if (root->lchild == NULL || root->rchild==NULL)
                *p+=1;
            
            LeafCount(root->lchild,p);
            LeafCount(root->rchild,p);
        }
    }
    int main()
    {
        char str[50], res;
        int r;
        pBitree root = NULL;
        int c = 0;
        int *p = &c;
        while (1){
            printf("请输入表达式:");
            scanf("%s", &str);

            turn_postorder(str);

            creat_tree(root);

            LeafCount(root,p);
            printf("%d",*p);
        }
        system("pause");
        return 0;
    }

  • 相关阅读:
    Commando War (贪心)
    Codehorses T-shirts (map+遍历)
    HDU
    HDU—2021-发工资咯(水题,有点贪心的思想)
    HDU
    CSDN自定义栏目代码
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/da-peng/p/4963936.html
Copyright © 2011-2022 走看看