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;
        int ltag;
        int rtag;
        tree *lchild;
        tree *rchild;
    }Bitree, *pBitree;

    pBitree pre = NULL;
    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->rtag = 1;
            root->ltag = 1;
            root->lchild = NULL;
            root->rtag = 1;
            root->ltag = 1;
            return;
        }
        else
        {
            creat_tree(root->rchild);
            root->ltag = root->rtag = 0;
            creat_tree(root->lchild);
            root->ltag = root->rtag = 0;
        }

    }
    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();
        }*/
    }
    char result(pBitree root)
    {
        char num1, num2;
        int n1, n2;
        switch (root->data)
        {
        case '+':
            num1 = result(root->lchild); n1 = num1 - '0';
            num2 = result(root->rchild); n2 = num2 - '0';
            root->data = n1 + n2 + '0';
            break;
        case'-':
            num1 = result(root->lchild); n1 = num1 - '0';
            num2 = result(root->rchild); n2 = num2 - '0';
            root->data = n1 - n2 + '0';
            break;
        case '*':
            num1 = result(root->lchild); n1 = num1 - '0';
            num2 = result(root->rchild); n2 = num2 - '0';
            root->data = n1*n2 + '0';
            break;
        case '/':
            num1 = result(root->lchild); n1 = num1 - '0';
            num2 = result(root->rchild); n2 = num2 - '0';
            root->data = n1 / n2 + '0';
            break;
        }
        return root->data;
    }
    void increat_thtree(pBitree &root)
    {

        if (root != NULL)
        {
            increat_thtree(root->lchild);
            if (!root->lchild)
            {
                root->ltag = 1;
                root->lchild = pre;
            }
            if (!pre->rchild)
            {
                pre->rtag = 1;
                pre->rchild = root;
            }
            pre = root;
            increat_thtree(root->rchild);
        }
    }
    void in_thread_head(pBitree &root)
    {
        pBitree p;
        p = pre;
        pre->lchild = root;
        pre->ltag = 0;
        increat_thtree(root);//中序线索化
        root=pre;
        pre = p;
        pre->rchild = root;
        pre->rtag = 1;
        root->rchild = pre;
    }
    void inorder_thread(pBitree p)
    {
        pBitree root;
        root = p->lchild;
        while (root!=p)
        {
            while (root->ltag == 0)
                root = root->lchild;
            printf("%c", root->data);
            while (root->rtag == 1 && root->rchild != p)
            {
                root = root->rchild;
                printf("%c", root->data);
            }
            root = root->rchild;
        }
    }
    int main()
    {
        char str[50], res;
        int r;
        pBitree root = NULL,p=NULL;
        pre = new Bitree;
        pre->data = '#';
        p = pre;
        printf("请输入表达式:");
        scanf("%s", &str);

        turn_postorder(str);//转为后缀表达式

        creat_tree(root);//后缀表达式建树
        in_thread_head(root);//加入头结点,并线索化
        inorder_thread(p);//线索中序输出

        
        system("pause");
        return 0;
    }

    参考   http://blog.chinaunix.net/uid-26548237-id-3476920.html

  • 相关阅读:
    Linux/Android 系统怎么修改mac地址
    Vue.js和jQuery混合使用的一点注意事项
    wpf研究之道——datagrid控件及样式
    asp.net url重写
    如何解决一个问题
    Word 2007 封面、目录和正文页码单独设置
    .net 多线程
    我对asp.net运行机制的理解
    Firefox扩展安装
    谷歌chrome 插件(扩展)开发——谈谈安装
  • 原文地址:https://www.cnblogs.com/da-peng/p/4946946.html
Copyright © 2011-2022 走看看