1. 程式人生 > 其它 >Trees on the level UVA - 122

Trees on the level UVA - 122

技術標籤:VJ刷題練習uva

Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-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.

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 on the right 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.

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.

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

HINT

這個題目可以不適用二叉樹,可以使用map排序來解決,但學到資料結構了就使用二叉樹來解決的。

程式設計思路是每行進行讀取,然後讀取這一行中的內容,直到遇到()結束這一組資料。用指標陣列來儲存資料。只要遇到一組資料就插入到二叉樹裡面,如果二叉樹對應的結點已經插入了資料,就輸出錯誤,如果二叉樹查詢對應結點的時候遇到了中間還沒有插入的結點,那麼就先建立一個空的結點,儲存的資料string num的大小為0,(不為0說明已經存入了資料)。輸出採用的是層序遍歷,當發現有結點的資料域的長度為0那麼就說明這個點沒有插入輸出錯誤。

這個題目程式有很多細節需要注意,針對自己的程式的總結如下:

  1. 使用 new後一定要初始化結點。
  2. 每次輸出結果都要將二叉樹刪除,並將頭指標指空。
  3. 刪除結點遞迴的時候一定要先判斷左右孩子是否為空,先序遍歷也一樣。

Accepted

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<sstream>

using namespace std;
struct TREE{
	string num;
	TREE* right;
	TREE* lift;
};

void remove(TREE* head){	//刪除結點空間
	if (!head)return;
	if(head->lift)	remove(head->lift);
	if(head->right)	remove(head->right);
	delete(head);
}

bool insert(TREE* head, string var, string s) {	//插入
	TREE* p = head,*temp;
	for (int i = 0;i < s.size()-1;i++) {
		temp = s[i] == 'R' ? p->right : p->lift;
		if (temp==NULL){
			temp = new TREE;		//如果是空的就申請空間
			temp->lift = temp->right = NULL;
		}
		if (s[i] == 'R')p->right = temp;
		else p->lift = temp;
		p=temp;		//向下指
	}
	if (p->num.size())return 0;
	else { p->num = var;return 1; }
}

void print(TREE* head) {			//首先層序遍歷,然後輸出,以內要先判斷是否合法
	vector<TREE *>list;				//因為不需要邊輸出邊層序遍歷,所以不用使用佇列
	int i = 0;
	if(head) list.push_back(head);
	while (i++ < list.size()) {		//遍歷
		if (!list[i-1]->num.size()) { cout << "not complete" << endl;return; }
		if (list[i-1]->lift)list.push_back(list[i-1]->lift);
		if (list[i-1]->right)list.push_back(list[i-1]->right);
	}
	for (int i = 0;i < list.size();i++) {	//輸出
		if (i)cout << ' ' << list[i]->num;
		else cout << list[i]->num;
	}
	cout << endl;
}

using namespace std;
int main(){
	TREE* head=NULL;
	string s,svar;
	while(getline(cin,s)){		//讀取每一行
		if (!head) {
			head = new TREE;	//申請頭地址
			head->lift = head->right = NULL;
		}
		stringstream ss(s);
		while (ss >> s ) {		//讀取每一個點
			if (s == "()") {	//清空並輸出。
				print(head);
				remove(head);
				head = NULL;
				break;
			}
			int i = s.find(',');				//拆分
			svar = s.substr(1, i-1);			//數值位
			s = s.substr(i + 1, s.size()-1);//路徑
			if (!insert(head, svar, s)) {
				cout << "not complete" << endl;
				remove(head);head = NULL;
				while (ss >> s)if (s == "()")break;//清空本組資料
				while (s != "()")cin >> s;		
				break;			//調出迴圈,進行下一組
			}	
		}
	}
}