例题6-7 树的层次遍历 BFS遍历

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=58&mosmsg=Submission+received+with+ID+20135244

思路:根据输入的点建树,如果有重复赋值的则输出not complete ,然后就是用BFS遍历,把值存入vector容器,如果哪个节点没有值就输出not complete。

Code:

(指针实现)(AC)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int AX = 256;
char s[AX << 2];
vectorres ;
bool flag;
struct Node
{
	bool have_value;
	Node *l , *r;
	int val;
	Node():have_value(false),l(NULL),r(NULL){}
};
Node* root;

Node* newNode(){ return new Node(); }

/*void remove_tree(Node* u){        //释放内存
	if( u == NULL ) return ;
	remove_tree(u->l);
	remove_tree(u->r);
	delete u;
}*/

void addNode( int v , char* s ){
	int len = strlen(s);
	Node* u = root ;
	for( int i = 0 ; i < len ; i++ ){
		if( s[i] == 'L' ){
			if( u -> l == NULL )  u -> l = newNode();
			u = u -> l;
		}else if( s[i] == 'R' ){
			if( u -> r == NULL )  u -> r = newNode();
			u = u -> r;
		}
	}
	if( u -> have_value )  flag = false ;  
	u -> val = v;
	u -> have_value = true;
}

bool input_node(){
	//remove_tree(root);
	root = newNode();
	flag = true;
	while(true){
		if( scanf("%s",s) != 1 ) return false;
		if( !strcmp(s,"()" )) return true;
		int v;
		sscanf(&s[1],"%d",&v);
		addNode( v , strchr(s,',') + 1 ) ;
		
	}
}
bool BFS(){
	queue  q;
	res.clear();
	q.push(root);
	while( !q.empty() ){
		Node* u = q.front(); q.pop();
		if( !u -> have_value ) return false;
		res.push_back(u -> val);
		if( u -> l != NULL ) q.push(u->l);
		if( u -> r != NULL ) q.push(u->r);
	}
	return true;
}

int main(){
	//freopen("openfile.txt","r",stdin);
	while(input_node()){
		if( !BFS() ) flag = false;
		if(flag){
			int len = res.size();
			for( int i = 0 ; i < len-1 ; i++ ){
				cout << res[i] << ' ';
			}
			cout << res[len-1] << endl;
		}else{ 
			cout << "not complete" << endl;
		}
	}
	return 0;
}

数组实现:(TE)数组比指针更慢,暂时不知道如何优化

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int AX = 256;
char s[AX << 2];

const int root = 1;
int left1[AX<<2], right1[AX<<2];
bool have_value[AX<<2];
int val[AX<<2];
int cnt;
vectorres ;
bool flag;

void newtree(){
	left1[root] = right1[root] = 0;
	have_value[root] = false;
}

int newNode(){
	int u = ++cnt;
	left1[u] = right1[u] = 0;
	have_value[u] = false;
	return u;
}

void addNode( int v , char* s ){
	int len = strlen(s);
	int u = root ;
	for( int i = 0 ; i < len ; i++ ){
		if( s[i] == 'L' ){
			if( left1[u] == 0 )  left1[u] = newNode();
			u = left1[u];
		}else if( s[i] == 'R' ){
			if( right1[u] == 0 ) right1[u] = newNode();
			u = right1[u];
		}
	}
	if( have_value[u] )  flag = false ;  
	val[u] = v;
	have_value[u] = true;
}

bool input_node(){
	cnt = 0;
	newtree();
	flag = true;
	while(true){
		if( scanf("%s",s) != 1 ) return false;
		if( !strcmp(s,"()" )) return true;
		int v;
		sscanf(&s[1],"%d",&v);
		addNode( v , strchr(s,',') + 1 ) ;
	}
}
bool BFS(){
	queue  q;
	res.clear();
	q.push(root);
	while( !q.empty() ){
		int u = q.front(); q.pop();
		if( !have_value[u] ) return false;
		res.push_back(val[u]);
		if( left1[u] != 0 ) q.push(left1[u]);
		if( right1[u] != 0 ) q.push(right1[u]);
	}
	return true;
}

int main(){
	//freopen("openfile.txt","r",stdin);
	while(input_node()){
		if( !BFS() ) flag = false;
		if(flag){
			int len = res.size();
			for( int i = 0 ; i < len-1 ; i++ ){
				cout << res[i] << ' ';
			}
			cout << res[len-1] << endl;
		}else{ 
			cout << "not complete" << endl;
		}
	}
	return 0;
}



你可能感兴趣的:(算法竞赛入门经典-数据结构基础,数据结构-树)