Trees on the level

本文介绍了一种处理二叉树的算法,该算法能够实现二叉树的层次遍历并输出每一层节点的值。文章详细解释了如何通过输入一系列结点路径来构建二叉树,并最终打印出按层次顺序排列的结点值。

Background

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.

This problem involves building and traversing binary trees.

The Problem

Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.

For example, a level order traversal of the tree

picture28

is: 5, 4, 8, 11, 13, 4, 7, 2, 1.

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.

The Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses.

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.

The Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete'' should be printed.

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete

# include <iostream>
# include <cstring>
# include <queue>
# include <cstdio>
# include <vector>
# include <algorithm>
using namespace std;

char s[300];
bool failed;

//建立树的结构体;
struct Node
{
	bool have_value;    //判断当前结点有没有值:
	int v;
	Node *left,*right;  //指针,指向当前结点的左子树和右子树;
	Node():have_value(false),left(NULL),right(NULL){}    //结点的初始化;
};

Node* root;   //建立指向根结点的指针;
Node* newnode() { return new Node(); }

void addnode(int v,char *s)     //新建结点:
{
	int n =strlen(s);      //将‘,’以后的长度赋值给n;
	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)   failed=true;
	u->v=v;
	u->have_value=true;
}

void remove_tree(Node *u)
{
	if(u ==NULL)    return;
	remove_tree(u->left);
	remove_tree(u->right);
	delete u;
}

bool read_in()
{
	failed=false;
	remove_tree(root);    //用指令new开辟的空间,所以要删除;
	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);     //strchr()找到字符出现的位置:
	}
	return true;
}

bool dfs(vector < int > & ans)    //来判断树有没有完整,然后进行层次遍历:
{
	queue <Node *> q;
	ans.clear();
	q.push(root);
	while(!q.empty())
	{
		Node *u=q.front(); q.pop();
		if(!u->have_value)    return false;
		ans.push_back(u->v);
		if(u->left!=NULL) q.push(u->left);
		if(u->right!=NULL) q.push(u->right);
	}
	return true;
}

int main()
{
    //freopen("a.txr","r",stdin);
	vector < int > ans;
	while(read_in())
	{
		if(!dfs(ans))    failed=1;
		if(failed)    printf("not complete\n");
		else
		{
			for(int i=0;i<ans.size();i++)
			{
 				if(i!=0)    printf(" ");
				printf("%d",ans[i]);
			}
			printf("\n");
		}
	}
	return 0;
}
翻译:and a material design model, for simulating the characteristics of the semiconductor equipment. Mitrovic and Strang [19] applied the first-principle simulation to virtual sensor measurements to facilitate the process performed by the semiconductor processing tool. However, semiconductor manufacturing consists of hundreds of processing phases, months of processing time and correlative process flows [20]. The corresponding behavior analysis of the tool failure degradation process is very limited. Thus, the results provide neither reliable and accurate physical or mathematical models nor confident experience feedback for mechanism-based modeling. This is the main limitation of the applications of model-based approaches. In the knowledge-based methods, competitive advantages for stabilizing maintenance processes and reducing unplanned costs are realized by holistic consideration of production processes [21]. Prescriptive maintenance is known as the highest maturity and complexity level of knowledge-based maintenance [22], and “how can we control the occurrence of a specific event” should be answered and useful advice for decision-making to be given to improve and optimize the upcoming maintenance processes [23]. Nemeth et al. [22] built on the concept of prescriptive maintenance and proposed a reference model called PriMa-X to support the implementation of a prescriptive maintenance strategy and the assessment of its maturity level, facilitating the integration of data driven methods for predicting future events and identify action fields to reach an enhanced target maturity state. Padovano et al. [24] proposed a framework to construct the missing link between prescriptive maintenance and production planning and control functions in a cyber–physical production environment. Shaheen and Németh [25], Silvestri et al. [26], and Zonta et al. [27] discussed the prescriptive maintenance methods for Industry 4.0 technologies. Obtaining domain knowledge and converting it to precise rules are usually difficult as they require strong background and experience. Besides, the new situations without being covered by the knowledge bases cannot be handled. Data-driven methods are effective when acquiring practical data is more convenient than establishing physical or analytical models [28]. With the development of sensor techniques and wide applications of advanced process control technologies [29], the numerous manufacturing data provide wide opportunities for applying the data-driven prognostics methods to deal with the failure time prediction in the semiconductor manufacturing. Based on the monitoring data, the system degradation tendency can be easily understood and predicted. Generally, data-driven methods applied to the prognostics in the industry are adapted from the ones used in machine learning [29], [30]: neural networks [10], [31], k-nearest neighbor [32], [33], statistical methods [11], [34], decision trees [35], [36], clustering analysis [37], [38], and so on. Regarding the prognostics in semiconductor manufacturing, Bouaziz et al. [40] addressed a predictive approach based on the Bayesian network to analyze the device health factor. It allows early scheduling for preventive maintenance and avoids unscheduled tool downtime. Jia et al. [41] developed an adaptive method on the basis of the polynomial neural network to infer the material removal rate in the chemical-mechanical planarization process of the semiconductor fabrication. Yang and Lee [42] applied the Bayesian belief network to analyze the causal relationship of the process variables and estimate their effects on wafer quality to achieve high-classification rates for wafer quality and identify the problematic sensors when any bad wafer is detected. In reality, the quantity and quality of the data collected from the processes are problematic when it comes to developing and maintaining effective data-driven prognostics models [43]. As the failure can be slowly accumulated, the relevant data for evaluating the failure behavior would be scarce. In addition, most data-driven prognostics methods in semiconductor manufacturing focus on the deterministic estimation without giving the confidence level of the inference of the tool failure prognostics, which implies how much effect on the accuracy of the prognostic models built upon the acquired data is unknown. Using the poorly monitoring data would result in an improper estimation of the cleaning time. For more accurate prognostics, it is necessary to have a good and reliable prognostic model. As the tool failure mechanisms and uncertainty in semiconductor manufacturing are complex and unavailable, this article proposes a data-driven prognostic method to study the failure behavior and predict the tendency on the basis of the obtained production data in the fabrication process. One of the achievements is implementing the prognostics for determining the explicit cleaning time and reducing the dependence on the mechanism analysis. A novel failure factor extraction and prognostic model is developed based on autoassociative regression (AR)-Gaussian process (GP), which is the integration of the AR [44] and the GP [45]. It can be used to analyze and extract the failure factors by the comparisons between the actual data and the fault-free data. Furthermore, with the analysis of the residual tendency, the abnormalities can be detected to be used as the variables of the prognostic model. To infer the future failure behaviors in the fabrication process of semiconductor manufacturing, the data-driven prognostic model is constructed on the basis of GP to present the degradation evolution in a probability distribution and evaluate the uncertainty. Thanks to the confident interval of each estimated value, the accuracy of the prediction influenced by the collected data quality can be presented. It cannot be done by the deterministic estimation. And the suitable cleaning time for the fabrication process is given based on the discussion of the possible failure phenomena in semiconductor manufacturing. This article is structured as follows. The prognostic problem in semiconductor manufacturing is defined and its issues are discussed in Section II. Section III details the novel AR-GP method. The feasibility and the promising results of the ARGP method are validated through a numerical example and a practical semiconductor manufacturing process for the failure factor extraction and failure behavior inference, respectively, in Section IV. Finally, Section V draws conclusions.
最新发布
11-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值