码迷,mamicode.com
首页 > 编程语言 > 详细

C++ —— 后缀表达式转表达式树

时间:2018-03-13 01:07:28      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:int   pac   destory   error   tree   ror   stack   时间   tor   

  写得时候思路还是很清晰的,所以没花费多久便基本实现了,不过过程还是遇到一些bug。

  版本1:

#include <iostream>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;

class tree {
public:
	char val;
	tree * leftchild;
	tree * rightchild;
};

class ExpressionTree {
public:
	void build_expression(tree * & root, string str);
	void proorder_print(tree * root);
	void inorder_print(tree * root);
	void postorder_print(tree * root);
	void show(tree * root);
	void destory(tree * & root);
};

void ExpressionTree::build_expression(tree * & root, string str)
{
	stack<tree *> treestack;
	for (auto c : str)
	{
		tree*node = new tree;
		node->val = c;
		node->leftchild = nullptr;
		node->rightchild = nullptr;
		
		if (c >= ‘0‘ && c <= ‘9‘ || c >= ‘a‘ && c <= ‘z‘)
			treestack.push(node);
		else if (c == ‘+‘ || c == ‘-‘ || c == ‘*‘ || c == ‘/‘)
		{
			if (!treestack.empty())
			{
				node->rightchild = treestack.top();
				treestack.pop();
			}
			if (!treestack.empty())
			{
				node->leftchild = treestack.top();
				treestack.pop();
			}
			treestack.push(node);
		}
		else
		{
			cout << "表达式有误!" << endl;
			exit(1);
		}
	}
	root = treestack.top();
	while (!treestack.empty())
		treestack.pop();
}

void ExpressionTree::proorder_print(tree * root)
{
	if (root != nullptr)
	{
		cout << root->val;
		proorder_print(root->leftchild);
		proorder_print(root->rightchild);
	}
}

void ExpressionTree::inorder_print(tree * root)
{
	if (root != nullptr)
	{
		cout << ‘(‘;
		inorder_print(root->leftchild);
		cout << root->val;
		inorder_print(root->rightchild);
		cout << ‘)‘;
	}
}

void ExpressionTree::postorder_print(tree * root)
{
	if (root != nullptr)
	{
		postorder_print(root->leftchild);
		postorder_print(root->rightchild);
		cout << root->val;
	}
}

void ExpressionTree::show(tree * root)
{
	cout << "前缀表达式:" << endl;
	proorder_print(root);
	cout << endl;
	cout << "中缀表达式:" << endl;
	inorder_print(root);
	cout << endl;
	cout << "后缀表达式:" << endl;
	postorder_print(root);
	cout << endl;
}

void ExpressionTree::destory(tree * & root)
{
	if (root != nullptr)
	{
		destory(root->leftchild);
		destory(root->rightchild);
		delete root;
	}
}

int main()
{
	string str = "23+456+**";
	ExpressionTree res;
	tree * root;

	res.build_expression(root, str);
	res.show(root);
	res.destory(root);

	getchar();
        return 0;
}

  版本2:

#include <iostream>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;

class tree {
public:
	char val;
	tree * leftchild;
	tree * rightchild;
};

class ExpressionTree {
public:
	tree * root;

	ExpressionTree()
	{
		this->root = new tree;
	}
	~ExpressionTree()
	{
		destory(this->root);
	}
	void insert(char c, stack<tree *> & treestack);
	void build_expression(string str);
	void proorder_print(tree * root);
	void inorder_print(tree * root);
	void postorder_print(tree * root);
	void show();
	void destory(tree * root);
};

void ExpressionTree::insert(char c, stack<tree *> & treestack)
{
	tree*node = new tree;
	node->val = c;
	node->leftchild = nullptr;
	node->rightchild = nullptr;

	if (c >= ‘0‘ && c <= ‘9‘)
		treestack.push(node);
	else if (c == ‘+‘ || c == ‘-‘ || c == ‘*‘ || c == ‘/‘)
	{
		try {
			node->rightchild = treestack.top();
			treestack.pop();
			node->leftchild = treestack.top();
			treestack.pop();
		}
		catch (overflow_error) {
			cout << "表达式有误!";
			exit(1);
		}
		treestack.push(node);
	}
	else
	{
		cout << "表达式有误!" << endl;
		exit(1);
	}
}

void ExpressionTree::build_expression(string str)
{
	stack<tree *> treestack;
	for (auto s : str)
		insert(s, treestack);
	this->root = treestack.top();
	while (!treestack.empty())
		treestack.pop();
}

void ExpressionTree::proorder_print(tree * root)
{
	if (root != nullptr)
	{
		cout << root->val;
		proorder_print(root->leftchild);
		proorder_print(root->rightchild);
	}
}

void ExpressionTree::inorder_print(tree * root)
{
	if (root != nullptr)
	{
	    cout << ‘(‘;
		inorder_print(root->leftchild);
		cout << root->val;
		inorder_print(root->rightchild);
		cout << ‘)‘;
	}
}

void ExpressionTree::postorder_print(tree * root)
{
	if (root != nullptr)
	{
		postorder_print(root->leftchild);
		postorder_print(root->rightchild);
		cout << root->val;
	}
}

void ExpressionTree::show()
{
	cout << "前缀表达式:" << endl;
	proorder_print(this->root);
	cout << endl;
	cout << "中缀表达式:" << endl;
	inorder_print(this->root);
	cout << endl;
	cout << "后缀表达式:" << endl;
	postorder_print(this->root);
	cout << endl;
}

void ExpressionTree::destory(tree * root)
{
	if (root != nullptr)
	{
		destory(root->leftchild);
		destory(root->rightchild);
		delete root;
	}
}

int main()
{
	string str = "23+456+**";
	ExpressionTree res;

	res.build_expression(str);
	res.show();
	getchar();
        return 0;
}

  基本思路:建立树的方式类似由下而上建立堆的办法,所以时间复杂度为O(n),这样算法就会变得很简单,只用考虑处理栈中的节点即可。好像也没什么好说的了。

C++ —— 后缀表达式转表达式树

标签:int   pac   destory   error   tree   ror   stack   时间   tor   

原文地址:https://www.cnblogs.com/darkchii/p/8552358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!