目录
动态二叉树结构体定义与生成新结构点:
指针:
struct node{
bool have_value;
int v;
node *left,*right;
node():have_value(false),left(NULL),right(NULL){}
};
node* newnode(){
return new node();
}
数组:
//这里动态是指哪里需要结点取哪里,动态的不是内存,是结点位置
int left[maxn],right[maxn];
bool have_value[maxn];
int cnt;
int root = 0;
void newtree(){
left[root] = right[root] = 0;
have_value[root] = false;cnt=root;
}
int newnode(){
int u=++cnt;left[u] = right[u] = 0;have_value[u] = false;return u;
}
二叉树的层次遍历:
层次遍历,逐层遍历。用广度优先搜索Breadth-First Search(BFS)
UVA - 122 Trees on the level
函数的应用:
1.sscanf
sscanf(char*,format,自选变量对应地址);scanf是从stdin输入,这个就是从char*对应的字符串输入给自选变量
sscanf(&s[1],"%d",&v);是指将从s+1地址开始的包括+-号所表示数组转化为int型给变量v。
%[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
%[aB'] 匹配a、B、'中一员,贪婪性
%[^a] 匹配非a的任意字符,并且停止读入,贪婪性
2.strchr
char *strchr(const char *str, int c) 返回第一个字符串str中第一个字符c的地址。
指针代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+50;
struct node{
bool have_value;
int v;
node *left,*right;
node():have_value(false),left(NULL),right(NULL){}
};
node *root;
char s[maxn];
vector<int>ans;
node* newnode(){
return new node();
}
bool failed = false;
void addnode(int v,char* s){
int n = strlen(s);
node *u = root;
for(int i = 0;i < n;i++){
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 == true) failed=true;
u->have_value=true;
u->v=v;
}
bool bfs(){
ans.clear();
queue<node*>que;
while(!que.empty()) que.pop();
que.push(root);
while(!que.empty()){
node* u = que.front();que.pop();
if(u->have_value==false) return false;
ans.push_back(u->v);
if(u->left != NULL) que.push(u->left);
if(u->right != NULL) que.push(u->right);
}
return true;
}
bool read(){
failed = false;
root = newnode();
for(;;){
//输入到文件末尾:
if(scanf("%s",s) != 1) return false;
//终止条件:
if(!strcmp(s,"()")) break;
int v;
//sscanf的应用
sscanf(&s[1],"%d",&v);
//strchr函数的应用
addnode(v,strchr(s,',')+1);
}
if(failed == true){
cout<<"not complete"<<endl;
return true;
}
if(!bfs()) cout<<"not complete"<<endl;
else {
for(int i = 0;i < ans.size();i++){
cout<<ans[i];
putchar(i==ans.size()-1?'\n':' ');
}
}
return true;
}
int main(){
while(read());
return 0;
}
数组代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 300;
int _left[maxn],_right[maxn];
bool have_value[maxn];
int val[maxn];
int cnt;
int root = 0;
vector<int>ans;
char s[maxn];
bool failed = false;
int newnode(){
int u=++cnt;
_left[u] = _right[u] = 0;have_value[u] = false;return u;
}
void addnode(int v,char* s){
int n = strlen(s);
int u = root;
for(int i = 0;i < n;i++){
if(s[i] == 'L'){
if(_left[u] == 0) _left[u] = newnode();
u=_left[u];
}
else if(s[i] == 'R'){
if(_right[u] == 0) _right[u] = newnode();
u = _right[u];
}
}
if(have_value[u]) failed=true;
have_value[u] = true;
val[u] = v;
}
bool bfs(){
ans.clear();
queue<int>que;
while(!que.empty()) que.pop();
que.push(root);
while(!que.empty()){
int u = que.front();que.pop();
if(!have_value[u]) return false;
ans.push_back(val[u]);
if(_left[u]) que.push(_left[u]);
if(_right[u]) que.push(_right[u]);
}
return true;
}
bool read(){
failed = false;
cnt = 0;
root = newnode();
for(;;){
if(scanf("%s",s) != 1) return false;
if(!strcmp(s,"()")) break;
int v;
sscanf(&s[1],"%d",&v);
addnode(v,strchr(s,',')+1);
}
if(failed == true){
cout << "not complete"<<endl;
return true;
}
if(!bfs()) cout << "not complete"<<endl;
else {
for(int i = 0;i < ans.size();i++){
cout << ans[i];
putchar(i == ans.size()-1?'\n':' ');
}
}
return true;
}
int main(){
while(read());
return 0;
}