tree--
用数组实现数--好像只能开一棵树
#include <vector> #include <cstdio> #include <cstring> #include <queue> const int root = 1; int cnt;//记录节点数量 const int max_nodenumber=256; int value[max_nodenumber], left[max_nodenumber], right[max_nodenumber]; bool have_value[max_nodenumber]; using namespace std; bool failed; char s[max_nodenumber+10]; void newtree() { cnt = root;//某一时刻只允许存在一棵树!!!!!!!!!!!!!!!!!!! left[root] = right[root] = NULL; have_value[root] = false; } int newnode() { cnt++; left[cnt] = right[cnt] = NULL; have_value[cnt] = false; return cnt; } void addnode(int v, char *s) { int n = strlen(s); int u = root; for (int i = 0;i < n - 1;i++) { if (s[i] == 'L') { if (left[u] == NULL)left[u] = newnode(); u = left[u]; } else if (s[i] == 'R') { if (right[u] == NULL)right[u] = newnode(); u = right[u]; } } if (have_value[u]==true)failed = true;//多次赋值 value[u] = v; have_value[u] = true;//标记已赋值 } void readinput() { failed = false;//新的开始,重设failed for (;;) { scanf("%s", s); if (!strcmp(s, "()"))break; int v; sscanf(s + 1, "%d", &v); addnode(v, strchr(s, ',') + 1); } } void remove_tree(int t) { if (t == NULL) { cnt = 0;return;} remove_tree(left[t]); remove_tree(right[t]); right[t] = left[t] = NULL; have_value[t] = false; } void bfs(vector<int> &ans,int start=root) { queue<int> q; q.push(start); while (!q.empty()) { int u = q.front(); q.pop(); if (!have_value[u])failed = true;//结点未赋值 ans.push_back(value[u]); if (left[u])q.push(left[u]); if (right[u])q.push(right[u]); } } vector<int> ans; int main(void) { //remember to memset() while (1) { newtree(); //memset(have_value, 0, sizeof(have_value)); //memset(left, 0, sizeof(left)); //memset(right, 0, sizeof(right));//value没必要清空,有have_value作为标记 ans.clear(); readinput(); bfs(ans); if(!failed) for (vector<int>::iterator it = ans.begin();it != ans.end();it++) printf("%d ", *it); else printf("%d",-1); printf("\n"); remove_tree(root); for (int i = 0;i < 30;i++) if (left[i]||right[i]||have_value[i])printf("未清除成功\n"); } return 0; } //链表实现 #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; vector<int> ans; const int maxn = 25; using namespace std; char s[maxn]; int failed; struct Node { bool have_value; int v; Node *left, *right; Node() :have_value(0), left(NULL), right(NULL){} }; Node *root; Node* newnode() { return new Node(); } void addnode(int v, char* s); bool read_input() { failed=false; root = newnode(); for (;;) { if (scanf("%s", s) != 1)return 0; if (!strcmp(s, "()"))break; int v; sscanf(&s[1], "%d", &v); addnode(v, strchr(s, ',') + 1); } return 1;//true } void addnode(int v, char *s) { int n = strlen(s); Node *u = root; for(int i=0;i<n-1;i++)//处理形如"LL)" { if (s[i] == 'L') { if (u->left==NULL)u->left = newnode(); u = u->left; } else if (s[i] == 'R') { if (u->right == NULL)u->right = newnode(); u = u->right; } } if (u->have_value == 1)failed = true;//重复赋值爆炸了,输入错误 u->v = v; u->have_value = true; } void bfs(vector<int> &ans) { queue<Node*> q; q.push(root); while (!q.empty()) { Node *t = q.front();q.pop(); if (t->have_value == 0)failed = true;//某一结点没有赋值,输入错误 if (t->left)q.push(t->left); if (t->right)q.push(t->right); ans.push_back(t->v); } } void remove_tree(Node *root) { if (root == NULL)return; remove_tree(root->left); remove_tree(root->right); delete root; } int main(void) { if (!read_input())failed = true; bfs(ans); if (!failed) { for (vector<int>::iterator it = ans.begin();it != ans.end();it++) printf("%d ", *it); printf("\n"); } else printf("%d", -failed); remove_tree(root); return 0; }