括号转树的模板

电子书板子:






希冀平台:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>
typedef long long ll;
using namespace std;


//int ans = 0;
//struct BTNode
//{
//	int data;
//	int depth;
//	BTNode* lchild;
//	BTNode* rchild;
//	BTNode() { lchild = rchild = NULL; depth = 0; }
//	BTNode(int s)
//	{
//		lchild = rchild = NULL;
//		depth = 0;
//		data = s;
//	}
//};
//class Btree
//{
//public:
//	BTNode* r;
//	Btree() { r = NULL; }
//	void CreateBtree(string str)
//	{
//		stack<BTNode*>st;
//		BTNode* p;
//		bool flag = 0;
//		int i = 0;
//		int his = 0;
//		int ans = 0;
//		while (i < str.length())
//		{
//			switch (str[i])
//			{
//			case'(':
//				st.push(p);
//				flag = true;
//				break;
//			case')':
//				st.pop();
//				break;
//			case',':
//				flag = false;
//				break;
//			default:
//				ans = ans * 10 + str[i] - '0';
//				if(i+1<str.length() and (str[i+1]>'9' or str[i+1]<'0'))
//				{
//					p = new BTNode(ans);
//					ans = 0;
//					if (r == NULL)r = p;
//					else
//					{
//						if (flag and !st.empty())
//							st.top()->lchild = p;
//						else if (!st.empty())
//							st.top()->rchild = p;
//					}
//				}
//				break;
//			}
//			i++;
//		}
//		return;
//	}
//	void init(BTNode *s)
//	{
//		if (s->lchild and s->rchild)
//		{
//			init(s->lchild); init(s->rchild);
//			s->depth = max(s->lchild->depth, s->rchild->depth) + 1;
//
//		}
//		else if (s->lchild)
//		{
//			init(s->lchild);
//			s->depth = s->lchild->depth + 1;
//		}
//		else if (s->rchild)
//		{
//			init(s->rchild);
//			s->depth = s->rchild->depth + 1;
//		}
//		else s->depth = 0;
//		return;
//	}
//	void findans(BTNode* rx)
//	{
//		if (rx->lchild and rx->rchild)
//		{
//			ans = max(ans, rx->lchild->depth + rx->rchild->depth + 2);
//			findans(rx->lchild); findans(rx->rchild);
//		}
//		else if (rx->lchild)
//		{
//			ans = max(ans, rx->lchild->depth + 1);
//			findans(rx->lchild);
//		}
//		else if (rx->rchild)
//		{
//			ans = max(ans, rx->rchild->depth + 1);
//			findans(rx->rchild);
//		}
//		return ;
//	}
//};
//
//int main()
//{
//	freopen("in.txt", "r", stdin);
//	string s;
//	cin >> s;
//	Btree B;
//	B.CreateBtree(s);
//	B.init(B.r);
//	B.findans(B.r);
//	cout << ans;
//	return 0;
//}




/*t1:record*/
struct BTNode
{
	int data;
	BTNode* lchild;
	BTNode* rchild;
	BTNode() { lchild = rchild = NULL; }
	BTNode(int d)
	{
		data = d;
		lchild = rchild = NULL;
	}
};
class BTree
{
public:
	BTNode* r; queue<int>q1; stack<int>s2;
	BTree() { r = NULL; }
	void CreateBTree(string str)
	{
		stack<BTNode*>st;
		BTNode* p;
		int ans = 0;
		bool flag;
		int i = 0;
		while (i < str.length())
		{
			switch (str[i])
			{
			case '(':
				st.push(p);
				flag = true;
				break;
			case ')':
				st.pop();
				break;
			case ',':
				flag = false;
				break;
			default:
				ans = ans * 10 + str[i] - '0';
				if(i+1<str.length() and (str[i+1]>'9' or str[i+1]<'0'))
				{
					p = new BTNode(ans);
					ans = 0;
					if (r == NULL)r = p;
					else
					{
						if (flag and !st.empty())
							st.top()->lchild = p;
						else if (!st.empty())
							st.top()->rchild = p;
					}
				}
				break;
			}
			i++;
		}
	}
	void bljd(BTNode *now)
	{
		if (now->lchild != NULL)bljd(now->lchild);
		if (now->rchild != NULL)bljd(now->rchild);
		if (now->lchild == NULL and now->rchild == NULL)q1.push(now->data);
		return;
	}
	void ques1and2()
	{
		BTNode* now = r;
		bljd(now);
		while (q1.size() > 1)
		{
			cout << q1.front() << ' ';
			s2.push(q1.front());
			q1.pop();
		}
		cout << q1.front() << endl << q1.front() << ' ';
		while (s2.size() > 1)
		{
			cout << s2.top() << ' ';
			s2.pop();
		}
		cout << s2.top() << endl;

	}
	void ques3()
	{
		queue<int>ansq;
		queue<BTNode*>tmp;
		tmp.push(r);
		while (!tmp.empty())
		{
			BTNode* rx = tmp.front();
			tmp.pop();
			ansq.push(rx->data);
			if (rx->rchild)tmp.push(rx->rchild);
			if (rx->lchild)tmp.push(rx->lchild);
			
		}
		while (!ansq.empty())
		{
			cout << ansq.front() << ' ';
			ansq.pop();
		}
		return;
	}

};
int main()
{
	freopen("in.txt", "r", stdin);
	string s;
	cin >> s;
	BTree a;
	a.CreateBTree(s);
	a.ques1and2();
	a.ques3();
	return 0;
}

posted on 2024-05-04 17:06  WHUStar  阅读(1)  评论(0编辑  收藏  举报