数据结构--后缀表达式转树(c++)

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <cctype>  //判断是否是数字、字母或、、

using namespace std;

struct BinaryNode
{
    char element;
    BinaryNode *left;
    BinaryNode *right;
};

typedef BinaryNode * BinaryNodeptr;

void deleteAllNode(BinaryNodeptr ptr)
{
    if (ptr->left!=NULL)
    {
        deleteAllNode(ptr->left);
    }
    else if (ptr->right!=NULL)
    {
        deleteAllNode(ptr->right);
    }
    else
    {
        delete ptr;
        ptr = NULL;
    }
}

class expression_to_tree
{
public:
    expression_to_tree(vector<char> str);
    BinaryNodeptr createNewNode();
    void printTree(BinaryNodeptr node);

    ~expression_to_tree();

    BinaryNodeptr root;

protected:
private:
    vector<char> str;
};

BinaryNodeptr expression_to_tree::createNewNode()
{
    BinaryNodeptr node = new BinaryNode;
    node->left = NULL;
    node->right = NULL;

    return node;
}

expression_to_tree::expression_to_tree(vector<char> str)
{
    stack<BinaryNodeptr> nodeStack;  //保存除运算符之外的字符
    BinaryNodeptr temp_node_ptr;

    char c;

    for (int i = 0; i < str.size(); i++)
    {
        cout << "输入的字符为:" << str[i] <<endl;
        c = str[i];
        temp_node_ptr = createNewNode();
        //temp_node_ptr->element = c;

        if (!(c == '+' || c == '-' || c == '*' || c == '/'))
        {
            cout << "推入栈的字符:" <<c<< endl;
            temp_node_ptr->element = c;
            nodeStack.push(temp_node_ptr);
        }
        else
        {
            cout << "推入栈的字符:" << c << endl;
            root = createNewNode();
            BinaryNodeptr leftNode ;
            BinaryNodeptr rightNode ;

            root->element = c;
            rightNode = nodeStack.top();
            nodeStack.pop();

            leftNode = nodeStack.top();
            nodeStack.pop();

            root->left = leftNode;
            root->right = rightNode;

            nodeStack.push(root);
            cout << "根结点:" << root->element << endl;
        }
    }
}

void expression_to_tree::printTree(BinaryNodeptr node)
{
    if (node==NULL)
    {
        return;
    }
    else
    {
        printTree(node->left);
        cout << node->element << endl;
        printTree(node->right);
    }
}

expression_to_tree::~expression_to_tree()
{
    deleteAllNode(root);
}

int main(void)
{
    vector<char> str;
    char c;

    BinaryNodeptr tree;

    cout << "Please input expression:" << endl;
    cout << "按q结束输入:" << endl;

    //输入后缀表达式
    c = getchar();
    while (c!='q')
    {
        if (c == '+' || c == '-' || c == '*' || c == '/' || isalnum(c))
        {
            str.push_back(c);
            getchar();
            c = getchar();
        }
        else
        {
            cout << "请输入字母、数字、或运算符" << endl;
            return false;
        }
    }

    expression_to_tree express(str);
    tree = express.root;

    express.printTree(tree);
    
    system("pause");
    return 0;
}

转载于:https://my.oschina.net/u/3397950/blog/1526889

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值